Variables, Data Types and Input/Output
Learn how C stores numbers and text in variables, picks the right data type, and talks to the user with printf() and scanf(). Then build your student information and calculator programs.
4-Hour Lesson Plan
| Time | Segment | Content | Hands-on |
|---|---|---|---|
| 0:00 | Recap and hook | A variable is a named box in memory. What happens when you type the wrong type in? | Variables (memory view) in the playground |
| 0:20 | Data types | int, float, double, char: size, range, specifier | Type Lab |
| 1:00 | Names and constants | Naming rules, const, #define | Name checker |
| 1:30 | printf and scanf | Format specifiers, width, precision, the & operator | Format Specifier Lab |
| 2:15 | Build lab | Student information, total and average, temperature, rectangle, calculator | 5 programs in the playground |
| 3:15 | Errors | Missing &, wrong specifier, integer division, overflow | Error and pitfall examples |
| 3:40 | Check | Quiz and checklist | Quick quiz |
Core Ideas
Variable
A named place in memory that holds a value, like int age = 20;. The declaration tells C the type and the name.
Data types
The data type decides what a variable can hold and how many bytes it uses: int, float, double, char.
Constants
A constant never changes. Use const for a protected variable or #define for a named value.
Format specifier
The % code in printf and scanf that says what type of value is being printed or read, like %d or %f.
Input and output
printf() shows output. scanf() reads input and needs the address operator & before the variable.
Data Types at a Glance
| Type | Holds | Typical size | Range or precision | printf | scanf | Example |
|---|---|---|---|---|---|---|
int | Whole numbers | 4 bytes | -2,147,483,648 to 2,147,483,647 | %d | %d | int age = 20; |
float | Decimals | 4 bytes | About 6-7 digits | %f | %f | float marks = 87.5; |
double | Precise decimals | 8 bytes | About 15-16 digits | %lf | %lf | double pi = 3.14159; |
char | One character | 1 byte | -128 to 127 (ASCII code) | %c | %c | char grade = 'A'; |
char name[30] | Text (string) | 30 bytes | Up to 29 characters plus end mark | %s | %s | char name[30]; |
Sizes are typical on common computers. Use sizeof(int) in a program to check yours.
Lab 1: Type Lab
Choose a type and type a value. See what C really stores and what printf shows. Try 3.99 as an int, 0.1 as a float, or A as a char.
Lab 2: Variable Names and Constants
Name checker
const | #define | |
|---|---|---|
| Example | const float PI = 3.14; | #define PI 3.14 |
| Has a type | Yes | No (text replacement) |
| Ends with ; | Yes | No |
| Uses = sign | Yes | No |
| Where | Inside or outside main() | Usually at the top |
| Change later | Error | Error |
Format Specifier Table
| Code | Meaning | Example value | Output |
|---|---|---|---|
%d | int | 42 | 42 |
%f / %.2f | float, 6 or 2 decimals | 3.14159 | 3.141590 / 3.14 |
%lf / %.2lf | double | 3.14159 | 3.141590 / 3.14 |
%c | char | 'A' | A |
%s | string | "Riya" | Riya |
%5d / %-5d | width 5, right / left | 42 | 42 / 42 |
%zu | result of sizeof | sizeof(int) | 4 |
%% | a percent sign | % |
Lab 3: Format Specifier Lab
Type a format string and the values (separated by commas, text in double quotes). The | bars show the exact width. Try %-8s|%5d|%.1f with "Riya", 42, 3.14159.
Lab 4: Calculators (check your answers)
Pick a calculator, change the numbers, and compare the results with what your C program prints.
Your Programs (Deliverable)
Each program shows the code, a sample input and the expected output. Download the file, compile it on your computer with gcc -Wall file.c -o file, and run it with ./file (Windows: file.exe).
Live C Playground: Auto-Run, Errors and Fixes
Every C program must start at main(). Type code below: it is colour-highlighted, checked and run as you type. Supports printf, scanf, int/float/double/char, const, #define, sizeof and basic maths. Use the Examples menu for the five programs and the error demos. Turn on Smart suggestions for autocomplete, and use Format code to tidy any code into a clean structure.
Smart suggestions are on. Click in the code, type pr and choose a suggestion: printf is filled in with the right % code for the type of your variable. Inside a string, type % to get the matching code. Press Ctrl+Space to show suggestions anywhere.
Terminal
When the program asks for input, type in the terminal and press Enter.Variables (memory view)
| Name | Type | Value | Bytes |
|---|
Problems and suggestions
Common Input and Format Errors
| Mistake | What happens | Fix |
|---|---|---|
scanf("%d", age); | Missing &: the program may crash or store nothing | Write &age |
printf("%d", price); with a float | Wrong or garbage number | Use %f or %.2f |
scanf("%f", &d); with a double | Wrong value stored | Use %lf for double |
total / 3 with ints | Decimal part is lost (7 / 2 gives 3) | Use total / 3.0 |
| Using a variable before setting it | Random garbage value | Give it a value or read it with scanf first |
char g = "A"; | Double quotes make text, not a char | Use single quotes: 'A' |
#define PI 3.14; | The ; becomes part of the value | No semicolon after #define |
scanf("%s", name) with spaces | Reads only the first word | Use one word, or fgets later |
| Number too large for int | Value wraps around (overflow) | Use long |
Resources, Tools and Reference Links
Links open in a new tab.