CBasicsbeginner

Data Types in C with Examples

Every basic C data type with its usual size, format specifier and a short program with output, plus modifiers like unsigned and long.

Published
TL;DR – Quick Answer

The basic data types in C are int for whole numbers, float and double for decimal numbers, and char for single characters. On most modern computers int is 4 bytes, float is 4 bytes, double is 8 bytes and char is 1 byte. Print an int with %d, a float or double with %f and a char with %c. Read a double with %lf in scanf.

On This Page

Every variable in C has a data type. The type tells the compiler how much memory to set aside and what kind of value the variable can hold: a whole number, a decimal number or a character. C will not guess. You must state the type when you declare the variable.

This page covers each basic data type with its usual size, its format specifier for printf and scanf, and a short program with output. If you have not set up a compiler yet, see install a C compiler and run your first program.

Types of data types in C

Group Types Example
Basic (primary) int, float, double, char int age = 19;
Void void void show(void) returns nothing
Derived Arrays, pointers, functions int marks[5];
User-defined struct, union, enum, typedef struct Student s;

This page focuses on the basic types. Arrays and structures have their own pages: arrays in C and structures in C.

Sizes and format specifiers at a glance

The sizes below are what you get with GCC on a normal 64-bit Windows or Linux computer. The C standard sets only minimum sizes, so other systems can differ. That is why C has the sizeof operator.

Type Usual size Holds Usual range printf scanf
char 1 byte One character -128 to 127 %c %c
unsigned char 1 byte Small non-negative number 0 to 255 %u or %c %hhu
short 2 bytes Small whole number -32,768 to 32,767 %hd %hd
int 4 bytes Whole number -2,147,483,648 to 2,147,483,647 %d %d
unsigned int 4 bytes Non-negative whole number 0 to 4,294,967,295 %u %u
long 4 bytes on Windows, 8 on 64-bit Linux Whole number Depends on size %ld %ld
long long 8 bytes Very large whole number About ±9.2 × 10^18 %lld %lld
float 4 bytes Decimal, about 6-7 digits About ±3.4 × 10^38 %f %f
double 8 bytes Decimal, about 15-16 digits About ±1.7 × 10^308 %f or %lf %lf

Check sizes on your computer with sizeof

sizeof gives the size of a type in bytes. Its result has the type size_t, which is printed with %zu.

#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 long : %zu bytes\n", sizeof(long long));
    printf("float     : %zu bytes\n", sizeof(float));
    printf("double    : %zu bytes\n", sizeof(double));
    return 0;
}

Output with GCC on a 64-bit computer:

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

sizeof(char) is always 1 by definition. The others are typical values. long is left out of this program on purpose, because it prints 4 on Windows and 8 on 64-bit Linux and macOS.

int: whole numbers

Use int for counts, marks, ages, roll numbers and loop counters.

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

int main(void) {
    int marks = 87;
    int temperature = -4;

    printf("Marks: %d\n", marks);
    printf("Temperature: %d\n", temperature);
    printf("Largest int: %d\n", INT_MAX);
    printf("Smallest int: %d\n", INT_MIN);
    printf("7 / 2 = %d\n", 7 / 2);
    return 0;
}

Output:

Marks: 87
Temperature: -4
Largest int: 2147483647
Smallest int: -2147483648
7 / 2 = 3

INT_MAX and INT_MIN come from <limits.h>. Dividing two int values gives an int, so 7 / 2 is 3, not 3.5. The decimal part is dropped.

Common mistake: Going past INT_MAX. Adding 1 to the largest int is undefined behaviour in C, so the result cannot be trusted. Use long long for values such as phone numbers or large totals.

unsigned and long long

unsigned removes negative numbers and doubles the positive range. long long gives at least 64 bits.

#include <stdio.h>

int main(void) {
    unsigned int students = 4000000000u;
    long long population = 1400000000LL;
    long long bigger = population * 5;

    printf("Students: %u\n", students);
    printf("Population: %lld\n", population);
    printf("Five times: %lld\n", bigger);
    return 0;
}

Output:

Students: 4000000000
Population: 1400000000
Five times: 7000000000

4,000,000,000 does not fit in a normal int, but it fits in an unsigned int. 7,000,000,000 needs long long. The suffixes u and LL tell the compiler the type of the constant.

float and double: decimal numbers

#include <stdio.h>

int main(void) {
    float cgpa = 8.5f;
    float pi_f = 3.14159f;
    double pi_d = 3.141592653589793;

    printf("CGPA: %f\n", cgpa);
    printf("CGPA: %.1f\n", cgpa);
    printf("pi as float : %.2f\n", pi_f);
    printf("pi as double: %.10f\n", pi_d);
    printf("Scientific  : %e\n", 12345.678);
    return 0;
}

Output:

