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 programming, 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 12100, or a decimal points number like 99.9510.5, or a text, like "Studytonight", all these values are handled differently by the C compiler, hence, we use data types to define the type of data used in any program.

  • Each data type occupies some memory.

  • Each datatype has its own valid range of values.

  • There are a set of operations that are allowed for every datatype.

  • For code examples, check out 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 Programming

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

Character (char):

  1. We use the keyword char for the character data type.

  2. It is used to store single-bit characters and occupies 1 byte of memory.

  3. You can store alphabets from A-Z(and a-z) and 0-9 digits using char datatype. For example,
     

    
    char b = 'A'; 
    char c = '0';
    char d = 0; // ERROR
    
  4. For char datatype, it is necessary to enclose the data within single quotes.

  5. You can perform addition and subtraction operations on char datatype values.

  6. The ASCII value of a char datatype value should not exceed 127.

Integer (int):

  1. We use the keyword int for the integer data type.

  2. The int data type is used to store non-fractional numbers which include positive, negative, and zero values.

  3. 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
  4. We can perform addition, subtraction, division, multiplication, bitwise, and modulo operations on int data type.

Floating-point (float):

  1. We use the keyword float for a floating-point data type.

  2. The keyword float is used to store decimal numbers.

  3. It occupies 4 bytes of memory and ranges from 1e-37 to 1e+37.

  4. For example,
    float a = 0.05; 
    float b = -0.005.
    float c = 1;  // it will become c = 1.000000 because of type-casting
  5. We can perform addition, subtraction, division, and multiplication operations on float data type.

 

Double (double):

  1.  We use the keyword double for the double data type.

  2. The double datatype is used to store decimal numbers.

  3. It occupies 8 bytes of memory and ranges from 1e-37 to 1e+37.

  4. Here is how we use it in code,
     

    double a = 10.09;
    double b = -67.9;
  5. The double datatype has more precision than float so double gives more accurate results as compared to float.

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

Void (void):

  1.  This means no value.

  2. This data type is mostly used when we define functions.

  3. The void datatype is used when a function does not return any result.

  4. It occupies 0 bytes of memory.

  5. We use the void keyword for void data type.

  6. Here is how we use the void type with functions,

    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 datatypes can hold.

Size of different Datatypes in C

  • The size of 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.

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

2. int can be 2 bytes/4 bytes

  • There is a very easy way to remember the size of 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.

3. 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 compared to double, because double data type works with very large values, hence it is slow.

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

5. void is 0 bytes

The void datatype 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:

There are 4 datatype modifiers in C programming 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 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.

The 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 datatype, 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 111111111 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 the rest represents 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.