Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Primary types  



1.1  Main types  





1.2  Boolean type  





1.3  Bit-precise integer types  





1.4  Size and pointer difference types  





1.5  Interface to the properties of the basic types  



1.5.1  Properties of integer types  





1.5.2  Properties of floating-point types  









2 Fixed-width integer types  



2.1  Printf and scanf format specifiers  



2.1.1  Printf format string  





2.1.2  Scanf format string  





2.1.3  Functions  









3 Additional floating-point types  





4 Structures  





5 Arrays  





6 Pointers  





7 Unions  





8 Function pointers  





9 Type qualifiers  





10 See also  





11 References  














C data types






العربية
Català
Deutsch
Français

Русский
Українська
Tiếng Vit

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 

(Redirected from Limits.h)

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 locationsorvariables. 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]

Primary types[edit]

Main types[edit]

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][a] 8 %c[b] [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[c] [0, UCHAR_MAX]
  • short
  • short int
  • signed short
  • signed short int
  • Short signed integer type. Capable of containing at least the [−32767, +32767] range.[3][a] 16 %hior%hd [SHRT_MIN, SHRT_MAX]
    • unsigned short
  • unsigned short int
  • Short unsigned integer type. Contains at least the [0, 65535] range.[3] 16 %hu [0, USHRT_MAX]
    • int
  • signed
  • signed int
  • Basic signed integer type. Capable of containing at least the [−32767, +32767] range.[3][a] 16 %ior%d [INT_MIN, INT_MAX] none[8]
    • unsigned
  • unsigned int
  • Basic unsigned integer type. Contains at least the [0, 65535] range.[3] 16 %u [0, UINT_MAX] uorU[8]
    • long
  • long int
  • signed long
  • signed long int
  • Long signed integer type. Capable of containing at least the [−2147483647, +2147483647] range.[3][a] 32 %lior%ld [LONG_MIN, LONG_MAX] lorL[8]
    • unsigned long
  • unsigned long int
  • Long unsigned integer type. Capable of containing at least the [0, 4294967295] range.[3] 32 %lu [0, ULONG_MAX] both uorU and lorL[8]
    • long long
  • long long int
  • signed long long
  • signed long long int
  • Long long signed integer type. Capable of containing at least the [−9223372036854775807, +9223372036854775807] range.[3][a] Specified since the C99 version of the standard. 64 %llior%lld [LLONG_MIN, LLONG_MAX] llorLL[8]
    • unsigned long long
  • unsigned long long int
  • 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 uorU and llorLL[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:[d]
    • %f %F
  • %g %G
  • %e %E
  • %a %A
  • forF
    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".
    • %lf %lF
  • %lg %lG
  • %le %lE
  • %la %lA[e]
  • 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[e]
    lorL
    1. ^ a b c d e The minimal ranges [−(2n−1−1), 2n−1−1] (e.g. [−127,127]) come from the various integer representations allowed by the standard (ones' complement, sign-magnitude, two's complement).[4] However, most platforms use two's complement, implying a range of the form [−2m−1, 2m−1−1] with mn for these implementations, e.g. [−128,127] (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]
  • ^ or%hhi for numerical output
  • ^ or%hhu for numerical output
  • ^ These format strings also exist for formatting to text, but operate on a double.
  • ^ a b Uppercase differs from lowercase in the output. Uppercase specifiers produce values in the uppercase, and lowercase in lower (%A, %E, %F, %G produce such values as INF, NAN and E (exponent) in uppercase)
  • 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 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.

    Boolean type[edit]

    C99 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 overflows in implicit narrowing conversions. For example, in the following code:

    unsigned char b = 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:

    _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) {
     /* this code will run */
    }
    

    Bit-precise integer types[edit]

    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) (orsigned _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]

    Size and pointer difference types[edit]

    The C language specification includes the typedefssize_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.

    Interface to the properties of the basic types[edit]

    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.

    Properties of integer types[edit]

    Properties of floating-point types[edit]

    Fixed-width integer types[edit]

    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

    Printf and scanf format specifiers[edit]

    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.

    Printf format string[edit]

    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.

    Scanf format string[edit]

    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.

    Functions[edit]

    Additional floating-point types[edit]

    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[edit]

    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 {
     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[edit]

    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:

    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[edit]

    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).

    Unions[edit]

    Aunion 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 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[edit]

    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.

    Type qualifiers[edit]

    The aforementioned types can be characterized further by type qualifiers, yielding a qualified type. As of 2014 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]

    See also[edit]

    References[edit]

    1. ^ Barr, Michael (2 December 2007). "Portable Fixed-Width Integers in C". Retrieved 18 January 2016.
  • ^ a b ISO/IEC 9899:1999 specification, TC3 (PDF). p. 255, § 7.18 Integer types <stdint.h>.
  • ^ a b c d e f g h i j ISO/IEC 9899:1999 specification, TC3 (PDF). p. 22, § 5.2.4.2.1 Sizes of integer types <limits.h>.
  • ^ Rationale for International Standard—Programming Languages—C Revision 5.10 (PDF). p. 25, § 5.2.4.2.1 Sizes of integer types <limits.h>.
  • ^ ISO/IEC 9899:2023 specification draft (PDF). p. 41, § 6.2.6 Representations of types.
  • ^ "C and C++ Integer Limits". 21 July 2023.
  • ^ a b ISO/IEC 9899:1999 specification, TC3 (PDF). p. 37, § 6.2.6.1 Representations of types – General.
  • ^ a b c d e f ISO/IEC 9899:1999 specification, TC3 (PDF). p. 56, § 6.4.4.1 Integer constants.
  • ^ "64-Bit Programming Models: Why LP64?". The Open Group. Retrieved 9 November 2011.
  • ^ "Width of Type (The GNU C Library)". www.gnu.org. Retrieved 30 July 2022.
  • ^ "<limits.h>". pubs.opengroup.org. Retrieved 30 July 2022.
  • ^ ISO/IEC 9899:1999 specification, TC3 (PDF). p. 67, § 6.5 Expressions.
  • ^ ISO/IEC 9899:2023 specification draft (PDF). p. 37, § 6.2.5 Types.
  • ^ C11:The New C Standard, Thomas Plum

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=C_data_types&oldid=1230529332#limits.h"

    Categories: 
    C (programming language)
    C standard library
    Data types
    Hidden categories: 
    Articles with short description
    Short description is different from Wikidata
    Use dmy dates from July 2021
    Articles to be expanded from October 2011
    All articles to be expanded
    Articles using small message boxes
    Articles containing potentially dated statements from 2014
    All articles containing potentially dated statements
    All articles with unsourced statements
    Articles with unsourced statements from September 2015
    Articles with example C code
     



    This page was last edited on 23 June 2024, at 07:31 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki