HOME

TheInfoList



OR:

In the
C programming language ''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well a ...
, 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 An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language o ...
and
real number In mathematics, a real number is a number that can be used to measure a ''continuous'' one-dimensional quantity such as a distance, duration or temperature. Here, ''continuous'' means that values can have arbitrarily small variations. Every r ...
types, and syntax to build array and compound types. ''Headers'' for the
C standard library The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard.ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the original ANSI C standard, it was ...
, to be used via
include directive Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
s, 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 An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language o ...
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 long long is not smaller than long, which is not smaller than int, which is not smaller than short. As char's size is always the minimum supported data type, no other data types (except bit-fields) can be smaller. The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and long long must contain at least 64 bits. The type int 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, char is usually 8 bits in size and short is usually 16 bits in size (as are their unsigned counterparts). This holds true for platforms as diverse as 1990s
SunOS SunOS is a Unix-branded operating system developed by Sun Microsystems for their workstation and server computer systems. The ''SunOS'' name is usually only used to refer to versions 1.0 to 4.1.4, which were based on BSD, while versions 5.0 and ...
 4 Unix, Microsoft
MS-DOS MS-DOS ( ; acronym for Microsoft Disk Operating System, also known as Microsoft DOS) is an operating system for x86-based personal computers mostly developed by Microsoft. Collectively, MS-DOS, its rebranding as IBM PC DOS, and a few oper ...
, modern
Linux Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which in ...
, and Microchip MCC18 for embedded 8-bit PIC
microcontroller A microcontroller (MCU for ''microcontroller unit'', often also MC, UC, or μC) is a small computer on a single VLSI integrated circuit (IC) chip. A microcontroller contains one or more CPUs (processor cores) along with memory and programmable ...
s.
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
requires char to be exactly 8 bits in size. Various rules in the C standard make unsigned char 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 requirement is that long double is not smaller than double, which is not smaller than float. Usually, the 32-bit and 64-bit
IEEE 754 The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found in ...
binary floating-point formats are used for float and double respectively. The
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
standard includes new real floating-point types float_t and double_t, defined in <math.h>. They correspond to the types used for the intermediate results of floating-point expressions when FLT_EVAL_METHOD is 0, 1, or 2. These types may be wider than long double. C99 also added
complex Complex commonly refers to: * Complexity, the behaviour of a system whose components interact in multiple ways so possible interactions are difficult to describe ** Complex system, a system composed of many components which may interact with each ...
types: float _Complex, double _Complex, long double _Complex.


Boolean type

C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
added a boolean (true/false) type _Bool. Additionally, the <stdbool.h> header defines bool as a convenient alias for this type, and also provides macros for true and false. _Bool functions similarly to a normal integer type, with one exception: any assignments to a _Bool that are not 0 (false) are stored as 1 (true). This behavior exists to avoid
integer overflow In computer programming, an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either higher than the maximum or lower t ...
s in implicit narrowing conversions. For example, in the following code: unsigned char b = 256; if (b) Variable b evaluates to false if unsigned char 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) The type ''_Bool'' also ensures true values always compare equal to each other: _Bool a = 1, b = 2; if (a

b)


Size and pointer difference types

The C language specification includes the s size_t and ptrdiff_t 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 header (cstddef in C++). size_t 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 size_t. The maximum size of size_t is provided via SIZE_MAX, a macro constant which is defined in the < stdint.h> header (cstdint header in C++). size_t is guaranteed to be at least 16 bits wide. Additionally, POSIX includes ssize_t, which is a signed integer type of the same width as size_t. ptrdiff_t 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: header (climits header in C++) defines macros for integer types and header (cfloat header in C++) defines macros for floating-point types. The actual values depend on the implementation.


Properties of integer types

