Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / C Programming Tutorial / Chapter 1: Foundations & Environment Setup
C ⏱ 10 min read

Variables, Data Types, and Constants

Unlike Python, C is statically typed. You must declare the exact type of every variable before you can use it, informing the compiler how many bytes of RAM to reserve.

What is it?

Data types define the size and type of values a variable can store. Primary types in C include `int`, `float`, `double`, and `char`.

Why is it used?

Guarantees memory efficiency and enables direct CPU register optimizations for high performance.

Detailed Explanation

Standard Data Types in 32/64-bit Systems: • `int` (Integer): Typically 4 bytes (range: -2,147,483,648 to 2,147,483,647). • `float` (Single precision decimal): 4 bytes (6-7 decimal places of precision). • `double` (Double precision decimal): 8 bytes (15 decimal places of precision). • `char` (Character): 1 byte (stores ASCII code, e.g. `'A'` is 65). Type Modifiers: • `short int` (2 bytes), `long int` (4 or 8 bytes), `unsigned int` (positive numbers only: 0 to 4,294,967,295). • `sizeof(type)` operator: Returns the exact size of a type or variable in bytes.

Syntax Structure

Syntax Blueprint
int age = 20;
float price = 2500.50f;
char grade = 'A';
const double PI = 3.1415926535;

Code Example

C Code
#include <stdio.h>

int main() {
    // Variable declarations with initializations
    int course_fee = 2500;
    float gst_rate = 0.18f;
    char rating_grade = 'A';
    double precise_pi = 3.141592653589793;
    
    // Read-only constant using 'const' keyword
    const int max_batch_size = 25;
    
    printf("Course Fee:   ₹%d\n", course_fee);
    printf("GST Rate:     %.2f\n", gst_rate);
    printf("Grade:        %c (ASCII: %d)\n", rating_grade, rating_grade);
    printf("Max Batch:    %d students\n", max_batch_size);
    
    // Inspecting memory footprints with sizeof operator
    printf("\n--- Memory Allocation (Bytes) ---\n");
    printf("sizeof(char):   %zu byte\n", sizeof(char));
    printf("sizeof(int):    %zu bytes\n", sizeof(int));
    printf("sizeof(float):  %zu bytes\n", sizeof(float));
    printf("sizeof(double): %zu bytes\n", sizeof(double));
    
    return 0;
}

Explanation of the Code

• `%zu` is the standard format specifier for `sizeof` (`size_t`). • Demonstrates that a `char` stores an underlying integer ASCII code (`'A'` = 65). • `const` prevents `max_batch_size` from being altered later.

Expected Output

Course Fee: ₹2500 GST Rate: 0.18 Grade: A (ASCII: 65) Max Batch: 25 students --- Memory Allocation (Bytes) --- sizeof(char): 1 byte sizeof(int): 4 bytes sizeof(float): 4 bytes sizeof(double): 8 bytes

Real-World Application

Selecting the right data type (`short` vs `int` vs `long`) is critical in embedded hardware, game graphics engines, and networking packets.

Common Mistakes to Avoid

• Single quotes for strings: `'Hello'` is invalid in C! Use single quotes `'A'` for single characters, double quotes `"Hello"` for strings. • Integer overflow: Exceeding 2.14 billion on a standard 32-bit signed `int` wraps into negative numbers.

Quick Practice Challenge

Write a C program that prints the `sizeof` `long long int` and `unsigned int` on your machine.

Key Takeaways

• C is statically typed; memory size is fixed at compile time. • Standard types: `char` (1B), `int` (4B), `float` (4B), `double` (8B). • Use `sizeof()` to check exact memory footprints across platforms.