Data Types Progress
0%
Examples · Data Types

int, float, double & Limits

Understand every number type in C — their sizes, limits, precision, and what happens when you go too far.

int vs long int
float vs double
%.nf precision
Overflow
sizeof & limits.h
1

int vs long int — Whole Numbers

Integer Types

int stores whole numbers in 4 bytes (32 bits). It can hold values from -2,147,483,648 to +2,147,483,647 — roughly ±2 billion.

long int stores whole numbers in 8 bytes (64 bits). It can hold values from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 — roughly ±9.2 quintillion.

  • int format specifier → %d
  • long int format specifier → %ld

Memory layout — how bits are used

int (32 bits)
±
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
= 31 bits for value + 1 sign bit
long (64 bits)
±
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
= 63 bits for value + 1 sign bit
int_vs_longint.c
C
#include <stdio.h>

int main() {

    // INT — 4 bytes — holds up to ~2 billion
    int a = 100;
    int b = -500;
    int c = 2147483647;      // MAX value of int

    printf("int a        = %d\n", a);
    printf("int b        = %d\n", b);
    printf("int max      = %d\n", c);

    // LONG INT — 8 bytes — holds much bigger numbers
    long int x = 100000;
    long int y = -9999999;
    long int z = 9223372036854775807; // MAX of long int

    printf("long int x   = %ld\n", x);
    printf("long int y   = %ld\n", y);
    printf("long int max = %ld\n", z);

    return 0;
}
terminal
output
int a        = 100
int b        = -500
int max      = 2147483647
long int x   = 100000
long int y   = -9999999
long int max = 9223372036854775807
💡 Why these exact numbers?
int = 4 bytes = 32 bits → 2³¹ - 1 = 2,147,483,647 (1 bit used for ± sign)
long int = 8 bytes = 64 bits → 2⁶³ - 1 = 9,223,372,036,854,775,807
2

float vs double vs long double

Decimal Types

float uses 4 bytes and gives about 7 digits of accuracy. Good for simple prices or small decimals.

double uses 8 bytes and gives about 15 digits of accuracy. This is the default decimal type in C — use this most of the time.

long double uses 16 bytes and gives about 18 digits of accuracy. Used in science, engineering, and astronomy.

float_double.c
C
#include <stdio.h>

int main() {

    // FLOAT — 4 bytes — ~7 digits precision
    float f = 3.14159265358979;
    printf("float       = %.7f\n",  f);   // only 7 digits reliable

    // DOUBLE — 8 bytes — ~15 digits precision
    double d = 3.14159265358979;
    printf("double      = %.15f\n", d);   // 15 digits reliable

    // LONG DOUBLE — 16 bytes — ~18 digits precision
    long double ld = 3.14159265358979323846L;
    printf("long double = %.18Lf\n", ld); // 18 digits reliable

    return 0;
}
terminal
output
float       = 3.1415927        ← rounds at 7th digit
double      = 3.141592653589790
long double = 3.141592653589793238
TypeBytesFormat (printf)Format (scanf)Accurate Digits
float4%f%f~7
double8%f or %lf%lf~15
long double16%Lf (capital L)%Lf~18
⚠️ float vs double in scanf:
Always use %f for float and %lf for double inside scanf. Mixing them up causes silent wrong values — no error message, just wrong data.
3

%.nf Precision — How Many Decimal Places?

Format Specifiers

The format %.nf controls exactly how many decimal places are printed. The n tells printf how many digits to show after the decimal point. C rounds automatically at the last digit shown.

  • Minimum n: 0 — shows no decimal places at all
  • Useful maximum: 15 for double, 18 for long double
  • Beyond that: garbage digits — CPU has no real data left to show
precision.c
C
#include <stdio.h>