* CHAR_BIT – size of the char type in bits (at least 8 bits) * SCHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN(C99) – minimum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long * SCHAR_MAX, SHRT_MAX, INT_MAX, LONG_MAX, LLONG_MAX(C99) – maximum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long * UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, ULLONG_MAX(C99) – maximum possible value of unsigned integer types: unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long * CHAR_MIN – minimum possible value of char * CHAR_MAX – maximum possible value of char * MB_LEN_MAX – maximum number of bytes in a multibyte character


Properties of floating-point types

* FLT_MIN, DBL_MIN, LDBL_MIN – minimum normalized positive value of float, double, long double respectively * FLT_TRUE_MIN, DBL_TRUE_MIN, LDBL_TRUE_MIN (C11) – minimum positive value of float, double, long double respectively * FLT_MAX, DBL_MAX, LDBL_MAX – maximum finite value of float, double, long double, respectively * FLT_ROUNDS – rounding mode for floating-point operations * FLT_EVAL_METHOD (C99) – evaluation method of expressions involving different floating-point types * FLT_RADIX – radix of the exponent in the floating-point types * FLT_DIG, DBL_DIG, LDBL_DIG – number of decimal digits that can be represented without losing precision by float, double, long double, respectively * FLT_EPSILON, DBL_EPSILON, LDBL_EPSILONdifference between 1.0 and the next representable value of float, double, long double, respectively * FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG – number of FLT_RADIX-base digits in the floating-point significand for types float, double, long double, respectively * FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXP – minimum negative integer such that FLT_RADIX raised to a power one less than that number is a normalized float, double, long double, respectively * FLT_MIN_10_EXP, DBL_MIN_10_EXP, LDBL_MIN_10_EXP – minimum negative integer such that 10 raised to that power is a normalized float, double, long double, respectively * FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXP – maximum positive integer such that FLT_RADIX raised to a power one less than that number is a normalized float, double, long double, respectively * FLT_MAX_10_EXP, DBL_MAX_10_EXP, LDBL_MAX_10_EXP – maximum positive integer such that 10 raised to that power is a normalized float, double, long double, respectively * DECIMAL_DIG (C99) – minimum number of decimal digits such that any number of the widest supported floating-point type can be represented in decimal with a precision of DECIMAL_DIG digits and read back in the original floating-point type without changing its value. DECIMAL_DIG is at least 10.


Fixed-width integer types

The
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
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 header (cinttypes header in C++) and also are available at header (cstdint header in C++). The types can be grouped into the following categories: * Exact-width integer types that are guaranteed to have the same number ''n'' of bits across all implementations. Included only if it is available in the implementation. * Least-width integer types that are guaranteed to be the smallest type available in the implementation, that has at least specified number ''n'' of bits. Guaranteed to be specified for at least N=8,16,32,64. * Fastest integer types that are guaranteed to be the fastest integer type available in the implementation, that has at least specified number ''n'' of bits. Guaranteed to be specified for at least N=8,16,32,64. * Pointer integer types that are guaranteed to be able to hold a pointer. Included only if it is available in the implementation. * Maximum-width integer types that are guaranteed to be the largest integer type in the implementation. 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 header (cinttypes in C++) provides features that enhance the functionality of the types defined in the header. It defines macros for
printf format string The printf format string is a control parameter used by a class of functions in the input/output libraries of C and many other programming languages. The string is written in a simple template language: characters are usually copied literal ...
and
scanf format string A scanf format string (''scan f''ormatted) is a control parameter used in various functions to specify the layout of an input string. The functions can then divide the string and translate into values of appropriate data types. String scanning ...
specifiers corresponding to the types defined in and several functions for working with the intmax_t and uintmax_t types. This header was added in
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
. ;Printf format string The macros are in the format PRI'. Here ' defines the output formatting and is one of d (decimal), x (hexadecimal), o (octal), u (unsigned) and i (integer). ' defines the type of the argument and is one of ''n'', FAST''n'', LEAST''n'', PTR, MAX, where ''n'' corresponds to the number of bits in the argument. ;Scanf format string The macros are in the format SCN'. Here ' defines the output formatting and is one of d (decimal), x (hexadecimal), o (octal), u (unsigned) and i (integer). ' defines the type of the argument and is one of ''n'', FAST''n'', LEAST''n'', PTR, MAX, where ''n'' corresponds to the number of bits in the argument. ;Functions


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: * _FloatN for binary interchange formats; * _DecimalN for decimal interchange formats; * _FloatNx for binary extended formats; * _DecimalNx for decimal extended formats.


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 struct birthday which contains the name and birthday of a person. The structure definition is followed by a declaration of the variable John that allocates the needed storage. struct birthday ; 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 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
, a structure may also end with a
flexible array member C struct data types may end with a flexible array member with no specified size: struct vectord ; Typically, such structures serve as the header in a larger, variable memory allocation: struct vectord *vector = malloc(...); vector->len = ...
. A structure containing a pointer to a structure of its own type is commonly used to build
linked data structure In computer science, a linked data structure is a data structure which consists of a set of data records ('' nodes'') linked together and organized by references (''links'' or '' pointers''). The link between data can also be called a connector. I ...
s: struct node ;


