12.07.2015 Views

C Programming ppt slides, PDF on data types - Tenouk C & C++

C Programming ppt slides, PDF on data types - Tenouk C & C++

C Programming ppt slides, PDF on data types - Tenouk C & C++

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C (Basic) Data Types-different <strong>data</strong> representati<strong>on</strong>s need different<strong>types</strong> in programming-www.tenouk.com, ©1/31


C BASIC (DATA) TYPESIn C, <strong>data</strong> type categorized as:1. Primitive Types in ANSI C (C89)/ISO C (C90) -char, short, int, float and double.2. Primitive Types added to ISO C (C99) - l<strong>on</strong>gl<strong>on</strong>g3. User Defined Types – struct, uni<strong>on</strong>, enumand typedef (will be discussed in separate sessi<strong>on</strong>).4. Derived Types – pointer, array and functi<strong>on</strong>pointer (will be discussed in separate sessi<strong>on</strong>).www.tenouk.com, ©2/31


C BASIC (DATA) TYPESType Size in Bits CommentsPrimitive Types in ANSI C (C89)/ISO C (C90)OtherNameschar ≥ 8• sizeof() will give the size in units of chars.• need not be 8-bit• The number of bits is given by the CHAR_BITmacro in the limits.h header.• Integer operati<strong>on</strong>s can be performed portably<strong>on</strong>ly for the range: 0 ~ 127 (2 8 / 2).—signed charunsigned charSame as charbut guaranteedto be signedSame as charbut guaranteedto be unsigned.• Can store integers in the range: -127 ~ 127(2 8 ) portably.• Can store integers in the range: 0 ~ 255 (2 8 )portably.——char type program examplewww.tenouk.com, ©3/31


C BASIC (DATA) TYPES• A sample output.www.tenouk.com, ©4/31


shortunsignedshortintunsigned int≥ 16, ≥ size ofcharSame as shortbut unsigned≥ 16, ≥ size ofshortC BASIC (DATA) TYPESSame as int butunsigned.• Can store integers in the range: -32767 ~ 32767 (2 16 / 2) portably.• Reduce memory usage thoughthe resulting executable may belarger and probably slower ascompared to using int.• Can store integers in the range: 0~ 65535 (2 16 ) portably.• Used to reduce memory usagethough the resulting executablemay be larger and probablyslower as compared to using int.• Basic signed integer type.• Represent a typical processor’s<strong>data</strong> size which is word-size• An integral <strong>data</strong>-type.• Can store integers in the range: -32767 ~ 32767 (2 16 / 2) portably.• Can store integers in the range: 0~ 65535 (2 16 ) portably.short int,signed short,signed short intunsigned short intsigned,signed intunsignedshort int type program examplewww.tenouk.com, ©5/31


C BASIC (DATA) TYPES• A sample output.www.tenouk.com, ©6/31


C BASIC (DATA) TYPESl<strong>on</strong>gunsignedl<strong>on</strong>gfloatdoublel<strong>on</strong>g double≥ 32, ≥ size ofintSame as l<strong>on</strong>gbut unsigned≥ size of char≥ size of float≥ size ofdouble• l<strong>on</strong>g signed integer type.• Can store integers in the range: -2147483647 ~ 2147483647 (2 32 / 2)portably.• Can store integers in the range: 0 ~4294967295 (2 32 ) portably.• Used to reduce memory usage whenthe values used do not vary widely.• The format used is implementati<strong>on</strong>defined and unnecessarily obeys theIEEE 754 single-precisi<strong>on</strong> format.• unsigned cannot be specified.• Typical floating-point <strong>data</strong> type usedby processor.• The format used is implementati<strong>on</strong>defined and unnecessarily obeys theIEEE 754 double-precisi<strong>on</strong> format.• unsigned cannot be specified.l<strong>on</strong>g int,signed l<strong>on</strong>g,signed l<strong>on</strong>g intunsigned l<strong>on</strong>gint• unsigned cannot be specified. ———l<strong>on</strong>g int type program example www.tenouk.com, ©7/31


C BASIC (DATA) TYPES• A sample output.www.tenouk.com, ©8/31


C BASIC (DATA) TYPESType Size in Bits Comments Other NamesPrimitive Types added to ISO C (C99)l<strong>on</strong>g l<strong>on</strong>gunsignedl<strong>on</strong>g l<strong>on</strong>g≥ 64, ≥ sizeof l<strong>on</strong>gSame asl<strong>on</strong>g l<strong>on</strong>g,butunsigned.• Can store integers inthe range: -9223372036854775807 ~9223372036854775807 (2 64 / 2) portably.• Can store integers inthe range: 0 ~18446744073709551615 (2 64 ) portably.l<strong>on</strong>g l<strong>on</strong>g int,signed l<strong>on</strong>g l<strong>on</strong>g,signed l<strong>on</strong>g l<strong>on</strong>gintunsigned l<strong>on</strong>g l<strong>on</strong>gintL<strong>on</strong>g l<strong>on</strong>g int type program examplewww.tenouk.com, ©9/31


