Signup/Sign In

Data Types in C Language

As the name suggests, a Datatype defines the type of data being used. Whenever we define a variable or use any data in the C language program, we have to specify the type of the data, so that the compiler knows what type of data to expect.

For example, you may want to use a number like 1, 2, 100, or a decimal point number like 99.95, 10.5, or a text, like "Studytonight", all these values are handled differently by the C language compiler, hence, we use data types to define the type of data used in any program.

Each data type occupies some memory, has a range of values and a set of operations it allows to be performed on itself. In this tutorial, we have explained the different data types used in C language. For code examples, checkout Using C Datatypes tutorial.

Data Types in C

Broadly, there are 5 different categories of data types in the C language, they are:

Type Example
Basic character, integer, floating-point, double.
Derived Array, structure, union, etc.
Enumeration enums
Bool type true or false
void Empty value

C datatype categories

Primary Data types in C

The C language has 5 basic (primary or primitive) data types, they are:

  1. Character: We use the keyword char for character data type. It is used to store single bit characters and occupies 1 byte of memory. We can store alphabets from A-Z(and a-z) and 0-9 digits using char. For example,

    char a = 'a'; 
    char b = 'A'; 
    char c = '0';
    char d = 0; //error 

    For char datatype, it is necessary to enclose our data in single quotes. We can perform addition and subtraction operations on char but the ASCII value should not exceed 127.

  2. Integer: We use the keyword int for integer data type. The int data type is used to store non-fractional numbers which includes positive, negative and zero values. The range of int is -2,147,483,648 to 2,147,483,647 and it occupies 2 or 4 bytes of memory, depending on the system you’re using. For example,

    int a = 5550; 
    int b = -90, 
    int c = 0; 
    int d = -0.5; //invalid

    We can perform addition, subtraction, division, multiplication, bitwise and modulo operations on int data type.

  3. Floating-point: We use the keyword float for floating-point data type. float is used to store decimal numbers. It occupies 4 bytes of memory and ranges from 1e-37 to 1e+37. For example,

    float a = 0.05; 
    float b = -0.005.
    float c = 1;  // it will become c = 1.000000 because of type-casting
    

    We can perform addition, subtraction, division, and multiplication operations on float data type.

  4. Double: We use the keyword double for double data type. double is used to store decimal numbers. It occupies 8 bytes of memory and ranges from 1e-37 to 1e+37.

    double a = 10.09;
    double b = -67.9;

    double has more precision than flaot so double gives more accurate results as compared to float. We can perform addition, subtraction, division and multiplication operations on double data type.

  5. Void: This means no value. This data type is mostly used when we define functions. The void data type is used when a function does not return anything. It occupies 0 bytes of memory. We use the void keyword for void data type.

    void function() {
       //your code goes here
    }

primary datatypes in c

Each data type has a size defined in bits/bytes and has a range for the values that these data types can hold.

Size of different Data types in C

The size for different data types depends on the compiler and processor types, in short, it depends on the Computer on which you are running the C language and the version of the C compiler that you have installed.

char is 1 byte

The char datatype is 1 byte in size or 8 bits. This is mostly the same and is not affected by the processor or the compiler used.

int can be 2 bytes/4 bytes

There is a very easy way to remember the size for int datatype. The size of int datatype is usually equal to the word length of the execution environment of the program. In simpler words, for a 16-bit environment, int is 16 bits or 2 bytes, and for a 32-bit environment, int is 32 bits or 4 bytes.

float is 4 bytes

The float datatype is 4 bytes or 32 bits in size. It is a single-precision data type that is used to hold decimal values. It is used for storing large values.

float is a faster data type as compared to double, because double data type works with very large values, hence it is slow.

double is 8 bytes

The double datatype is 8 bytes or 64 bits in size. It can store values that are double the size of what a float data type can store, hence it is called double.

In the 64 bits, 1 bit is for sign representation, 11 bits for the exponent, and the rest 52 bits are used for the mantissa.

The double data type can hold approximately 15 to 17 digits, before the decimal and after the decimal.

void is 0 bytes

The void data type means nothing, hence it doesn't have a size.