Arrays

For every type T, except
void Void may refer to: Science, engineering, and technology * Void (astronomy), the spaces between galaxy filaments that contain no galaxies * Void (composites), a pore that remains unoccupied in a composite material * Void, synonym for vacuum, a s ...
and function types, there exist the types ''"array of N elements of type T"''. An array is a collection of values, all of the same type, stored contiguously in memory. An array of size N is indexed by integers from 0 up to and including ''N''−1. Here is a brief example: int cat 0 // 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 08]; // array of 10 elements, each of type 'array of 8 int elements'


Pointers

Every data type T has a corresponding type ''pointer to T''. A pointer (computer programming), 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 (*) 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 0 // array of 10 elements of 'pointer to char' char (*pa) 0 // pointer to a 10-element array of char The element pc requires ten blocks of memory of the size of ''pointer to char'' (usually 40 or 80 bytes on common platforms), but element pa 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 In computer science, a union is a value that may have any of several representations or formats within the same position in memory; that consists of a variable that may hold such a data structure. Some programming languages support special dat ...
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 u; The total size of u is the size of u.s – which happens to be the sum of the sizes of u.s.u and u.s.d – since s is larger than both i and f. When assigning something to u.i, some parts of u.f may be preserved if u.i is smaller than u.f. 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 pointer A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory. Dereferencing the function poi ...
s allow referencing functions with a particular signature. For example, to store the address of the standard function abs in the variable my_int_f: 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 pointer In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer ''refe ...
s.


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: const ( C89), volatile ( C89),
restrict In the C programming language, restrict is a keyword, introduced by the C99 standard, that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, no other po ...
(
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
) and _Atomic ( C11) the latter has a private name to avoid clashing with user names,C11:The New C Standard
Thomas Plum
but the more ordinary name atomic can be used if the header is included. Of these, const is by far the best-known and most used, appearing in the
standard library In computer programming, a standard library is the library made available across implementations of a programming language. These libraries are conventionally described in programming language specifications; however, contents of a language's ...
and encountered in any significant use of the C language, which must satisfy
const-correctness In some programming languages, const is a type qualifier (a keyword applied to a data type) that indicates that the data is read-only. While this can be used to declare constants, in the C family of languages differs from similar constructs i ...
. The other qualifiers are used for low-level programming, and while widely used there, are rarely used by typical programmers.


See also

*
C syntax C, or c, is the third letter in the Latin alphabet, used in the modern English alphabet, the alphabets of other western European languages and others worldwide. Its name in English is ''cee'' (pronounced ), plural ''cees''. History "C" ...
*
Uninitialized variable In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have ''some'' value, but not a predictable one. As such, it is a programming error and a common source of b ...
*
Integer (computer science) In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers ar ...


References

{{DEFAULTSORT:C Variable Types And Declarations C (programming language) C standard library Data types Articles with example C code