int main() {
    double num = 3.123456789;

    printf("%.0f\n",  num);   // 0 decimal places
    printf("%.1f\n",  num);   // 1 decimal place
    printf("%.2f\n",  num);   // 2 decimal places
    printf("%.5f\n",  num);   // 5 decimal places — note rounding
    printf("%.10f\n", num);   // 10 decimal places
    printf("%.15f\n", num);   // last accurate digits
    printf("%.20f\n", num);   // garbage starts here!

    return 0;
}
terminal
output
3                               ← %.0f  — whole number only
3.1                             ← %.1f
3.12                            ← %.2f
3.12346                         ← %.5f  — 5 rounds up!
3.1234567890                    ← %.10f
3.123456789000000               ← %.15f — still accurate
3.12345678900000017763568394    ← %.20f — garbage after 17!
FormatOutputUse For
%.0f3Whole number display
%.1f3.1Simple temperature
%.2f3.12Money / prices ₹99.50
%.4f3.1235Engineering values
%.6f3.123457Default %f (6 places)
%.15f3.123456789000000Max safe for double
%.20f3.12345…garbage❌ Do not use
💡 Rounding rule: 3.123456789 with %.5f gives 3.12346 — the 6th digit was 6, so the 5th digit 5 rounds up to 6. C always rounds the last shown digit automatically.
integer overflow — when numbers go too far
4

Integer Overflow — What Happens at the Limit

Overflow

When you go one step beyond the maximum value of an integer type, it does not crash — it wraps around to the most negative number. This is called integer overflow and is one of the most dangerous silent bugs in C.

int overflow — what happens step by step

2,147,483,647
← MAX int
2,147,483,647
+ 1
-2,147,483,648
← WRAPS to MIN!
-2,147,483,648
+ 1
-2,147,483,647
← continues up...
overflow.c
C
#include <stdio.h>

int main() {

    // INT overflow
    int a = 2147483647;      // MAX int
    int b = 2147483647 + 1;  // OVERFLOW!
    printf("int max     = %d\n", a);
    printf("int max + 1 = %d\n", b);  // shows -2147483648 !

    // Real world: 3 billion too big for int!
    int      wrong   = 3000000000;   // WRONG — overflows
    long int correct = 3000000000;   // CORRECT — fits fine
    printf("int wrong   = %d\n",  wrong);    // garbage!
    printf("long correct= %ld\n", correct);  // 3000000000

    // LONG INT overflow
    long int c = 9223372036854775807;     // MAX long
    long int d = 9223372036854775807 + 1; // OVERFLOW!
    printf("long max    = %ld\n", c);
    printf("long max + 1= %ld\n", d); // shows most negative!

    return 0;
}
terminal
output
int max      = 2147483647
int max + 1  = -2147483648      ← overflow!
int wrong    = -1294967296      ← garbage!
long correct = 3000000000       ← correct
long max     = 9223372036854775807
long max + 1 = -9223372036854775808  ← overflow!
⚠️ C gives NO error for overflow! The program runs, compiles, and prints — but gives completely wrong answers. Always use long int when your value might exceed 2 billion. For population, file sizes, or large counts — always use long int.
5

sizeof() and limits.h — Check Size & Limits in Code

sizeof & limits

sizeof() is a built-in operator that returns the number of bytes any type or variable uses in memory. It is extremely useful for understanding and debugging memory usage.

<limits.h> is a library that provides ready-made constants like INT_MAX, INT_MIN, LONG_MAX — so you never have to remember the exact numbers.

sizeof_limits.c
C
#include <stdio.h>
#include <limits.h>    // library for INT_MAX, LONG_MAX etc.

int main() {

    // sizeof — how many bytes each type uses
    printf("--- SIZES ---\n");
    printf("int         = %lu bytes\n", sizeof(int));
    printf("long int    = %lu bytes\n", sizeof(long int));
    printf("float       = %lu bytes\n", sizeof(float));
    printf("double      = %lu bytes\n", sizeof(double));
    printf("long double = %lu bytes\n", sizeof(long double));
    printf("char        = %lu bytes\n", sizeof(char));

    // limits.h constants — INT_MAX, LONG_MAX etc.
    printf("\n--- INT LIMITS ---\n");
    printf("INT_MIN  = %d\n",  INT_MIN);
    printf("INT_MAX  = %d\n",  INT_MAX);

    printf("\n--- LONG LIMITS ---\n");
    printf("LONG_MIN = %ld\n", LONG_MIN);
    printf("LONG_MAX = %ld\n", LONG_MAX);

    return 0;
}
terminal
output
--- SIZES ---
int         = 4 bytes
long int    = 8 bytes
float       = 4 bytes
double      = 8 bytes
long double = 16 bytes
char        = 1 bytes

