C Programming Data Type - Int, Float, Double Etc
C Programming Data Type - Int, Float, Double Etc
1. Primitive Types in ANSI C (C89)/ISO C (C90) char, short, int, float and double. 2. Primitive Types added to ISO C (C99) - long long 3. User Defined Types struct, union, enum and typedef. 4. Derived Types pointer, array and function pointer.
char
sizeof() will give the size in units of chars. need not be 8-bit The number of bits is given by the CHAR_BIT macro in the limits.h header. Integer operations can be performed portably only for the range: 0 ~ 127 (28 / 2). Can store integers in the range: -127 ~ 127 (28) portably.
signed char
unsigned char
Same as char but guaranteed to be signed Same as char but guaranteed to be unsigned.
short
unsigned short
int
unsigned int
Can store integers in the range: 32767 ~ 32767 (216 / 2) portably. Reduce memory usage though the resulting executable may be larger and probably slower as compared to using int. Can store integers in the range: 0 ~ 65535 (216) portably. Used to reduce memory usage though the resulting executable may be larger and probably slower as compared to using int. Basic signed integer type. Represent a typical processors data size which is word-size An integral data-type. Can store integers in the range: 32767 ~ 32767 (216 / 2) portably. Can store integers in the range: 0 ~ 65535 (216) portably.
unsigned
long
unsigned long
float
size of char
double
size of float
long signed integer type. Can store integers in the range: 2147483647 ~ 2147483647 (232 / 2) portably. Can store integers in the range: 0 ~ 4294967295 (232) portably. Used to reduce memory usage when the values used do not vary widely. The format used is implementation defined and unnecessarily obeys the IEEE 754 single-precision format. unsigned cannot be specified. Typical floating-point data type used by processor. The format used is implementation defined and unnecessarily obeys the IEEE 754 double-precision format. unsigned cannot be specified. unsigned cannot be specified.
long int, signed long, signed long int unsigned long int
long double
size of double
long long
Primitive Types added to ISO C (C99) Can store integers in long long int, the range: 64, size 922337203685477580 signed long long, of long signed long long 7~ 922337203685477580 int 7 (264 / 2) portably. Same as Can store integers in long long, unsigned long long the range: 0 ~ but 184467440737095516 int unsigned. 15 (264) portably.
Long long int type program example
intmax_t
Signed integer types capable of representing any value of any signed integer type.
uintmax_t
Unsigned integer types capable of representing any value of any unsigned integer type
It is a typedef represents the signed integer type with largest possible range. If you want an integer with the widest range possible on the platform on which it is being used. It is a typedef represents the unsigned integer type with largest possible range. If you want an integer with the widest range possible on the platform on which it is being used.
Program example
The boolean (true/false) type is _Bool defined in stdbool.h The stdbool.h type also defines a few useful identifiers as macros: bool is defined as _Bool, true as 1, false as 0.
Existing types were inadequate, because their size is defined according to the target processor's arithmetic capabilities, not the memory capabilities, such as the address space availability. Both of these types are defined in the stddef.h header file (cstddef in C++).
size_t is used to represent the maximum size of any object (including arrays) in the particular implementation. An unsigned integer type used to represent the sizes of objects
size_t program example
Information about the actual properties, such as size, of the basic arithmetic types, is provided via macro constants in two header files,
1) limits.h header (climits in C++) defines macros for integer types. 2) float.h header (cfloat in C++) defines macros for floating-point types.
Signed types
Min value INTN_MIN Max value INTN_MAX Type uintN_t
Unsigned types
Min value 0 0 0 0 0 Max value UINTN_MAX UINT_LEASTN_MAX UINT_FASTN_MAX UINTPTR_MAX UINTMAX_MAX
int_leastN_t INT_LEASTN_MIN INT_LEASTN_MAX uint_leastN_t int_fastN_t intptr_t intmax_t INT_FASTN_MIN INTPTR_MIN INTMAX_MIN INT_FASTN_MAX uint_fastN_t INTPTR_MAX INTMAX_MAX uintptr_t uintmax_t
sum of size An aggregate type which can contain more struct of each than one different types. member
tag or label is optional struct theEmployee { int age; double salary; char department; char name[15]; char address[5][25]; }; struct theEmployee workerRec; typedef struct { int x; int SomeArray[100]; } MyFoo; int main() { MyFoo strctVar; return 0; }
union someData { int pNum; float qNum; double rNum; }; union someData simpleData; union OtherData{ char aNum; int xNum; float fNum; } simpleData; simpleData saveData;
size of char
Enumerations are a separate type from ints, though they are mutually convertible. Used to declare identifiers as constants in an ordered manner.
enum ndays {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; / * Creates enum days type, which the identifiers are set automatically to the integers 0 to 6. */ enum ndays ndayCount;
enum trafficDirection{ north, south, east, west }; enum trafficDirection newDirection;
typedef struct TOKEN_SOURCE { CHAR SourceName[8]; LUID SourceIdentifier; } TOKEN_SOURCE, *PTOKEN_SOURCE; TOKEN_SOURCE newToken;
Or
typedef struct { int nData; char cData;} newNameType; newNameType strctType;
typedef union unData{ double lngSalary; int nDay; }newUntype; newUnType lntotalSalary;
typedef enum DayNames { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } Weekdays; Weekdays dayOfWeek;
type*
size of char
(a pointer)
Note Hold the memory address which point to the actual data/value. 0 address always represents the null pointer (an address where no data can be placed), irrespective of what bit sequence represents the value of a null pointer. Pointers to different types will have different sizes. So they are not convertible to one another. Even in an implementation which guarantees all data pointers to be of the same size, function pointers and data pointers are in general incompatible with each other. For functions taking a variable number of arguments, the arguments passed must be of appropriate type.
char *ptoChar; char csimpleChr = 'T'; char *chptr; // assignment chptr = &csimpleChr;
Use to declare a variable with collection of identical properties or types. Simplify variable declaration. In a declaration which also initializes the array (including a function parameter declaration), the size of the array (the integer) can be omitted, which is called unsized. type [ ] is not the same as type*. Only under some circumstances one can be converted to the other.
char cName1[ ] = {'a','r','r','a','y'}; char cName2[ ] = {"array"}; char cName3[6] = "array"; int nrowCol[2][3] = {4,2,3,7,2,8};
allow referencing functions with a particular signature. Function pointers are invoked by name just like normal function calls. Function pointers are separate from pointers and void pointers.
*/
/* two arguments function pointer int (* fptr) (int arg1, int arg2)
/* to store the address of the standard function stdFunct in the variable myIntFunct */ int (*myIntFunct)(int) = stdFunct;