Skip to content
StudyFame

Data Types in C

Before you store something, C wants to know what kind of data it is: a whole number, a decimal number, or a letter. This is called the data type.

The data type decides two things: what values a variable can hold and how much memory it takes. In this lesson you will learn all the data types of C with their sizes.

What You Will Learn

  • What a data type is.
  • The 3 categories of data types: basic, derived and user-defined.
  • The 4 basic types: int, char, float, double (and void).
  • Size and range of each type.
  • How to find the size with sizeof.

Data Types in C Explained

Exam definition: A data type tells the compiler what type of value a variable can store and how much memory it needs. C data types are of three kinds: basic (primary), derived and user-defined.

CategoryTypes
Basic (primary)int, char, float, double, void
DerivedArray, pointer, function
User-definedstruct, union, enum, typedef

Memory trick: "B-D-U" = Basic, Derived, User-defined. Derived = "APF" (Array, Pointer, Function). User-defined = "SUET" (Struct, Union, Enum, Typedef).

The basic data types

TypeStoresSize (usual)FormatExample
charA single character1 byte%cchar grade = 'A';
intWhole numbers4 bytes%dint age = 19;
floatDecimal numbers (about 6–7 digits)4 bytes%ffloat price = 49.5f;
doubleDecimal numbers (about 15–16 digits)8 bytes%lf (scanf), %f (printf)double pi = 3.14159265;
voidNo value (empty)––void show(void);

Sizes can differ between computers. The values above are true for almost all modern 64-bit systems. char is always 1 byte.

Range of values

TypeRange (usual)
char−128 to 127 (as signed char; 0 to 255 as unsigned char)
int−2,147,483,648 to 2,147,483,647 (about ±2.1 billion)
floatabout 1.2 × 10−38 to 3.4 × 1038
doubleabout 2.3 × 10−308 to 1.7 × 10308

float vs double

  • float = single precision: 4 bytes, about 6–7 correct digits.
  • double = double precision: 8 bytes, about 15–16 correct digits.
  • Use double when you need more accuracy (money calculations, science).

Syntax: Declaring Variables of Each Type

char   letter = 'C';      /* single quotes for a character */
int    count  = 100;
float  marks  = 92.5f;    /* the f means "this is a float" */
double area   = 153.938;

Example 1: Size of Data Types Using sizeof

The sizeof operator tells you how many bytes a type uses on your computer.

#include <stdio.h>

int main(void)
{
    printf("Size of char   = %zu byte\n", sizeof(char));
    printf("Size of int    = %zu bytes\n", sizeof(int));
    printf("Size of float  = %zu bytes\n", sizeof(float));
    printf("Size of double = %zu bytes\n", sizeof(double));
    return 0;
}

Output

Size of char   = 1 byte
Size of int    = 4 bytes
Size of float  = 4 bytes
Size of double = 8 bytes

Code Explanation

  1. sizeof(char): gives the size of char in bytes.
  2. %zu: the correct format specifier for the result of sizeof (an unsigned size value). Some old compilers use %lu instead.
  3. Each line prints one type and its size.

How Data Types Work in Memory

  • Memory is made of bytes. One byte = 8 bits (8 zeros and ones).
  • An int takes 4 bytes = 32 bits. With 32 bits, about 4.29 billion different values can be stored, which is why its range is about ±2.1 billion.
  • A char is stored as a number too. 'A' is stored as 65 (its ASCII code).
  • Decimal numbers (float, double) are stored in a special format, so they are sometimes slightly inaccurate (0.1 cannot be stored exactly).

Example 2: Student Details Using Different Data Types

#include <stdio.h>

int main(void)
{
    char section = 'B';
    int roll_no = 27;
    float percentage = 86.75f;
    double fees = 125000.50;

    printf("Section    : %c\n", section);
    printf("Roll No    : %d\n", roll_no);
    printf("Percentage : %.2f\n", percentage);
    printf("Fees       : %.2f\n", fees);
    printf("ASCII of %c : %d\n", section, section);

    return 0;
}