Good Read: Understand what are Bits and Bytes

Before moving on to the range of values for these data types, there is one more important concept to learn, which is Datatype modifiers.

C Data type Modifiers:

In the C language, there are 4 datatype modifiers, that are used along with the basic data types to categorize them further.

For example, if you say, there is a playground, it can be a park, a playground or a stadium, but if you be more specific and say, there is a Cricket ground or a Football stadium, that would make it even more precise.

Similarly, there are modifiers in the C language, to make the primary data types more specific.

Following are the modifiers:

  1. signed

  2. unsigned

  3. long

  4. short

As the name suggests, signed and unsigned are used to represent the signed(+ and -) and unsigned(only +) values for any data type. And long and short affects the range of the values for any datatype.

For example, signed int, unsigned int, short int, long int, etc. are all valid data types in the C language.

long long num = 123456789987654321; // we cannot store a value this big value using int data type.

Now let's see the range for different data types formed as a result of the 5 primary data types along with the modifiers specified above.

C Data type Value Range

In the table below we have the range for different data types in the C language.

Type Typical Size in Bits Minimal Range Format Specifier
char 8 -127 to 127 %c
unsigned char 8 0 to 255 %c
signed char 8 -127 to 127 %c
int 16 or 32 -32,767 to 32,767 %d, %i
unsigned int 16 or 32 0 to 65,535 %u
signed int 16 or 32 Same as int %d, %i
short int 16 -32,767 to 32,767 %hd
unsigned short int 16 0 to 65,535 %hu
signed short int 16 Same as short int %hd
long int 32 -2,147,483,647 to 2,147,483,647 %ld, %li
long long int 64 -(263 - 1) to 263 - 1 (Added by C99 standard) %lld, %lli
signed long int 32 Same as long int %ld, %li
unsigned long int 32 0 to 4,294,967,295 %lu
unsigned long long int 64 264 - 1 (Added by C99 standard) %llu
float 32 1E-37 to 1E+37 with six digits of precision %f
double 64 1E-37 to 1E+37 with ten digits of precision %lf
long double 80 1E-37 to 1E+37 with ten digits of precision %Lf

As you can see in the table above, with different combinations of the datatype and modifiers the range of value changes.

When we want to print the value for any variable with any data type, we have to use a format specifier in the printf() statement.

What happens if the value is out of Range?

Well, if you try to assign a value to any datatype which is more than the allowed range of value, then the C language compiler will give an error. Here is a simple code example to show this,

#include <stdio.h>

int main() {
   // allowed value up to 65535
   unsigned short int x = 65536;
  
   return 0;
}


warning: large integer implicitly truncated to unsigned type [-Woverflow]
unsigned short int x = 65536;
^

Run Code →

When a type modifier is used without any data type, then the int data type is set as the default data type. So, unsigned means unsigned int, signed means signed int, long means long int, and short means short int.

What does signed and unsigned means?

This is a little tricky to explain, but let's try.

In simple words, the unsigned modifier means all positive values, while the signed modifier means both positive and negative values.

When the compiler gets a numeric value, it converts that value into a binary number, which means a combination of 0 and 1. For example, 32767 in binary is 01111111 11111111, and 1 in binary is 01 (or 0001), 2 is 0010, and so on.

In the case of a signed integer, the highest order bit or the first digit from left (in binary) is used as the sign flag. If the sign flag is 0, the number is positive, and if it is 1, the number is negative.

And because one bit is used for showing if the number is positive or negative, hence there is one less bit to represent the number itself, hence the range is less.

For signed int, 11111111 11111111 means -32,767 and because the first bit is a sign flag to mark it as a negative number, and rest represent the number. Whereas in the case of unsigned int, 11111111 11111111 means 65,535.

Derived Data types in C

While there are 5 primary data types, there are some derived data types too in the C language which are used to store complex data.

Derived data types are nothing but primary data types but a little twisted or grouped together like an array, structure, union, and pointers. These are discussed in detail later.

Conclusion:

In the next tutorial, we will learn about variables and there you will learn the actual usage of data types, with man code examples. So let's move on.