Variables, Data Types & Input

Variables & Fundamental Data Types Deep Dive

Understand memory containers: int, float, double, char, size in bytes, and format specifiers.

1. The Hook Imagine you own a courier delivery company. You have small envelopes for letters, medium cardboard boxes for shoes, and heavy wooden crates for refrigerators.

Would you put a single letter inside a massive refrigerator crate? Or try to cram a refrigerator inside a paper envelope? Of course not!

In C programming, data comes in different sizes and shapes. We use Data Types to pick the right sized storage box in RAM memory!


2. Real World Story When you store items in your hostel room: - Clothes go in your closet. - Books go on your desk. - Water goes in a bottle.

Each item needs a container designed specifically for its type.


3. Problem Computers store integers (whole numbers), decimals, and text characters differently in binary. We need explicit data types so the CPU knows how many RAM bytes to reserve and how to interpret the binary bits inside.


4. Concept: C Fundamental Data Types

| Data Type | Description | RAM Size | Format Specifier | Example Declaration | | :--- | :--- | :--- | :--- | :--- | | int | Integer (whole numbers) | 4 Bytes | %d | int age = 19; | | float | Floating point decimal | 4 Bytes | %f | float price = 99.50f; | | double | Double precision decimal | 8 Bytes | %lf | double pi = 3.14159265; | | char | Single character | 1 Byte | %c | char grade = 'A'; |


5. Mental Model - `int` = Medium box for whole numbers. - `float` = Glass jar for decimal numbers. - `char` = Small envelope holding 1 letter character.


6. Visualization

RAM Memory Allocation:

+-------------------+-------------------+-------------------+-------------------+ | age (int) | cgpa (float) | grade (char) | Unallocated RAM | | 4 Bytes (0x1000) | 4 Bytes (0x1004) | 1 Byte (0x1008) | ... | | Value: 19 | Value: 8.95 | Value: 'A' | | +-------------------+-------------------+-------------------+-------------------+ ```


7. Code Example

#include <stdio.h>

int main() { int age = 19; float cgpa = 8.95f; char grade = 'A';

printf("Age: %d years\n", age); printf("CGPA: %.2f\n", cgpa); printf("Grade: %c\n", grade); return 0; } ```


8. Explain EVERY Line - int age = 19;: Reserves 4 bytes in RAM labeled age and assigns integer value 19. - %.2f: Format specifier printing floating point number rounded to 2 decimal places.


9. Memory Table

Address        Variable        Type         Value
--------------------------------------------------------
0x7ffe4a80     age             int          19
0x7ffe4a84     cgpa            float        8.95
0x7ffe4a88     grade           char         'A' (ASCII 65)

10. Common Mistakes - Trying to store a decimal number like 85.75 inside an int variable. The decimal portion .75 is truncated (lost)! - Forgetting single quotes around characters (char g = A; instead of char g = 'A';).


11. Viva Corner - Q: What is the size of int and float in standard 32/64-bit C compilers? - A: Both int and float consume 4 Bytes (32 bits) in RAM.


12. Practice - Easy: Declare variables for your roll number, height in meters, and section grade. - Medium: Calculate the area of a circle using float radius and float area. - Challenge: Write a program demonstrating integer truncation when dividing two integers.


13. Summary Today you learned how to select the perfect variable container type (int, float, char) for your data!

C Runner