Output

Section    : B
Roll No    : 27
Percentage : 86.75
Fees       : 125000.50
ASCII of B : 66

The last line prints the same char twice: once as a letter (%c) and once as a number (%d). This shows that characters are stored as numbers.

Important Points to Remember

  • 3 categories: Basic, Derived, User-defined.
  • Basic types: int, char, float, double, void.
  • Usual sizes: char 1, int 4, float 4, double 8 bytes.
  • char is stored as its ASCII number ('A' = 65).
  • void means no value.

Common Mistakes Beginners Make

MistakeWhat happensFix
int price = 49.99;Decimal part is lost; stores 49Use float or double
char name = "Riya";Error / wrong value: a char holds one characterUse a string (array): char name[] = "Riya";
char c = "A";Double quotes make a string, not a characterchar c = 'A';
Printing a float with %dWrong outputUse %f

Data Types in C Practice Questions

  1. What is a data type? Name its three categories.
  2. Write the four basic data types with their usual sizes.
  3. What is the difference between float and double?
  4. Name two derived and two user-defined data types.
  5. Which data type would you use for: age, price of a phone, first letter of your name?
Show answers
  1. It tells the type of value a variable can store and its memory size. Categories: basic, derived, user-defined.
  2. char 1 byte, int 4 bytes, float 4 bytes, double 8 bytes.
  3. float is single precision (4 bytes, ~6–7 digits); double is double precision (8 bytes, ~15–16 digits).
  4. Derived: array, pointer (also function). User-defined: struct, union (also enum, typedef).
  5. Age: int. Price: float or double. First letter: char.

Coding Exercise

Write a program that stores a product's code letter ('P'), quantity (12) and price per item (24.50), calculates the total cost in a double, and prints all values.

Show solution
#include <stdio.h>

int main(void)
{
    char code = 'P';
    int quantity = 12;
    double price = 24.50;
    double total = quantity * price;

    printf("Code: %c\n", code);
    printf("Quantity: %d\n", quantity);
    printf("Price: %.2f\n", price);
    printf("Total: %.2f\n", total);
    return 0;
}
Code: P
Quantity: 12
Price: 24.50
Total: 294.00

Quick Quiz: Data Types in C (MCQ)

  1. What is the size of char in C?

    A) 1 byte   B) 2 bytes   C) 4 bytes   D) 8 bytes

  2. Which type stores decimal numbers with the most precision?

    A) int   B) char   C) float   D) double

  3. An array is which kind of data type?

    A) Basic   B) Derived   C) User-defined   D) None

  4. Which format specifier prints a char?

    A) %d   B) %f   C) %c   D) %s

  5. void means:

    A) Zero   B) No value   C) Empty string   D) Error

Show answers
  1. A: 1 byte.
  2. D: double.
  3. B: derived.
  4. C: %c.
  5. B: no value.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

CategoriesBasic, Derived, User-defined
Basic typesint, char, float, double, void
DerivedArray, Pointer, Function
User-definedstruct, union, enum, typedef
Sizeschar 1, int 4, float 4, double 8 (bytes)
Formats%c, %d, %f, %lf
'A' stored as65 (ASCII)

Frequently asked questions

What is the difference between float and double?
float is 4 bytes, single precision; double is 8 bytes, double precision.
Why is the size of int not fixed in C?
It depends on the compiler and computer; C only fixes the minimum. On modern systems it is 4 bytes.
What is void used for?
To say "no value", e.g. a function that returns nothing.
How is a character stored in memory?
As a number: its ASCII code ('A' = 65, 'a' = 97).

Remember

  • Categories B-D-U; Derived APF; User-defined SUET.
  • Sizes: 1-4-4-8 (char, int, float, double).

Next Topic

Next lesson: Type Modifiers in C. You will learn how short, long, signed and unsigned change the size and range of a type.