--- INT LIMITS ---
INT_MIN  = -2147483648
INT_MAX  =  2147483647

--- LONG LIMITS ---
LONG_MIN = -9223372036854775808
LONG_MAX =  9223372036854775807
💡 Use %lu for sizeofsizeof() returns type size_t which is an unsigned long. Always print it with %lu, not %d, to avoid compiler warnings.
6

All Types Together — Complete Reference Program

Full Example

Here is one single program that demonstrates every data type together — int, long int, float, double, long double — with their values, format specifiers, and sizes all in one place. Save this as your reference.

all_types.c
C
#include <stdio.h>

int main() {

    // ── INTEGER TYPES ──
    int         i  = 2147483647;
    long int    li = 9223372036854775807;

    // ── DECIMAL TYPES ──
    float       f  = 3.14159265358979;
    double      d  = 3.14159265358979;
    long double ld = 3.14159265358979323846L;

    printf("--- INTEGER TYPES ---\n");
    printf("int          = %d\n",    i);
    printf("long int     = %ld\n",   li);

    printf("\n--- DECIMAL TYPES ---\n");
    printf("float        = %.7f\n",   f);
    printf("double       = %.15f\n",  d);
    printf("long double  = %.18Lf\n", ld);

    printf("\n--- SIZES IN MEMORY ---\n");
    printf("int          = %lu bytes\n", sizeof(int));
    printf("long int     = %lu bytes\n", sizeof(long int));
    printf("float        = %lu bytes\n", sizeof(float));
    printf("double       = %lu bytes\n", sizeof(double));
    printf("long double  = %lu bytes\n", sizeof(long double));

    return 0;
}
terminal
output
--- INTEGER TYPES ---
int          = 2147483647
long int     = 9223372036854775807

--- DECIMAL TYPES ---
float        = 3.1415927
double       = 3.141592653589790
long double  = 3.141592653589793238

--- SIZES IN MEMORY ---
int          = 4 bytes
long int     = 8 bytes
float        = 4 bytes
double       = 8 bytes
long double  = 16 bytes
quick reference & quiz

Complete Quick Reference Table

TypeBytesBitsprintfscanfMax ValueUse For
int432%d%d2,147,483,647age, score, year
long int864%ld%ld9,223,372,036,854,775,807population, big counts
float432%f%f3.4 × 10³⁸simple decimals
double864%f%lf1.7 × 10³⁰⁸precise decimals ✅
long double16128%Lf%Lf1.1 × 10⁴⁹³²science / max precision
char18%c%c127single character
💡 Golden Rule:
Whole number → int → too big? → long int
Decimal number → double by default → need more? → long double
Simple / small decimal → float (saves memory)
Q

Quick Quiz — Test Yourself

Question 1 of 5

What is the maximum value of int in C?

Question 2 of 5

Which format specifier do you use to print a long int?

Question 3 of 5

What happens when you store 3000000000 in an int variable?

Question 4 of 5

How many reliable decimal digits does double give you?

Question 5 of 5

Which format specifier is used to print a long double?

Lesson Checklist

  • I know int uses 4 bytes and holds up to 2,147,483,647
  • I know long int uses 8 bytes and holds up to 9.2 quintillion
  • I use %d for int and %ld for long int
  • I understand float gives ~7 digits of precision
  • I understand double gives ~15 digits and is the default decimal type
  • I know long double uses %Lf with capital L
  • I understand %.nf controls decimal places in output
  • I know beyond %.15f for double gives garbage digits
  • I understand integer overflow — no error, just wrong value
  • I can use sizeof() and know to print it with %lu
  • I know limits.h provides INT_MAX, LONG_MAX constants
  • I completed the quiz