C BASIC (DATA) TYPES• A sample output.www.tenouk.com, ©10/31


C BASIC (DATA) TYPESintmax_tuintmax_tSigned integer <strong>types</strong>capable ofrepresenting anyvalue of any signedinteger type.Unsigned integer<strong>types</strong> capable ofrepresenting anyvalue of anyunsigned integertype• It is a typedef representsthe signed integer type withlargest possible range.• If you want an integer withthe widest range possible <strong>on</strong>the platform <strong>on</strong> which it isbeing used.• It is a typedef representsthe unsigned integer typewith largest possible range.• If you want an integer withthe widest range possible <strong>on</strong>the platform <strong>on</strong> which it isbeing used.——Not supported by MSV<strong>C++</strong> < 2012Program examplewww.tenouk.com, ©11/31


C BASIC (DATA) TYPES• A sample output.www.tenouk.com, ©12/31


C BASIC (DATA) TYPES• Unfortunately this is not supported by MSV<strong>C++</strong>< 2012• int<strong>types</strong>.h vs. stdint.h: The C99 standardsays that int<strong>types</strong>.h includes stdint.h, sothere's no need to include stdint.h separately ina standard envir<strong>on</strong>ment.• Some implementati<strong>on</strong>s have int<strong>types</strong>.h but notstdint.h.• VS/V<strong>C++</strong> users may want to use msint<strong>types</strong>.• Other references,1. http://www.qnx.com/developers/docs/6.5.0/index.jsp?topic=/com.qnx.doc.dinkum_en_c99/stdint.html2. http://pubs.opengroup.org/<strong>on</strong>linepubs/007904975/basedefs/stdint.h.html3. http://publib.boulder.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frtref%2Fstdinth.htmwww.tenouk.com, ©13/31


C BASIC (DATA) TYPES• Actual size of integer <strong>types</strong> varies by implementati<strong>on</strong>: Windows, Linux,BSD etc.• The <strong>on</strong>ly guarantee is that the l<strong>on</strong>g l<strong>on</strong>g is not smaller than l<strong>on</strong>g,which is not smaller than int, which is not smaller than short.l<strong>on</strong>g l<strong>on</strong>g > l<strong>on</strong>g > int > short• int should be the integer type that the target processor is most efficientworking with. For example, all <strong>types</strong> can be 64-bit.• Actual size of floating point <strong>types</strong> also varies by implementati<strong>on</strong>.• The <strong>on</strong>ly guarantee is that the l<strong>on</strong>g double is not smaller thandouble, which is not smaller than float.l<strong>on</strong>g double > double > float• The 32-bit and 64-bit IEEE 754 floating point formats should be used.www.tenouk.com, ©14/31


C BASIC (DATA) TYPESBoolean type• The boolean (true/false) type is _Bool defined instdbool.h• The stdbool.h type also defines a few useful identifiersas macros: bool is defined as _Bool, true as 1, false as0.• Additi<strong>on</strong>ally, __bool_true_false_are_defined isdefined as 1.• The _Bool type and stdbool.h header did not exist inpre-1999 versi<strong>on</strong>s of the standard.bool in V<strong>C++</strong> examplebool in Pelles C examplewww.tenouk.com, ©15/31


C BASIC (DATA) TYPESSize and pointer difference <strong>types</strong>• Separate size_t and ptrdiff_t <strong>types</strong> to represent memory-relatedquantities.• Existing <strong>types</strong> were inadequate, because their size is definedaccording to the target processor's arithmetic capabilities, not thememory capabilities, such as the address space availability.• Both of these <strong>types</strong> are defined in the stddef.h header file (cstddef in<strong>C++</strong>).• size_t is used to represent the maximum size of any object (includingarrays) in the particular implementati<strong>on</strong>.• An unsigned integer type used to represent the sizes of objectssize_t program examplewww.tenouk.com, ©16/31


C BASIC (DATA) TYPES• A sample output.www.tenouk.com, ©17/31


C BASIC (DATA) TYPES• Used as the return type of the sizeof() operator.• The maximum size of size_t is provided via SIZE_MAX, amacro c<strong>on</strong>stant which is defined in the stdint.h header file(cstdint in <strong>C++</strong>).• It is guaranteed to be at least 65535.• ptrdiff_t is used to represent the difference betweenpointers.• Is the signed integer type of the result of subtracting twopointers.• The type's size is chosen so that it could store themaximum size of a theoretically possible array of any type.• On a 32-bit system ptrdiff_t will take 32 bits and <strong>on</strong> a64-bit <strong>on</strong>e - 64 bits and it is portable.size_t and ptrdiff_t: a storyptrdiff_t program examplewww.tenouk.com, ©18/31


