Data types – C

All variables in C have data types associated with them. This describes the kind of data a variable can store and how much memory it will use.

The following table contains the base data types in C, memory usage (in bytes), range of values, format specifier (for formatted data – printf, scanf, etc.). In addition to those there is data type void, which has no type and a variable with that type cannot be declared (only functions – see Functions post). It is possible to find out how much memory a variable uses by using sizeof operator (see Operators).  More details on all of these below

 

Data type Size (in bytes) Value range Format specifier
char 1 -128 .. 127 %c or %hhi
unsigned char 1 0 .. 255 %c or %hhu
short 2 -(2^15) .. (2^15)-1 %hi
unsigned short 2 0 .. 65535 (2^16)-1 %hu
int 4 -(2^31) .. (2^31)-1 %i or %d
unsigned int 4 0 .. (2^32)-1 %u or %d
long 8 -(2^63) .. (2^63)-1 %li or %ld
unsigned long 8 0 .. (2^64)-1 %lu or %ld
long long 8 -(2^63) .. (2^63)-1 %lli or %lld
unsigned long long 8 0 .. (2^64)-1 %llu or %lld
float 4 8-bits exponent
23-bits fraction
%f or %g or %e or %a
double 8 11-bits exponent
52-bits fraction
%lf or %lg or %le or %la
long double 16 15-bits exponent
112-bits fraction
%Lf or %Lg or %Le or %La
void 1
int* 4 or 8 (depending on architecture) 0x0000000000000000 .. 0xffffffffffffffff (64-bit arch.) %p

In C language the data types could be categorized as follows:

  • Type void – typeless.
  • Basic data types
    • Integer types
      • char, short, int, long, long long
    • Floating types
      • Real types
        float, double, long double
      • Complex types  (C99)
        float _Complex, double _Complex, long double _Complex
        float complex, double complex, long double complex(When <complex.h> is included)
  • Enumerated
    • Enumeration (C99)
      •  enum
  • Derived data types
    • Arrays
      • e.g.  int array[10];
    • Pointers
      • e.g.  int *pointer;
    • Functions
      • e.g. int function(void);
    • Structures
      •  struct, union
    • Atomic (C11)
      • e.g. atomic_int

In addition to fore mentioned data types there are pointers to data types. The size of those variables depends on the architecture, which may be 32-bits or 64-bits. Example pointer variable : int *numbers;. Pointers can be of all the types in the table followed by an asterisk (*), even void pointers (meaning that it is unknown what sort of data will the address contain).

Some libraries include more data types which can be used in your programs. For example <stdint.h> adds support forsigned and unsigned integers of different sizes. Usually the naming convention goes as follows: (u)int[size_in_bytes]_t.

Example: 8-bit signed integer would be int8_t and 32-bit unsigned integer would be  uint32_t . The different sized variables include 8, 16, 32 and 64 bit integers. Usually these types are used in embedded systems to avoid confusion between the size of integer ( It could be 2 or 4 bits).

For usage of data types see Variables post.

Resources

  1. Data types [ Link ]
  2. stdint.h [ Link ]
  3. Type – cppreference [ Link ]

 

5 thoughts on “Data types – C

Comments are closed.