Data Types

C language is a strictly typed language. So the variables we declare in C language must be of a particular type always. In C language, we have 3 fundamental data types called int, float, and char.

Data Type: int

Using data type int we can declare variables of type integers.

int a = 10;

We have short and long variants of int data type. These two variants allow us to have less or more range of integer values.

short int b = 20;
long int c = 30;
long long int d = 40;

We can also write them in a short form, like short int as short, long int as long, and long long int as long long.

short b = 20;
long c = 30;
long long d = 40;

By default, the variables declared using int are signed integers. You do not have to mention signed keyword while declaring a variable. A signed integer can store both negative and positive values.

C also provides us another keyword unsigned which allows only positive numbers. Not only that, we can store double the number of positive numbers than signed int.

For example, the range of signed int, on a 16-bit compiler on a 16-bit OS may show -32768 to +32767. However, the range of unsigned int will be 0 to 65535.

So, if you are sure that you will not have negative integer value in a variable, you can better declare that variable as unsigned.

Each of these variants occupy certain memory size and allows a certain range of values, based on the compiler and the OS. Below are the minimum requirements defined by the open standards.

Data Type Min/MaxValue
short minimum-32767
short maximum32767
unsigned maximum65535
int minimum-32767
int maximum32767
unsigned int maximum65535
long minimum-2147483647
long maximum2147483647
unsigned long maximum4294967295
long long minimum-9223372036854775807
long long maximum9223372036854775807
unsigned long long maximum18446744073709551615
Data TypeMinimum Size
short16 bits
int16 bits
long32 bits
long long64 bits

When in doubt you can print the sizes and ranges of int for your compiler and OS:

#include <stdio.h>
#include <limits.h>

void main()
{
    printf("Data Type \t Size in Bytes\n");
    printf("short \t\t %d\n", sizeof(short));
    printf("int \t\t %d\n", sizeof(int));
    printf("long \t\t %d\n", sizeof(long));
    printf("long long \t %d\n\n", sizeof(long long));

    printf("Data Type \t Min \t\t\t Max\n");
    printf("short \t\t %d \t\t %d\n", SHRT_MIN, SHRT_MAX);
    printf("int \t\t %d \t\t %d\n", INT_MIN, INT_MAX);
    printf("long \t\t %ld \t\t %ld\n", LONG_MIN, LONG_MAX);
    printf("long long \t %lld \t %lld\n", LLONG_MIN, LLONG_MAX);
}

Data Type: float

You can declare a variable using float to store fractional numbers.

float price = 299.99;
printf("%f", price);

A float type variable will be of size 4 bytes. The range of values that we can store in a float variable are from -3.4e38 to +3.4e38.

To print a float variable value, we use %f format specifier.

Data Type: double

Similar to float, C offers double data type which allows you to store more values than float.

A double type variable occupies 8 bytes in memory and the range of values that we can store in a double variable are starting from -1.7e308 to +1.7e308. To print a double type variable we use %ld format specifier within printf function.

double totalAmount = 999.99
printf("%ld", totalAmount);

If the range of values provided by double is not enough, you can use long double, which occupies 10 bytes and the range of values are from -1.7e4932 to +1.7e4932. We can use %Ld to print long double variable value using printf function.

long double totalAmount = 999.99
printf("%Ld", totalAmount);

Data Type: char

Using char data type, you can declare variables that can store a single character.

char letter = 'a';

To initialize a character variable, you enclose the character with a single quote as shown above. To print the value of a character we use %c format specifier.

C open standards defined the minimum size in memory for a char to be of 1 byte.

Data type char can also be signed and unsigned type. A signed char can hold values from -128 to +127. An unsigned char can store 0 to 255.

You may wonder why there are number ranges for a char type. You may know that your machine designate an ASCII value for each character. For example, ASCII value for ‘A’ is 65, whereas for ‘a’ it is 97.

You can print the ASCII value of any character as follows:

char c1 = 'A';
printf("%d", c1);

Similarly, you can also print character of a given ASCII value as follows:

int n1 = 97;
printf("%c", n1);