C data types

In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements. 
The C language provides basic arithmetic types, such as integer and real number types, and syntax to build array and compound types. "Headers" for the C standard library, to be used via include directives, contain definitions of support types, that have additional properties, such as providing storage with an exact size, independent of the language implementation on specific hardware platforms.
Basic types.
Main types.
The C language provides the four basic arithmetic type specifiers "char", "int", "float" and "double", and the modifiers "signed", "unsigned", "short", and "long". The following table lists the permissible combinations in specifying a large set of storage size-specific declarations.
The actual size of the integer types varies by implementation. The standard requires only size relations between the data types and minimum sizes for each data type:
The relation requirements are that the codice_1 is not smaller than codice_2, which is not smaller than codice_3, which is not smaller than codice_4. As codice_5's size is always the minimum supported data type, no other data types (except bit-fields) can be smaller.
The minimum size for codice_5 is 8 bits, the minimum size for codice_4 and codice_3 is 16 bits, for codice_2 it is 32 bits and codice_1 must contain at least 64 bits.
The type codice_3 should be the integer type that the target processor is most efficiently working with. This allows great flexibility: for example, all types can be 64-bit. However, several different integer width schemes (data models) are popular. Because the data model defines how different programs communicate, a uniform data model is used within a given operating system application interface.
In practice, codice_5 is usually 8 bits in size and codice_4 is usually 16 bits in size (as are their unsigned counterparts). This holds true for platforms as diverse as 1990s SunOS 4 Unix, Microsoft MS-DOS, modern Linux, and Microchip MCC18 for embedded 8-bit PIC microcontrollers. POSIX requires codice_5 to be exactly 8 bits in size.
Various rules in the C standard make codice_15 the basic type used for arrays suitable to store arbitrary non-bit-field objects: its lack of padding bits and trap representations, the definition of "object representation", and the possibility of aliasing.
The actual size and behavior of floating-point types also vary by implementation. The only guarantee is that codice_16 is not smaller than codice_17, which is not smaller than codice_18. Usually, the 32-bit and 64-bit IEEE 754 binary floating-point formats are used.
The C99 standard includes new real floating-point types codice_19 and codice_20, defined in codice_21. They correspond to the types used for the intermediate results of floating-point expressions when codice_22 is 0, 1, or 2. These types may be wider than codice_16.
C99 also added complex types: codice_24, codice_25, codice_26.
Boolean type.
C99 added a boolean (true/false) type codice_27. Additionally, the codice_28 header defines codice_29 as a convenient alias for this type, and also provides macros for codice_30 and codice_31. codice_27 functions similarly to a normal integer type, with one exception: any assignments to a codice_27 that are not 0 (false) are stored as 1 (true). This behavior exists to avoid integer overflows in implicit narrowing conversions. For example, in the following code:
unsigned char b = 256;
if (b) {
 /* do something */
Variable codice_34 evaluates to false if codice_15 has a size of 8 bits. This is because the value 256 does not fit in the data type, which results in the lower 8 bits of it being used, resulting in a zero value. However, changing the type causes the previous code to behave normally:
_Bool b = 256;
if (b) {
 /* do something */
The type "_Bool" also ensures true values always compare equal to each other:
_Bool a = 1, b = 2;
if (a == b) {
 /* do something */
Size and pointer difference types.
The C language specification includes the s codice_36 and codice_37 to represent memory-related quantities. Their size is defined according to the target processor's arithmetic capabilities, not the memory capabilities, such as available address space. Both of these types are defined in the codice_38 header (codice_39 in C++).
codice_36 is an unsigned integer type used to represent the size of any object (including arrays) in the particular implementation. The operator yields a value of the type codice_36. The maximum size of codice_36 is provided via codice_43, a macro constant which is defined in the codice_44 header (codice_45 header in C++). codice_36 is guaranteed to be at least 16 bits wide. Additionally, POSIX includes codice_47, which is a signed integer type of the same width as codice_36.
codice_37 is a signed integer type used to represent the difference between pointers. It is guaranteed to be valid only against pointers of the same type; subtraction of pointers consisting of different types is implementation-defined.
Interface to the properties of the basic types.
Information about the actual properties, such as size, of the basic arithmetic types, is provided via macro constants in two headers: codice_50 header (codice_51 header in C++) defines macros for integer types and codice_52 header (codice_53 header in C++) defines macros for floating-point types. The actual values depend on the implementation.
Fixed-width integer types.
The C99 standard includes definitions of several new integer types to enhance the portability of programs. The already available basic integer types were deemed insufficient, because their actual sizes are implementation defined and may vary across different systems. The new types are especially useful in embedded environments where hardware usually supports only several types and that support varies between different environments. All new types are defined in codice_112 header (codice_113 header in C++) and also are available at codice_44 header (codice_45 header in C++). The types can be grouped into the following categories:
The following table summarizes the types and the interface to acquire the implementation details ("n" refers to the number of bits):
Printf and scanf format specifiers.
The codice_112 header (codice_113 in C++) provides features that enhance the functionality of the types defined in the codice_44 header. It defines macros for printf format string and scanf format string specifiers corresponding to the types defined in codice_44 and several functions for working with the codice_120 and codice_121 types. This header was added in C99.
The macros are in the format codice_122. Here "{fmt}" defines the output formatting and is one of codice_123 (decimal), codice_124 (hexadecimal), codice_125 (octal), codice_126 (unsigned) and codice_127 (integer). "{type}" defines the type of the argument and is one of codice_128, codice_129, codice_130, codice_131, codice_132, where codice_128 corresponds to the number of bits in the argument.
The macros are in the format codice_134. Here "{fmt}" defines the output formatting and is one of codice_123 (decimal), codice_124 (hexadecimal), codice_125 (octal), codice_126 (unsigned) and codice_127 (integer). "{type}" defines the type of the argument and is one of codice_128, codice_129, codice_130, codice_131, codice_132, where codice_128 corresponds to the number of bits in the argument.
Additional floating-point types.
Similarly to the fixed-width integer types, ISO/IEC TS 18661 specifies floating-point types for IEEE 754 interchange and extended formats in binary and decimal:
Structures.
Structures aggregate the storage of multiple data items, of potentially differing data types, into one memory block referenced by a single variable. The following example declares the data type codice_150 which contains the name and birthday of a person. The structure definition is followed by a declaration of the variable codice_151 that allocates the needed storage.
struct birthday {
 char name[20];
 int day;
 int month;
 int year;
struct birthday John;
The memory layout of a structure is a language implementation issue for each platform, with a few restrictions. The memory address of the first member must be the same as the address of structure itself. Structures may be initialized or assigned to using compound literals. A function may directly return a structure, although this is often not efficient at run-time. Since C99, a structure may also end with a flexible array member.
A structure containing a pointer to a structure of its own type is commonly used to build linked data structures:
struct node {
 int val;
 struct node *next;
Arrays.
For every type codice_152, except void and function types, there exist the types "array of codice_153 elements of type codice_152". An array is a collection of values, all of the same type, stored contiguously in memory. An array of size codice_153 is indexed by integers from codice_156 up to and including codice_157. Here is a brief example:
int cat[10]; // array of 10 elements, each of type int
Arrays can be initialized with a compound initializer, but not assigned. Arrays are passed to functions by passing a pointer to the first element. Multidimensional arrays are defined as "array of array …", and all except the outermost dimension must have compile-time constant size:
int a[10][8]; // array of 10 elements, each of type 'array of 8 int elements'
Pointers.
Every data type codice_152 has a corresponding type "pointer to codice_152". A pointer is a data type that contains the address of a storage location of a variable of a particular type. They are declared with the asterisk (codice_160) type declarator following the basic storage type and preceding the variable name. Whitespace before or after the asterisk is optional.
char *square;
long *circle;
int *oval;
Pointers may also be declared for pointer data types, thus creating multiple indirect pointers, such as and , including pointers to array types. The latter are less common than an array of pointers, and their syntax may be confusing:
char *pc[10]; // array of 10 elements of 'pointer to char'
char (*pa)[10]; // pointer to a 10-element array of char
The element codice_161 requires ten blocks of memory of the size of "pointer to codice_5" (usually 40 or 80 bytes on common platforms), but element codice_163 is only one pointer (size 4 or 8 bytes), and the data it refers to is an array of ten bytes ().
Unions.
A union type is a special construct that permits access to the same memory block by using a choice of differing type descriptions. For example, a union of data types may be declared to permit reading the same data either as an integer, a float, or any other user declared type:
union {
 int i;
 float f;
 struct {
 unsigned int u;
 double d;
 } s;
} u;
The total size of codice_126 is the size of codice_165 — which happens to be the sum of the sizes of codice_166 and codice_167 — since codice_168 is larger than both codice_127 and codice_170. When assigning something to codice_171, some parts of codice_172 may be preserved if codice_171 is smaller than codice_172.
Reading from a union member is not the same as casting since the value of the member is not converted, but merely read.
Function pointers.
Function pointers allow referencing functions with a particular signature. For example, to store the address of the standard function codice_175 in the variable codice_176:
int (*my_int_f)(int) = &abs;
// the & operator can be omitted, but makes clear that the "address of" abs is used here
Function pointers are invoked by name just like normal function calls. Function pointers are separate from pointers and void pointers.
Type qualifiers.
The aforementioned types can be characterized further by type qualifiers, yielding a "qualified type". and C11, there are four type qualifiers in standard C: codice_177 (C89), codice_178 (C89), codice_179 (C99) and codice_180 (C11) the latter has a private name to avoid clashing with user names, but the more ordinary name codice_181 can be used if the codice_182 header is included. Of these, codice_177 is by far the best-known and most used, appearing in the standard library and encountered in any significant use of the C language, which must satisfy const-correctness. The other qualifiers are used for low-level programming, and while widely used there, are rarely used by typical programmers.