C PROGRAMMING • Beginner

C Data Types

A data type tells the compiler what kind of data a variable will store and how much memory to allocate. C has 4 fundamental data types — int, float, double, and char — plus modifiers like short, long, signed, and unsigned. Choosing the right type is critical for memory efficiency and correctness.

Why Data Types Matter?

  • They define how much memory to allocate.
  • They determine what operations are valid (e.g., can't multiply strings).
  • They specify the range of values a variable can hold.
  • They impact performance — smaller types are faster to process.

The 4 Fundamental Data Types

1. int — Integer

Stores whole numbers (positive, negative, or zero).

int age = 22;
int marks = -5;
int population = 100000;

printf("%d\n", age);   // 22
  • Size: Usually 4 bytes (32 bits) on most modern systems.
  • Range: −2,147,483,648 to 2,147,483,647
  • Format specifier: %d

2. float — Floating Point (small)

Stores decimal numbers with single precision.

float price = 99.99f;
float temperature = 36.6f;

printf("%.2f\n", price);   // 99.99
  • Size: 4 bytes
  • Range: ~1.2E−38 to 3.4E+38
  • Precision: ~6-7 decimal digits
  • Format specifier: %f

3. double — Floating Point (large)

Stores decimal numbers with double precision — more accurate than float.

double pi = 3.141592653589793;
double salary = 45000.50;

printf("%.10f\n", pi);   // 3.1415926536
  • Size: 8 bytes
  • Range: ~2.3E−308 to 1.7E+308
  • Precision: ~15-16 decimal digits
  • Format specifier: %lf (with scanf) or %f (with printf)

4. char — Character

Stores a single character (letter, digit, or symbol). Enclosed in single quotes.

char grade = 'A';
char symbol = '$';
char digit = '7';

printf("%c\n", grade);   // A
printf("%d\n", grade);   // 65 (ASCII code)
  • Size: 1 byte
  • Range: −128 to 127 (signed) or 0 to 255 (unsigned)
  • Format specifier: %c (character) or %d (ASCII value)

5. void — No Value

Represents "no value". Used for functions that return nothing, and for generic pointers.

void greet() {
    printf("Hello!\n");
    // no return value
}

void* ptr;   // generic pointer

Data Type Modifiers

C allows modifiers to change the size or sign of basic types.

  • short — smaller int
  • long — larger int
  • signed — can hold negatives (default for int)
  • unsigned — only positive values
short int small = 100;          // 2 bytes
long int big = 1000000L;        // 8 bytes (usually)
unsigned int pos = 4000000000U; // no negatives
signed int normal = -500;       // default
long long int huge = 9999999999LL;

Complete Size & Range Table

Type                    Size      Range
----                    ----      -----
char                    1 byte    -128 to 127
unsigned char           1 byte    0 to 255
short int               2 bytes   -32,768 to 32,767
unsigned short          2 bytes   0 to 65,535
int                     4 bytes   -2.1B to 2.1B
unsigned int            4 bytes   0 to 4.29B
long int                8 bytes   -9.2E18 to 9.2E18
unsigned long           8 bytes   0 to 1.8E19
long long int           8 bytes   same as long
float                   4 bytes   ~6-7 digits
double                  8 bytes   ~15-16 digits
long double             16 bytes  ~18-19 digits
💡 Academy Tip

Use sizeof operator to check the size of any type on your system. Sizes may vary between architectures — always verify: printf("%lu", sizeof(int));

sizeof Operator

#include <stdio.h>

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

// Output (typical):
// char: 1 byte
// int: 4 bytes
// float: 4 bytes
// double: 8 bytes

Format Specifiers Summary

Type              printf / scanf
----              ---------------
int               %d
unsigned int      %u
short             %hd
long              %ld
long long         %lld
float             %f
double            %lf
char              %c
string            %s
pointer           %p
hex               %x
octal             %o

Type Conversion

Implicit (Automatic) Conversion

Compiler automatically promotes smaller types to larger ones.

int x = 10;
float y = x;         // int → float
double z = y;        // float → double
printf("%.2f", z);   // 10.00

Explicit (Type Casting)

double pi = 3.99;
int x = (int) pi;      // 3 (decimal truncated)
printf("%d\n", x);

int a = 10, b = 3;
float result = (float) a / b;
printf("%.2f\n", result);   // 3.33
⚠ Common Mistake

Integer division truncates: 10 / 3 = 3, not 3.33. Cast at least one operand to float: (float) 10 / 3 = 3.33. This is one of the most common C bugs.

Real-World Example — Employee Record

#include <stdio.h>

int main() {
    // Different data types
    int emp_id = 101;
    char name[] = "Ankit Kumar";
    float salary = 45000.50f;
    double bonus = 5000.75;
    char grade = 'A';
    unsigned int leaves = 12;

    printf("Employee ID: %d\n", emp_id);
    printf("Name: %s\n", name);
    printf("Salary: ₹%.2f\n", salary);
    printf("Bonus: ₹%.2f\n", bonus);
    printf("Grade: %c\n", grade);
    printf("Leaves Taken: %u\n", leaves);

    double total = salary + bonus;
    printf("Total: ₹%.2f\n", total);

    return 0;
}

Output

Employee ID: 101
Name: Ankit Kumar
Salary: ₹45000.50
Bonus: ₹5000.75
Grade: A
Leaves Taken: 12
Total: ₹50001.25

Choosing the Right Type

  • Age, count, quantity → int
  • Price, percentage → float
  • Scientific data, precise math → double
  • Single character → char
  • Large numbers (population, distance) → long
  • Only positive counts → unsigned int
  • Very small numbers (device sensors) → short
📝 Try Yourself

1. Write a program that prints the size of all primitive types using sizeof.
2. Declare an int and a float, then convert the float to int (casting) and print both.
3. Try integer division 10 / 3 and then (float)10 / 3 — observe the difference.
4. Declare an unsigned int with a value of 4 billion and print it.
5. Try storing 300 in a char — what happens?

Quick Revision

  • 4 fundamental types: int, float, double, char.
  • int = 4 bytes, float = 4 bytes, double = 8 bytes, char = 1 byte.
  • Modifiers: short, long, signed, unsigned.
  • sizeof gives the size in bytes.
  • Format specifiers: %d, %f, %lf, %c, %s.
  • Integer division truncates — cast to float for decimals.
  • Use double for precision, float for memory savings.
  • char stores ASCII value (A = 65, a = 97, '0' = 48).

Interview Questions

Q: What is the difference between float and double?

A: float is 4 bytes with ~6-7 decimal digits precision. double is 8 bytes with ~15-16 digits. Use double when accuracy matters (scientific computation).

Q: What is the size of int in C?

A: Typically 4 bytes (32 bits) on modern 64-bit systems, but it can be 2 bytes on older systems. Use sizeof(int) to be sure. C standard only guarantees int is at least 2 bytes.

Q: Difference between signed and unsigned int?

A: signed int (default) can hold both positive and negative values, range −2.1B to 2.1B. unsigned int holds only non-negative values, range 0 to 4.29B. Use unsigned when you know values won't be negative.

Q: What is the default value of an int in C?

A: Global/static int variables default to 0. Local int variables are NOT initialized by default — they contain garbage values (undefined behavior).

Q: Why does 5 / 2 give 2 instead of 2.5 in C?

A: Because both operands are integers, C performs integer division which truncates the decimal. To get 2.5, cast one operand: (float) 5 / 2.

Q: What is the range of a char?

A: signed char goes from −128 to 127. unsigned char goes from 0 to 255. Each char stores an ASCII value.

Q: What is a void pointer?

A: A void* pointer can point to any data type. It's used for generic programming (like malloc(), qsort()). It must be cast to the correct type before dereferencing.

Want to Master C Programming?

Join our C Programming course in Varanasi — practical classes and career support at our Sarnath centre.

Ask on WhatsApp
Chat with us