C Data Types

C is a typed language. That means, each variable is given a specific type which defines what values it can represent, how its data is stored in memory, and what operations can be performed on it.

So, a type of a variable is a formal description of what kind of data its value is. For instance,0 is an integer, 3.142 is a floating-point (decimal) number, and "Hello, world!\n" is a string value (a sequence of characters).

By forcing the programmer to explicitly define a type for all variables and interfaces, the type system enables the compiler to catch type-mismatch errors, thereby preventing a significant source of bugs.

 

Fundamental Data Types

Fundamental data types are basic types implemented directly by the language that represent the basic storage units supported natively by most systems. They can mainly be classified into:

  • Character types: They can represent a single character, such as 'A' or '$'. The most basic type is char, which is a one-byte character. Other types are also provided for wider characters.
  • Numerical integer types: They can store a whole number value, such as 7 or 1024. They exist in a variety of sizes, and can either be signed or unsigned, depending on whether they support negative values or not.
  • Floating-point types: They can represent real values, such as 3.14 or 0.01, with different levels of precision, depending on which of the three floating-point types is used.

The following table lists the data types and their storage sizes.

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Table : C data types and their sizes

Note I: Nearly all current machines represent an int with at least 32-bits and many now use 64-bits. The size of an int generally represents the natural word-size of a machine; the native size with which the CPU handles instructions and data.

We can use sizeof operator, returns an unsigned integer,  to find the size of a type in number of characters i.e. number of bytes. Do not be cofused that sizeof operator is not a function, although it often appears like one. It is a keyword.

Here is example program to find the size for different types:

/* program to print the size of various types in “number-of-bytes" */
#include <stdio.h>

int main ()
{
    printf("Size for different types:\n");
    printf("void \t char \t short \t int \t long \t float \t double\n");
    printf("%d \t %d \t %d \t %d \t %d \t %d \t %d\n",
           sizeof(void), sizeof(char), sizeof(short), sizeof(int),
           sizeof(long), sizeof(float), sizeof(double));
}

The ouput of the program is:

data types sizes

We have run the code in 64-bit system.

C Operators
C Variables and Expression