Loading...

C essentialsFoundations

Data types, sizes & limits

10 min lesson

Next
  • A data type tells the computer what kind of value a variable will hold, such as a whole number, a decimal, or a single character.
  • Data types matter because the computer needs to know how much memory to reserve and how to interpret the bits stored there.
  • Think of data types as different-sized containers in a kitchen: a teaspoon for salt, a cup for flour, and a big bowl for soup. Each holds a different amount and a different kind of thing.
  • int stores whole numbers like 42, -7 or 1000. It is usually 4 bytes and can hold roughly -2 billion to 2 billion.
  • float stores decimal numbers like 3.14 or -0.5 with about 6-7 digits of precision. It is usually 4 bytes.
  • double stores decimals with about 15 digits of precision. It is usually 8 bytes and is the default for decimal literals.
  • char stores a single character like 'A' or '7'. It is 1 byte and is really a small whole number underneath.
  • The computer reserves exactly the number of bytes the type needs and uses that size to know where the next variable begins in memory.
  • The syntax is: type name = value; for example int count = 10; or char grade = 'A';.
  • A realistic use: storing a person's age as an int, their salary as a double, and their initial as a char.
  • A surprising detail: the exact size of int can vary between computers, so C provides sizeof to check it on your machine.
  • The example program prints the size in bytes of several types and shows the largest and smallest int value using limits.h.
Keep in mind
  • Choose the type that matches your data: int for counting, double for money and measurements, char for single characters.
  • Never assume int is always 4 bytes; use sizeof to check on your system.
  • Prefer double over float for decimals unless memory is very tight.
  • Watch out for overflow: adding 1 to the largest int wraps around to a negative number.
  • You now know the building blocks of every variable. Keep going, you are building a strong foundation!
main.c
#include <stdio.h>
#include <limits.h>

int main(void) {
    // Ask the computer how many bytes each type uses on this machine
    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));
    printf("Size of char: %zu bytes\n", sizeof(char));

    // limits.h gives us the largest and smallest int values
    // INT_MAX is the biggest whole number an int can hold here
    printf("Largest int: %d\n", INT_MAX);
    // INT_MIN is the smallest (most negative) whole number
    printf("Smallest int: %d\n", INT_MIN);

    // Show a char stored and printed as a character
    char grade = 'A';
    printf("Grade: %c\n", grade);

    return 0;
}

What happens: Size of int: 4 bytes Size of float: 4 bytes Size of double: 8 bytes Size of char: 1 bytes Largest int: 2147483647 Smallest int: -2147483648 Grade: A

Breaking it down

limits.h include

#include <limits.h>

Brings in constants like INT_MAX and INT_MIN that describe the limits of integer types on your machine. Without it those names are unknown.

sizeof operator

sizeof(int)

Asks the compiler how many bytes a type uses. It is replaced by a number at compile time, so it costs nothing at runtime.

%zu format specifier

%zu

Prints a size_t value, which is the type sizeof returns. Using %d here would be technically wrong on some systems.

INT_MAX and INT_MIN

INT_MAX

Predefined constants for the largest and smallest int. They show the range you can safely store before overflow happens.

char variable

char grade = 'A';

Stores a single character. The single quotes are required; double quotes would make it a string, which is different.

%c format specifier

%c

Prints a char as a character. Using %d instead would print the numeric code behind the character.

Common C data types

TypeTypical sizeWhat it holdsExample
char1 byteOne character'A'
intUsually 4 bytesWhole numbers42
floatUsually 4 bytesDecimals, ~7 digits3.14f
doubleUsually 8 bytesDecimals, ~15 digits3.14159
shortUsually 2 bytesSmaller whole numbers1000
longUsually 8 bytesLarger whole numbers1000000L
unsigned intUsually 4 bytesWhole numbers 0 and up4000000000u

A helpful analogy

Data types are like different-sized containers in a kitchen. A teaspoon (char) holds a tiny amount, a cup (int) holds a moderate amount, and a big pot (double) holds a large amount with more precision. Using the wrong container either wastes space or overflows and makes a mess.

Nesting rules

  • 1.Use the smallest type that safely fits your data, but do not go so small that overflow becomes likely.
  • 2.Use double for money and science unless you have a strong reason to save memory with float.
  • 3.Never mix signed and unsigned types in comparisons; the signed value can silently become huge.
  • 4.Check sizeof on your actual machine before assuming a size; the C standard only guarantees minimums.

Common mistakes

Assuming int is always 4 bytes

Avoid

int always holds 2 billion; // not guaranteed

Use instead

printf("%zu", sizeof(int)); // check it

The C standard only guarantees int is at least 2 bytes. On most modern systems it is 4, but embedded systems may differ.

Using double quotes for a char

Avoid

char grade = "A"; // string, not char

Use instead

char grade = 'A'; // single char

Double quotes create a string (a pointer to characters), which does not fit in a char. The compiler warns or errors.

Ignoring integer overflow

Avoid

int big = INT_MAX + 1; // wraps around

Use instead

long long big = (long long)INT_MAX + 1;

Signed overflow is undefined behavior in C. Use a larger type or check before adding.

Quick tips

  • •Print sizeof for every type once to learn the sizes on your own machine.
  • •Use %zu for sizeof results, not %d.
  • •Remember that a char is really a small integer; 'A' equals 65 on most systems.
  • •When in doubt for decimals, use double; modern computers handle it easily.
  • •Try printing (char)65 to see it prints the letter A.
Previous
Next