DEV Community

loading...

C Data Types

freethebit profile image Bruno Diniz Updated on ・4 min read

Simple reference on C data types.


TOC


Data Type Keywords

Also called Type specifiers : void, char, short, int, long, float, double, signed, unsigned, _Bool[C99], _Complex[C99], struct, union, enum, _Atomic (type name)[C11], typedef-name.

Groups of Types

  • Object types: all types that aren't function types.
  • Character types: char, signed char, unsigned char.
  • Integer types: char, signed integer types, unsigned integer types, enumerated types.
  • Real types: integer types and real floating types.
  • Arithmetic types: integer types and floating types.
  • Scalar types: arithmetic types and pointer types.
  • Structure types: struct and anonymous struct. union is not considered as structure type. Before C11, structure types is only struct.
  • Aggregate types: Array and Structure types. union is not, because it can contain only one member at a time.
  • Derived types: Array, struct, union, function, pointer, _Atomic (type name)[C11].
  • Built-in types: void, _Bool[C99], char, short, int, long, long long[C99], float, double, long double, enum, struct, union, array, function, pointer, _Atomic (type name)[C11].

Built-in Data Types

  • Void: void.
  • Character: char, signed char, unsigned char.
  • Signed Integers: signed char, short, int, long, long long[C99].
  • Unsigned Integers: _Bool[C99], unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long[C99].
  • Enumerated: enum.
  • Real Floating-point : float, double, long double.
  • Derived types: struct, union, array, function, pointer, _Atomic (type name)[C11].

Common C Standard Library Types (Macros or typedefs)

  • Character: wchar_t, char16_t[C11], char32_t[C11].
  • Signed Integers: ptrdiff_t.
  • Unsigned Integers: bool[C99], size_t.
  • Signed Integer able to hold a pointer [C99]: intptr_t.
  • Unsigned Integer able to hold a pointer [C99]: uintptr_t.
  • Fixed Width Signed Integers [C99]: int8_t, int16_t, int32_t, int64_t.
  • Fixed Width Fast Signed Integers [C99]: int_fast8_t, int_fast16_t, int_fast32_t, int_fast64_t.
  • Fixed Width Smallest Signed Integers [C99]: int_least8_t, int_least16_t, int_least32_t, int_least64_t.
  • Fixed Width Unsigned Integers [C99]: uint8_t, uint16_t, uint32_t, uint64_t.
  • Fixed Width Fast Unsigned Integers [C99]: uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t.
  • Fixed Width Smallest Unsigned Integers [C99]: uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t.
  • Complex Floating-point [C99]: float _Complex, double _Complex, long double _Complex.
  • Imaginary Floating-point [C99]: float _Imaginary, double _Imaginary, long double _Imaginary.
  • Atomic types [C99]: Not listed here because every implementation follows their own naming convention, just check stdatomic.h.

Choosing a Type

  • Is not a built-in type, it needs a lib. E.g.: No need to #include <bool.h> to use _Bool, but for bool yes.
  • How the target CPU reads/writes a chunk of memory at a time. Not because the type uses less bytes that is going to be faster. Related concepts: data alignment, CPU granularity, type promotion. On Intel x86 and x64, an operation on anint is faster than in a short, because short type has to be converted to int before the operation happen.
  • Target architecture type value limit. The number of bytes for a type may change in different architectures. E.g.: The standard defines INT_MAX with at least +32767. Intel implementation of x86 and x64, an int can hold values up to +2147483647, because is defined with 4 bytes. The standard define the minimum value to be implemented in <limits.h>, not the maximum. This is why its important to check the compiler and target limits.
  • The keyword _Atomic can be a type qualifier or type specifier depending on the context and how it is used.
  • Comparing different types can cause loss of precision, in some cases because of binary format:
    • char and integer use binary format (…, 8, 16, 32, 64, 128, … bits).
    • Floating-point is implementation specific, but most implementations follow:
      • float, 32 bits, IEEE 754 single-precision binary floating-point format: 1 bit is the sign, 8 bits are exponent, 23 bits are mantissa or fraction.
      • double, 64 bits, IEEE 745 double-precision binary floating-point format: 1 bit is the sign, 11 bits are exponent, 52 bits are mantissa or fraction.
      • long double, some implementations:
        • 80 bits, x86 extended-precision floating-point format: 1 bit is the sign, 15 bits are exponent, 63 bits are mantissa or fraction.
        • 128 bits, IEEE 754 quadruple-precision binary floating-point format: 1 bit is the sign, 15 bits are exponent, 112 bits are mantissa or fraction.

Notes

NOT types:

  • Storage-class specifiers: extern, static, _Thread_local[C11] , auto , register.
  • Type qualifiers: const, restrict[C99], volatile, _Atomic[C11].
  • There is no string type in C. An array of characters, where the last character is a null character '\0' is the concept of string in C.

References

Discussion (0)

pic
Editor guide