CGPA: 8.500000
CGPA: 8.5
pi as float : 3.14
pi as double: 3.1415926536
Scientific  : 1.234568e+04

By default %f shows six digits after the decimal point. %.1f shows one and %.2f shows two, with rounding. %e prints in scientific notation.

The f in 8.5f makes the constant a float. Without it, 8.5 is a double. Both work here, but writing the f avoids compiler warnings in larger programs.

Why double is more precise

Decimal values are stored in binary, so most of them are stored as a very close approximation. float has fewer bits, so the error shows up sooner.

#include <stdio.h>

int main(void) {
    float  a = 0.1f;
    double b = 0.1;

    printf("float : %.10f\n", a);
    printf("double: %.10f\n", b);
    return 0;
}

Output:

float : 0.1000000015
double: 0.1000000000

For money, marks percentages and most calculations, prefer double. Also avoid comparing decimals with ==. Check whether the difference is very small instead.

char: single characters

A char holds one character in single quotes. Inside the computer it is stored as a small number, its character code (ASCII for English letters and digits).

#include <stdio.h>

int main(void) {
    char grade = 'A';
    char next = grade + 1;
    char digit = '7';

    printf("Grade: %c\n", grade);
    printf("Code of %c: %d\n", grade, grade);
    printf("Next letter: %c\n", next);
    printf("Digit char %c as number: %d\n", digit, digit - '0');
    return 0;
}

Output:

Grade: A
Code of A: 65
Next letter: B
Digit char 7 as number: 7
Character Code Character Code
'A' 65 'a' 97
'Z' 90 'z' 122
'0' 48 '9' 57
' ' (space) 32 '\n' 10

Printing a char with %c shows the character. With %d it shows the code. digit - '0' turns the character '7' into the number 7, a trick used often in programs that work with digits.

Common mistake: Writing char name = "Ravi";. A char holds one character only. Text needs a char array, such as char name[] = "Ravi";. See strings in C.

Reading each type with scanf

#include <stdio.h>

int main(void) {
    int age;
    float height;
    double salary;
    char section;

    printf("Enter age, height, salary and section: ");
    scanf("%d %f %lf %c", &age, &height, &salary, &section);

    printf("Age: %d\n", age);
    printf("Height: %.2f\n", height);
    printf("Salary: %.2f\n", salary);
    printf("Section: %c\n", section);
    return 0;
}

Sample run:

Enter age, height, salary and section: 20 5.75 25000.50 B
Age: 20
Height: 5.75
Salary: 25000.50
Section: B

Note %lf for the double in scanf. In printf, %f works for both float and double.

More format specifiers

Specifier Prints Example call Output
%d or %i int printf("%d", 42) 42
%5d int, at least 5 wide printf("[%5d]", 42) [ 42]
%x Hexadecimal printf("%x", 255) ff
%o Octal printf("%o", 255) 377
%.3f Decimal with 3 places printf("%.3f", 2.5) 2.500
%s String printf("%s", "C") C
%% A percent sign printf("100%%") 100%

To check hexadecimal, octal and binary values by hand, use the number system converter.

Choosing the right data type

You want to store Use
Age, marks, count, roll number int
Large totals, phone numbers as numbers long long
CGPA, percentage, price double (or float to save memory)
A grade, a yes/no answer as 'Y' or 'N' char
A name or a sentence char array (string)
True or false int with 0 and 1, or bool from <stdbool.h>

Quick revision

  1. int holds whole numbers and is usually 4 bytes. Print with %d.
  2. float (4 bytes) and double (8 bytes) hold decimals. Print with %f. Read double with %lf.
  3. char holds one character and is 1 byte. Print with %c, or %d for its code.
  4. unsigned, short, long and long long change range and size.
  5. Use sizeof to check sizes on your own system.
  6. int / int drops the decimal part.

The same ideas apply in every language, with different names. Variables and data types compares them across languages. Next in C, learn to work with these values using operators in C.

Frequently Asked Questions

What are the basic data types in C?
The basic (primary) data types are int, float, double and char. void is also a type that means no value. Modifiers such as short, long, signed and unsigned change the size or range of int and char.
What is the size of int in C?
The C standard only says int is at least 16 bits. On almost every modern desktop and laptop compiler, including GCC on Windows and Linux, int is 4 bytes. Use sizeof(int) to check on your system.
What is the difference between float and double?
float usually uses 4 bytes and keeps about 6 to 7 correct digits. double usually uses 8 bytes and keeps about 15 to 16 correct digits. Use double unless you have a reason to save memory.
Which format specifier is used for double in C?
Use %f or %lf to print a double with printf. Use %lf to read a double with scanf. Using %f in scanf for a double is a common bug.
Is string a data type in C?
No. C has no built-in string type. A string is an array of char that ends with the null character '\0'.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — Explore the Java Full Stack program

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 5 December 2026 LinkedIn
Chat with us