Signup/Sign In

Understanding C Datatypes

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.

In this tutorial, we have explained the Datatypes, for code examples, checkout Using C Datatypes tutorial.

C Datatypes

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

C Primary Data types:

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

  1. Character - ASCII character set or generally a single alphabet like 'a', 'B', etc.

  2. Integer - Used to store whole numbers like 1, 2, 100, 1000, etc.

  3. Floating-point - Decimal point or real numbers values like 99.9, 10.5, etc.

  4. Double - Very large numeric values which are not allowed in Integer or Floating point type.

  5. Void - This means no value. This data type is mostly used when we define functions.

There are different keywords to specify these data types, the keywords are:

Datatype Keyword
Character char
Integer int
Floating-point float
Double double
Void void

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 Datatypes

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 Datatype 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, the other person will know that there is a playground, but you can be more specific and say, there is a Cricket playground or a Football playground, which makes it even more clear for the other person.

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.

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

C Derived Datatypes:

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.


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.