C standard library (libc) |
---|
General topics |
Miscellaneous headers |
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. [1] [2]
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.
Type | Explanation | Minimum size (bits) | Format specifier | Range | Suffix for decimal constants |
---|---|---|---|---|---|
char | Smallest addressable unit of the machine that can contain basic character set. It is an integer type. Actual type can be either signed or unsigned. It contains CHAR_BIT bits. [3] | 8 | %c | [CHAR_MIN, CHAR_MAX] | — |
signed char | Of the same size as char, but guaranteed to be signed. Capable of containing at least the [−127, +127] range. [3] [lower-alpha 1] | 8 | %c [lower-alpha 2] | [SCHAR_MIN, SCHAR_MAX] [6] | — |
unsigned char | Of the same size as char, but guaranteed to be unsigned. Contains at least the [0, 255] range. [7] | 8 | %c [lower-alpha 3] | [0, UCHAR_MAX] | — |
| Short signed integer type. Capable of containing at least the [−32767, +32767] range. [3] [lower-alpha 1] | 16 | %hi or %hd | [SHRT_MIN, SHRT_MAX] | — |
| Short unsigned integer type. Contains at least the [0, 65535] range. [3] | 16 | %hu | [0, USHRT_MAX] | — |
| Basic signed integer type. Capable of containing at least the [−32767, +32767] range. [3] [lower-alpha 1] | 16 | %i or %d | [INT_MIN, INT_MAX] | none [8] |
| Basic unsigned integer type. Contains at least the [0, 65535] range. [3] | 16 | %u | [0, UINT_MAX] | u or U [8] |
| Long signed integer type. Capable of containing at least the [−2147483647, +2147483647] range. [3] [lower-alpha 1] | 32 | %li or %ld | [LONG_MIN, LONG_MAX] | l or L [8] |
| Long unsigned integer type. Capable of containing at least the [0, 4294967295] range. [3] | 32 | %lu | [0, ULONG_MAX] | both u or U and l or L [8] |
| Long long signed integer type. Capable of containing at least the [−9223372036854775807, +9223372036854775807] range. [3] [lower-alpha 1] Specified since the C99 version of the standard. | 64 | %lli or %lld | [LLONG_MIN, LLONG_MAX] | ll or LL [8] |
| Long long unsigned integer type. Contains at least the [0, 18446744073709551615] range. [3] Specified since the C99 version of the standard. | 64 | %llu | [0, ULLONG_MAX] | both u or U and ll or LL [8] |
float | Real floating-point type, usually referred to as a single-precision floating-point type. Actual properties unspecified (except minimum limits); however, on most systems, this is the IEEE 754 single-precision binary floating-point format (32 bits). This format is required by the optional Annex F "IEC 60559 floating-point arithmetic". | Converting from text: [lower-alpha 4]
| f or F | ||
double | Real floating-point type, usually referred to as a double-precision floating-point type. Actual properties unspecified (except minimum limits); however, on most systems, this is the IEEE 754 double-precision binary floating-point format (64 bits). This format is required by the optional Annex F "IEC 60559 floating-point arithmetic". |
| none | ||
long double | Real floating-point type, usually mapped to an extended precision floating-point number format. Actual properties unspecified. It can be either x86 extended-precision floating-point format (80 bits, but typically 96 bits or 128 bits in memory with padding bytes), the non-IEEE "double-double" (128 bits), IEEE 754 quadruple-precision floating-point format (128 bits), or the same as double. See the article on long double for details. | %Lf %LF %Lg %LG %Le %LE %La %LA [lower-alpha 5] | l or L |
SCHAR_MIN == −128
and SCHAR_MAX == 127
) for an 8-bit signed char. Since C23, the only representation allowed is two's complement, therefore the values range from at least [−2n−1, 2n−1−1]. [5] %hhi
for numerical output%hhu
for numerical outputThe 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 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. [9]
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 4 Unix, Microsoft MS-DOS, modern Linux, and Microchip MCC18 for embedded 8-bit PIC microcontrollers. POSIX requires char to be exactly 8 bits in size. [10] [11]
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, [7] and the possibility of aliasing. [12]
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 binary floating-point formats are used for float and double respectively.
The C99 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 types: float _Complex
, double _Complex
, long double _Complex
. C11 added imaginary types (which were described in an informative annex of C99): float _Imaginary
, double _Imaginary
, long double _Imaginary
. Including the header <complex.h>
allows all these types to be accessed with using complex and imaginary respectively.
C99 added a Boolean data 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 overflows in implicit narrowing conversions. For example, in the following code:
unsignedcharb=256;if(b){/* do something */}
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:
_Boolb=256;if(b){/* do something */}
The type _Bool also ensures true values always compare equal to each other:
_Boola=1,b=2;if(a==b){/* this code will run */}
Since C23, the language allows the programmer to define integers that have a width of an arbitrary number of bits. Those types are specified as _BitInt(N)
, where N is an integer constant expression that denotes the number of bits, including the sign bit for signed types, represented in two's complement. The maximum value of N is provided by BITINT_MAXWIDTH
and is at least ULLONG_WIDTH
. Therefore, the type _BitInt(2)
(or signed_BitInt(2)
) takes values from −2 to 1 while unsigned_BitInt(2)
takes values from 0 to 3. The type unsigned_BitInt(1)
also exists, being either 0 or 1 and has no equivalent signed type. [13]
The C language specification includes the typedef 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 <stddef.h>
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 sizeof 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.
Information about the actual properties, such as size, of the basic arithmetic types, is provided via macro constants in two headers: <limits.h>
header (climits
header in C++) defines macros for integer types and <float.h>
header (cfloat
header in C++) defines macros for floating-point types. The actual values depend on the implementation.
CHAR_BIT
– size of the char type in bits, commonly referred to as the size of a byte (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 longSCHAR_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 longUCHAR_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 longCHAR_MIN
– minimum possible value of charCHAR_MAX
– maximum possible value of charMB_LEN_MAX
– maximum number of bytes in a multibyte characterBOOL_WIDTH
(C23) - bit width of _Bool
, always 1CHAR_WIDTH
(C23) - bit width of char
; CHAR_WIDTH
, UCHAR_WIDTH
and SCHAR_WIDTH
are equal to CHAR_BIT
by definitionSCHAR_WIDTH
, SHRT_WIDTH
, INT_WIDTH
, LONG_WIDTH
, LLONG_WIDTH
(C23) - bit width of signed char
, short
, int
, long
, and long long
respectivelyUCHAR_WIDTH
, USHRT_WIDTH
, UINT_WIDTH
, ULONG_WIDTH
, ULLONG_WIDTH
(C23) - bit width of unsigned char
, unsigned short
, unsigned int
, unsigned long
, and unsigned long long
respectivelyFLT_MIN
, DBL_MIN
, LDBL_MIN
– minimum normalized positive value of float, double, long double respectivelyFLT_TRUE_MIN
, DBL_TRUE_MIN
, LDBL_TRUE_MIN
(C11) – minimum positive value of float, double, long double respectivelyFLT_MAX
, DBL_MAX
, LDBL_MAX
– maximum finite value of float, double, long double, respectivelyFLT_ROUNDS
– rounding mode for floating-point operationsFLT_EVAL_METHOD
(C99) – evaluation method of expressions involving different floating-point typesFLT_RADIX
– radix of the exponent in the floating-point typesFLT_DIG
, DBL_DIG
, LDBL_DIG
– number of decimal digits that can be represented without losing precision by float, double, long double, respectivelyFLT_EPSILON
, DBL_EPSILON
, LDBL_EPSILON
– difference between 1.0 and the next representable value of float, double, long double, respectivelyFLT_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, respectivelyFLT_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, respectivelyFLT_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, respectivelyFLT_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, respectivelyFLT_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, respectivelyDECIMAL_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.The C99 standard includes definitions of several new integer types to enhance the portability of programs. [2] 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 <inttypes.h>
header (cinttypes
header in C++) and also are available at <stdint.h>
header (cstdint
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):
Type category | Signed types | Unsigned types | ||||
---|---|---|---|---|---|---|
Type | Minimum value | Maximum value | Type | Minimum value | Maximum value | |
Exact width | intn_t | INTn_MIN | INTn_MAX | uintn_t | 0 | UINTn_MAX |
Least width | int_leastn_t | INT_LEASTn_MIN | INT_LEASTn_MAX | uint_leastn_t | 0 | UINT_LEASTn_MAX |
Fastest | int_fastn_t | INT_FASTn_MIN | INT_FASTn_MAX | uint_fastn_t | 0 | UINT_FASTn_MAX |
Pointer | intptr_t | INTPTR_MIN | INTPTR_MAX | uintptr_t | 0 | UINTPTR_MAX |
Maximum width | intmax_t | INTMAX_MIN | INTMAX_MAX | uintmax_t | 0 | UINTMAX_MAX |
The <inttypes.h>
header (cinttypes
in C++) provides features that enhance the functionality of the types defined in the <stdint.h>
header. It defines macros for printf format string and scanf format string specifiers corresponding to the types defined in <stdint.h>
and several functions for working with the intmax_t
and uintmax_t
types. This header was added in C99.
The macros are in the format PRI{fmt}{type}
. Here {fmt} defines the output formatting and is one of d
(decimal), x
(hexadecimal), o
(octal), u
(unsigned) and i
(integer). {type} defines the type of the argument and is one of n
, FASTn
, LEASTn
, PTR
, MAX
, where n
corresponds to the number of bits in the argument.
The macros are in the format SCN{fmt}{type}
. Here {fmt} defines the output formatting and is one of d
(decimal), x
(hexadecimal), o
(octal), u
(unsigned) and i
(integer). {type} defines the type of the argument and is one of n
, FASTn
, LEASTn
, PTR
, MAX
, where n
corresponds to the number of bits in the argument.
This section needs expansion. You can help by adding to it. (October 2011) |
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 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.
structbirthday{charname[20];intday;intmonth;intyear;};structbirthdayJohn;
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:
structnode{intval;structnode*next;};
For every type T
, except void 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:
intcat[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:
inta[10][8];// array of 10 elements, each of type 'array of 8 int elements'
Every data type T
has a corresponding type pointer to T
. 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 (*
) 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 char ** and int ***, 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 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 (sizeof*pa==10
).
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{inti;floatf;struct{unsignedintu;doubled;}s;}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 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 pointers.
The aforementioned types can be characterized further by type qualifiers, yielding a qualified type. As of 2014 [update] and C11, there are four type qualifiers in standard C: const
(C89), volatile
(C89), restrict
(C99) and _Atomic
(C11) – the latter has a private name to avoid clashing with user names, [14] but the more ordinary name atomic
can be used if the <stdatomic.h>
header is included. Of these, const
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.[ citation needed ]
C is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems code, device drivers, and protocol stacks, but its use in application software has been decreasing. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems.
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 are commonly represented in a computer as a group of binary digits (bits). The size of the grouping varies so the set of integer sizes available varies between different types of computers. Computer hardware nearly always provides a way to represent a processor register or memory address as an integer.
In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled programs must use. Most processors support a similar set of primitive data types, although the specific representations vary. More generally, "primitive data types" may refer to the standard data types built into a programming language. Data types which are not primitive are referred to as derived or composite.
The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.
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 references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.
In computer science, a union is a value that may have any of multiple representations or formats within the same area of memory; that consists of a variable that may hold such a data structure. Some programming languages support a union type for such a data type. In other words, a union type specifies the permitted types that may be stored in its instances, e.g., float
and integer
. In contrast with a record, which could be defined to contain both a float and an integer; a union would hold only one at a time.
C99 is a past version of the C programming language open standard. It extends the previous version (C90) with new features for the language and the standard library, and helps implementations make better use of available computer hardware, such as IEEE 754-1985 floating-point arithmetic, and compiler technology. The C11 version of the C programming language standard, published in 2011, updates C99.
IEC 61131-3 is the third part of the international standard IEC 61131 for programmable logic controllers. It was first published in December 1993 by the IEC; the current (third) edition was published in February 2013.
The computer programming languages C and Pascal have similar times of origin, influences, and purposes. Both were used to design their own compilers early in their lifetimes. The original Pascal definition appeared in 1969 and a first compiler in 1970. The first version of C appeared in 1972.
A bit field is a data structure that maps to one or more adjacent bits which have been allocated for specific purposes, so that any single bit or group of bits within the structure can be set or inspected. A bit field is most commonly used to represent integral types of known, fixed bit-width, such as single-bit Booleans.
scanf, short for scan formatted, is a C standard library function that reads and parses text from standard input.
A class in C++ is a user-defined type or data structure declared with any of the keywords class
, struct
or union
that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class declared with the keyword class
is private. The private members are not accessible outside the class; they can be accessed only through member functions of the class. The public members form an interface to the class and are accessible outside the class.
sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of char-sized units. Consequently, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t.
The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages are often integrated into a single product, with the programmer able to specify C or C++ as their source language.
C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.
In computer science, a type punning is any programming technique that subverts or circumvents the type system of a programming language in order to achieve an effect that would be difficult or impossible to achieve within the bounds of the formal language.
In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at runtime, instead of at compile time. In the language C, the VLA is said to have a variably modified data type that depends on a value.
This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.
Database Management Library (DBL) is a relational database management system (RDBMS) contained in a C++ programming library. The DBL source code is available under the terms of the GNU General Public License.
MessagePack is a computer data interchange format. It is a binary form for representing simple data structures like arrays and associative arrays. MessagePack aims to be as compact and simple as possible. The official implementation is available in a variety of languages, some official libraries and others community created, such as C, C++, C#, D, Erlang, Go, Haskell, Java, JavaScript (NodeJS), Lua, OCaml, Perl, PHP, Python, Ruby, Rust, Scala, Smalltalk, and Swift.