Theory
Three values, three shaped boxes
The marks program is growing and today it must store three things: marks 78, percentage 78.5, and grade 'A'.
Try pouring all three into int boxes and watch: 78 fits perfectly, 78.5 loses its .5, and 'A' becomes a number you never asked for.
Boxes have shapes. Choosing the shape is what a data type does, and choosing wrong is the quietest way for a C program to lie to you.
Theory
The dabba set
A kitchen dabba set has shapes for a reason: a flat one for rotis, a tall one for dal, a tiny one for elaichi. Pour dal into the roti box and you lose half of it. Data types are the shapes: int for whole numbers, float/double for values with decimals, char for a single character. The type also fixes the box's SIZE in bytes, which decides how big a value can fit.
At a glance
The C type family (typical sizes)
| Type | Bytes | Holds | In the marks program |
|---|---|---|---|
| short int | 2 | Small whole numbers | age |
| int | 4 | Whole numbers | marks = 78 |
| long | 4/8 | Big whole numbers | college enrolment no. |
| float | 4 | Decimals, ~6-7 digits | percentage = 78.5 |
| double | 8 | Decimals, ~15 digits | precise averages |
| char | 1 | ONE character (as ASCII) | grade = 'A' |
Theory
The special ones: string, void, signed/unsigned
- string: NOT a basic C type. Text like
"Riya"is a char array, a row of char boxes, written in double quotes. Full treatment comes in Unit 3; for now, know why 'A' and "A" differ. - void: the "no value" type, used for functions that return nothing. No variable is declared void.
- signed / unsigned: in a signed int, one bit stores the sign, leaving ~±214 crore. Declare
unsigned intand that bit joins the value: no negatives, double the positive reach (~429 crore). Perfect for counts that can never be negative.
Practical
The marks program, properly typed
#include <stdio.h>
int main() {
int marks = 78;
float percentage = 78.5;
char grade = 'A';
unsigned int total_students = 60;
printf("Marks: %d\n", marks);
printf("Percentage: %f\n", percentage);
printf("Grade: %c\n", grade);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
You declare int percentage = 78.5; What does the box actually hold?
- 78, the decimal part is cut off
- 78.5, C adjusts the box automatically
- 79, C rounds to the nearest whole number
- 0, the program refuses the value
Show the answer
78, the decimal part is cut off
An int box has no shelf for decimals, so C truncates: the .5 is dropped, not rounded, 78.9 would also become 78. No error, no warning in most compilers, just silent data loss, which is why type choice matters. "Truncation, not rounding" is the exact phrase exams reward.
Think first
The char secret you already know
A char box is 1 byte, and bytes hold numbers. So what number sits in the box when you store 'A'? You learned this in BCA103.
Show the answer
65, the ASCII code. char is really a small number box wearing a character costume: 'A' stores 65, 'a' stores 97, '0' stores 48. That is why grade + 1 is legal C and gives 66 ('B'). Your BCA103 ASCII table just became programming equipment.
Watch out
Where marks leak
Calling string a basic data type of C: it is a char array, and examiners check. Writing sizes as universal laws: they are typical (int is "usually 4 bytes", compiler-dependent), say so. Quoting wrong: 'A' is a char, "A" is a string of two bytes (A plus a hidden end-marker). And unsigned does not mean "no sign written": it means negatives are impossible for that box.
Theory
Choosing like a professional
The working rule: whole counts → int (unsigned if negatives are impossible), money and percentages → float, science-grade precision → double, one letter/symbol → char. Get the shapes right now, because the next lesson hands you the verbs, operators, and 78 / 60 behaves very differently in int boxes versus float boxes.
Summary
Key takeaways
- A data type fixes a box's size (bytes) and what values fit.
- Whole numbers: short int, int, long; decimals: float (~7 digits), double (~15).
- char holds ONE character, stored as its ASCII number; 'A' is 65.
- string is a char array (double quotes), not a basic type; void means no value.
- unsigned sacrifices negatives to double the positive range.
- int boxes truncate decimals: 78.5 becomes 78, no rounding.
- Memory hook: the dabba set, right shape for the right thing.