C Data Types

A data type tells the compiler what kind of value a variable will store, and how much memory it needs.

Basic data types

TypeUsed forExampleFormat
charSingle character'A'%c
intWhole numbers120%d
floatDecimal number (6 digits precision)3.14f%f
doubleDecimal number (15 digits precision)3.141592%lf
voidNo valuefunction with no returnnone

Size of data types

The size depends on the system. This program prints it on your computer with sizeof.

#include <stdio.h>

int main(void) {
    printf("char      : %zu byte\n", sizeof(char));
    printf("short     : %zu bytes\n", sizeof(short));
    printf("int       : %zu bytes\n", sizeof(int));
    printf("long      : %zu bytes\n", sizeof(long));
    printf("long long : %zu bytes\n", sizeof(long long));
    printf("float     : %zu bytes\n", sizeof(float));
    printf("double    : %zu bytes\n", sizeof(double));
    return 0;
}

Output:

Output of the above program is

char : 1 byte
short : 2 bytes
int : 4 bytes
long : 8 bytes
long long : 8 bytes
float : 4 bytes
double : 8 bytes

Note: This output is from a 64 bit Mac. On 64 bit Windows long is 4 bytes, so your result can be different. int is normally 4 bytes.

Type modifiers

You can change the size or range with short, long, signed and unsigned. An unsigned int stores only zero and positive numbers, but its upper limit is double.

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

int main(void) {
    printf("int max      : %d\n", INT_MAX);
    printf("int min      : %d\n", INT_MIN);
    printf("unsigned max : %u\n", UINT_MAX);
    return 0;
}

Output:

Output of the above program is

int max : 2147483647
int min : -2147483648
unsigned max : 4294967295

Next: C Constants.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Write a C program to swap the values of two variables using a third variable
Write a C program to print words using spacing and new line characters
Write a C program to get the salary from the user and print it
Write a C program to find the size of int, float, long, double and char
Write a C program to get the marks of 5 subjects and find the total and average
Write a C program to calculate the perimeter of a rectangle
Write a C program to convert minutes into hours and minutes
Interview Questions & Answers
What is the difference between a variable and a constant in C?
What are the data types in C?
What is the difference between = and == in C?
What is the difference between ++i and i++ in C?
What is the use of the sizeof operator in C?
How to swap two numbers in C?