C BASIC (DATA) TYPES• A sample output.www.tenouk.com, ©19/31


C BASIC (DATA) TYPESInterface to the properties of the basic <strong>types</strong>• Informati<strong>on</strong> about the actual properties, such assize, of the basic arithmetic <strong>types</strong>, is provided viamacro c<strong>on</strong>stants in two header files,1) limits.h header (climits in <strong>C++</strong>) definesmacros for integer <strong>types</strong>.2) float.h header (cfloat in <strong>C++</strong>) defines macrosfor floating-point <strong>types</strong>.• The actual values depend <strong>on</strong> the implementati<strong>on</strong>.www.tenouk.com, ©20/31


C BASIC (DATA) TYPESFixed width integer <strong>types</strong>• C99 standard includes definiti<strong>on</strong>s of several new integer<strong>types</strong> to enhance programs’ portability.• Existing basic integer <strong>types</strong> were c<strong>on</strong>sidered inadequate;because their actual sizes are implementati<strong>on</strong> defined andmay vary across different systems.• The new <strong>types</strong> are especially useful in embeddedenvir<strong>on</strong>ments where hardware supports limited to several<strong>types</strong> and varies from system to system.• All new <strong>types</strong> are defined in int<strong>types</strong>.h (cint<strong>types</strong> in <strong>C++</strong>)and stdint.h (cstdint in <strong>C++</strong>) header files.• The <strong>types</strong> can be grouped into the following categories:www.tenouk.com, ©21/31


C BASIC (DATA) TYPES• Exact width integer <strong>types</strong> - are guaranteed to have thesame number N of bits across all implementati<strong>on</strong>s.Included <strong>on</strong>ly if it is available in the implementati<strong>on</strong>.• Least width integer <strong>types</strong> - are guaranteed to be thesmallest type available in the implementati<strong>on</strong>, that has atleast specified number N of bits. Guaranteed to bespecified for at least N=8, 16, 32, 64.• Fastest integer <strong>types</strong> - are guaranteed to be the fastestinteger type available in the implementati<strong>on</strong>, that has atleast specified number N of bits. Guaranteed to bespecified for at least N=8, 16, 32, 64.• Pointer integer <strong>types</strong> - are guaranteed to be able to hold apointer.• Maximum width integer <strong>types</strong> - are guaranteed to be thelargest integer type in the implementati<strong>on</strong>.www.tenouk.com, ©22/31


C BASIC (DATA) TYPES• The following table summarizes the <strong>types</strong> andthe interface to acquire the implementati<strong>on</strong>details (N refers to the number of bits).Type categorySigned <strong>types</strong>Unsigned <strong>types</strong>Type Min value Max value Type Min value Max valueExact width intN_t INTN_MIN INTN_MAX uintN_t 0 UINTN_MAXLeast width int_leastN_t INT_LEASTN_MIN INT_LEASTN_MAX uint_leastN_t 0 UINT_LEASTN_MAXFastest int_fastN_t INT_FASTN_MIN INT_FASTN_MAX uint_fastN_t 0 UINT_FASTN_MAXPointer intptr_t INTPTR_MIN INTPTR_MAX uintptr_t 0 UINTPTR_MAXMaximum width intmax_t INTMAX_MIN INTMAX_MAX uintmax_t 0 UINTMAX_MAXwww.tenouk.com, ©23/31


USER DEFINED (DATA) TYPESKeyword Size Notestruct≥ sum of sizeof eachmembertag or label is opti<strong>on</strong>alstruct theEmployee {int age;double salary;char department;char name[15];char address[5][25];};struct theEmployee workerRec;struct newPoint {short xPoint;short yPoint;} justPoint;An aggregate type which can c<strong>on</strong>tain morethan <strong>on</strong>e different <strong>types</strong>.typedef struct{int x;int SomeArray[100];} MyFoo;int main(){MyFoo strctVar;}return 0;justPoint thePoint;www.tenouk.com, ©24/31


USER DEFINED (DATA) TYPESuni<strong>on</strong>≥ size of thelargestmemberuni<strong>on</strong> someData{int pNum;float qNum;double rNum;};uni<strong>on</strong> someData simpleData;uni<strong>on</strong> OtherData{char aNum;int xNum;float fNum;} simpleData;simpleData saveData;An aggregate type which can c<strong>on</strong>tain more than<strong>on</strong>e other <strong>types</strong>. uni<strong>on</strong> uses shared memoryspace compared to struct, so <strong>on</strong>ly <strong>on</strong>e membercan be accessed at <strong>on</strong>e time.www.tenouk.com, ©25/31


