07 - C Data Types - Integers, Floats, Characters

Learn about C data types and how to choose the right type for your data


What is this topic?

This guide explains C Data Types - Integers, Floats, Characters, and More in simple terms, what it does, and how to use it in real C programs.

Why We Need It

  • It helps you write correct and reliable C code.
  • It makes your programs easier to read and maintain.
  • It is used in real projects and interviews.
  • It reduces common beginner mistakes.
  • It builds a strong foundation for advanced topics.

Use Cases

  • Building practical C programs step by step.
  • Solving real coding tasks with clean logic.
  • Preparing for exams, interviews, and projects.
  • Understanding and improving existing C code.

What Are Data Types?

A data type tells C what kind of data a variable will hold. Different types use different amounts of memory and can store different ranges of values.

Think of it like different containers:

  • Small box for small numbers
  • Large box for large numbers
  • Text box for letters

Primary Data Types

C has several main data types:

Type Size Purpose Example
int 4 bytes Whole numbers int age = 25;
float 4 bytes Decimal numbers float price = 9.99;
double 8 bytes Large decimals double pi = 3.14159;
char 1 byte Single characters char letter = 'A';

Integer (int)

Stores whole numbers (no decimals).

Characteristics

  • Stores positive and negative whole numbers
  • Range: approximately -2 billion to +2 billion
  • Uses 4 bytes of memory

Examples

#include <stdio.h>

int main() {
    int age = 25;
    int temperature = -5;
    int population = 50000;
    
    printf("Age: %d\n", age);
    printf("Temperature: %d\n", temperature);
    printf("Population: %d\n", population);
    
    return 0;
}

Output:

Age: 25
Temperature: -5
Population: 50000

Float (float)

Stores decimal numbers with less precision.

Characteristics

  • Stores numbers with decimals
  • Less precise than double
  • Uses 4 bytes
  • Good for most calculations

Examples

#include <stdio.h>

int main() {
    float price = 19.99;
    float height = 5.9;
    float temperature = 98.6;
    
    printf("Price: $%.2f\n", price);
    printf("Height: %.1f feet\n", height);
    printf("Temperature: %.1f°F\n", temperature);
    
    return 0;
}

Output:

Price: $19.99
Height: 5.9 feet
Temperature: 98.6°F

Double (double)

Stores decimal numbers with more precision.

Characteristics

  • Most precise decimal type
  • Uses 8 bytes
  • Better for scientific calculations
  • Slightly slower than float

Examples

#include <stdio.h>

int main() {
    double pi = 3.14159265359;
    double avogadro = 6.02214076e23;
    
    printf("Pi: %.11f\n", pi);
    printf("Avogadro's Number: %.2e\n", avogadro);
    
    return 0;
}

Character (char)

Stores single characters.

Characteristics

  • Stores one character
  • Uses 1 byte
  • Represented in single quotes
  • Each character has an ASCII value

Examples

#include <stdio.h>

int main() {
    char initial = 'J';
    char grade = 'A';
    char symbol = '$';
    
    printf("Initial: %c\n", initial);
    printf("Grade: %c\n", grade);
    printf("Symbol: %c\n", symbol);
    
    return 0;
}

Output:

Initial: J
Grade: A
Symbol: $

ASCII Values

Characters are actually stored as numbers (ASCII):

#include <stdio.h>

int main() {
    char letter = 'A';
    
    printf("Character: %c\n", letter);    // Shows: A
    printf("ASCII Value: %d\n", letter);  // Shows: 65
    
    return 0;
}

String (char array)

Store multiple characters (we’ll cover this more later).

char name[] = "John";
printf("Name: %s\n", name);

Data Type Sizes

Check the size of data types with sizeof():

#include <stdio.h>

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

Output (typical):

int: 4 bytes
float: 4 bytes
double: 8 bytes
char: 1 byte

Integer Type Variations

For different size needs:

Type Size Range
char 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes ~-2 billion to +2 billion
long 4 or 8 bytes Very large numbers

Choosing the Right Data Type

Use int for:

  • Ages, counts, IDs
  • Most whole numbers

Use float for:

  • Prices, measurements
  • Most decimal calculations

Use double for:

  • Scientific calculations
  • Very precise decimals

Use char for:

  • Individual characters
  • Single letters

Practical Example

#include <stdio.h>

int main() {
    // Store product information
    char name[] = "Laptop";
    int quantity = 5;
    float price = 999.99;
    
    // Calculate total
    float total = quantity * price;
    
    // Display
    printf("Product: %s\n", name);
    printf("Quantity: %d\n", quantity);
    printf("Price: $%.2f\n", price);
    printf("Total: $%.2f\n", total);
    
    return 0;
}

Output:

Product: Laptop
Quantity: 5
Price: $999.99
Total: $4999.95

Common Mistakes

Mistake 1: Wrong Format Specifier

float price = 19.99;
printf("%d\n", price);  // ✗ Wrong! Should use %f

Fix:

printf("%.2f\n", price);  // ✓ Correct

Mistake 2: Integer Division

int x = 7 / 2;
printf("%d\n", x);  // Prints 3 (not 3.5!)

Why: Both are integers, so result is integer.

Fix:

float x = 7.0 / 2.0;
printf("%.1f\n", x);  // Prints 3.5

Mistake 3: Wrong Quotes for Characters

char letter = "A";   // ✗ Double quotes - wrong!
char letter = 'A';   // ✓ Single quotes - correct!

Quick Reference

// Integer - whole numbers
int age = 25;
printf("%d\n", age);

// Float - decimals
float price = 19.99;
printf("%.2f\n", price);

// Double - more precise
double value = 3.14159265;
printf("%.10f\n", value);

// Char - single character
char grade = 'A';
printf("%c\n", grade);

// String - multiple characters
char name[] = "John";
printf("%s\n", name);

Practice Exercise

Create datatypes.c:

#include <stdio.h>

int main() {
    int students = 30;
    float gpa = 3.75;
    double pi = 3.14159265;
    char grade = 'A';
    char subject[] = "Math";
    
    printf("=== Class Information ===\n");
    printf("Subject: %s\n", subject);
    printf("Number of Students: %d\n", students);
    printf("Class GPA: %.2f\n", gpa);
    printf("Overall Grade: %c\n", grade);
    printf("Pi Value: %.5f\n", pi);
    
    return 0;
}

:books: Navigation


:red_question_mark: Have Questions?

Comment below and we’ll help you! Confused about data types? Let us know in the comments!