USER DEFINED (DATA) TYPESenum≥ size ofcharEnumerati<strong>on</strong>s are a separate type fromints, though they are mutuallyc<strong>on</strong>vertible. Used to declare identifiers asc<strong>on</strong>stants in an ordered manner.enum ndays {M<strong>on</strong>, Tue, Wed, Thu, Fri, Sat, Sun};/ * Creates enum days type, which the identifiers are setautomatically to the integers 0 to 6. */enum ndays ndayCount;enum trafficDirecti<strong>on</strong>{north,south,east,west};enum trafficDirecti<strong>on</strong> newDirecti<strong>on</strong>;enum cColor = {red = 2,green, blue, black};Enum cColor ccolorCode;www.tenouk.com, ©26/31


typedefUSER DEFINED (DATA) TYPESsame as the type;being given a newnametypedef used to give new identifier names or alias (tosimplify the l<strong>on</strong>g identifier names), normally used foraggregate defined <strong>types</strong>.typedef unsigned char BYTE; /* Declares BYTE to be a syn<strong>on</strong>ym for unsigned char */typedef float FLOAT; /* Declares FLOAT (uppercase letter) to be a syn<strong>on</strong>ym for unsigned float(lowercase) */tag or label is opti<strong>on</strong>altypedef struct simpleData{ int nData;char cData;} newNameType;Ortypedef struct { int nData;char cData;} newNameType;newNameType strctType;typedef uni<strong>on</strong> unData{double lngSalary;int nDay;}newUntype;newUnType lntotalSalary;typedef struct TOKEN_SOURCE {CHAR SourceName[8];LUID SourceIdentifier;} TOKEN_SOURCE, *PTOKEN_SOURCE;TOKEN_SOURCE newToken;typedef enum DayNames { M<strong>on</strong>day,Tuesday,Wednesday,Thursday,Friday, Saturday, Sunday} Weekdays;Weekdays dayOfWeek;www.tenouk.com, ©27/31


type*(a pointer)DERIVED (DATA) TYPESType Size Note• Hold the memory address which point to theactual <strong>data</strong>/value.• 0 address always represents the null pointer (anaddress where no <strong>data</strong> can be placed),irrespective of what bit sequence represents thevalue of a null pointer.• Pointers to different <strong>types</strong> will have different sizes.≥ size of char So they are not c<strong>on</strong>vertible to <strong>on</strong>e another.• Even in an implementati<strong>on</strong> which guarantees all<strong>data</strong> pointers to be of the same size, functi<strong>on</strong>pointers and <strong>data</strong> pointers are in generalincompatible with each other.• For functi<strong>on</strong>s taking a variable number ofarguments, the arguments passed must be ofappropriate type.char *ptoChar;char csimpleChr = 'T';char *chptr;// assignmentchptr = &csimpleChr;int iNumber = 20;int *imyPtr = &iNumber;www.tenouk.com, ©28/31


DERIVED (DATA) TYPEStype [integer](an array)≥ integer × size oftype• Use to declare a variable withcollecti<strong>on</strong> of identical properties or<strong>types</strong>.• Simplify variable declarati<strong>on</strong>.• In a declarati<strong>on</strong> which also initializesthe array (including a functi<strong>on</strong>parameter declarati<strong>on</strong>), the size ofthe array (the integer) can beomitted, which is called unsized.• type [ ] is not the same as type*.Only under some circumstances <strong>on</strong>ecan be c<strong>on</strong>verted to the other.int fstudentNumber[3] = {4,7,1};int nrowandColumn[1][2] = {34, 21};int nl<strong>on</strong>gHeightWidth[3][4][5] = 0;char cName1[ ] ={'a','r','r','a','y'};char cName2[ ] = {"array"};char cName3[6] = "array";int nrowCol[2][3] = {4,2,3,7,2,8};www.tenouk.com, ©29/31


DERIVED (DATA) TYPEStype (commadelimitedlist of<strong>types</strong>/declarati<strong>on</strong>s)(a functi<strong>on</strong> pointers)/* two arguments functi<strong>on</strong> pointer */int (* fptr) (int arg1, int arg2)— • allow referencing functi<strong>on</strong>s with aparticular signature.• Functi<strong>on</strong> pointers are invoked byname just like normal functi<strong>on</strong>calls. Functi<strong>on</strong> pointers areseparate from pointers and voidpointers./* to store the address of the standardfuncti<strong>on</strong> stdFunct in the variable myIntFunct */int (*myIntFunct)(int) = stdFunct;www.tenouk.com, ©30/31


End of the C <strong>data</strong> <strong>types</strong>www.tenouk.com, ©31/31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!