⚙️ C programming basics · Class 10 · Lesson 1 of 18 · Free
C Fundamentals – printf, Variables, scanf, Operators, if/else, Loops and Functions
⏱ About 40 minutes · ▶ run real C here — nothing to install · no sign-in
In this lesson you will
- Write, run and read a complete C program with printf and scanf
- Store data in int, double and char variables and use C's operators correctly
- Use if/else, for and while loops, and write your own functions in C
Your school's report card software must add marks, find averages and print grades for hundreds of students in a second. Programs like that are often written in C, a language that runs very close to the computer's hardware. In this lesson you write your first real C programs, from printing a line to writing your own function.
What does a C program look like?
C is a compiled language. A program called a compiler turns your C code into machine code first. Then the computer runs that machine code. This is why C programs are very fast.
Every C program starts running at a function called main. The line #include <stdio.h> brings in the standard input/output library, which gives you printf. printf prints text, and \n inside the text means "go to a new line".
Every statement ends with a semicolon (;). return 0; tells the system that the program finished without problems.
Try it: add a third printf line with your school's name and press ▶ Run again.
Namaste! Welcome to C. Class 10 coding starts today.
💡 int main(void) means: main takes nothing and gives back a whole number (0 = success).
How do variables and data types work in C?
A variable is a named box that holds a value. In C you must say the type of the box before you use it. The type decides what the box can hold and how much memory it takes.
int holds whole numbers like 87. double holds decimal numbers like 86.5. char holds one character like 'A', written in single quotes. float is a smaller, less exact decimal type; prefer double.
printf needs a format code for each value: %d for int, %f for double (%.1f means one digit after the point), %c for char. sizeof tells you how many bytes a type uses.
Try it: change marks to 95 and grade to 'O', then run again.
Marks: 87 Percentage: 86.5% Grade: A An int uses 4 bytes here. A double uses 8 bytes here. A char uses 1 byte here.
💡 %% prints a real % sign. Sizes are for this compiler; most computers today give the same answers.
How do you take input from the user in C with scanf?
scanf reads what the user types. Its format codes are like printf's, with one change: for a double you write %lf (long float) in scanf, but %f in printf.
You must put & before the variable name, as in &plates. & gives scanf the address (memory location) of the box, so scanf can fill it. Lesson 2 explains addresses fully.
Always print a prompt first, so the user knows what to type. This program reads a tiffin order and prints the bill.
Try it: change the inputs to 4 and 65.5 and run again.
How many tiffin plates? 3 Price of one plate in Rs: 60 Total bill: Rs 180.00
💡 The typed values (3 and 60) appear in the output, just like on a real terminal.
Which operators does C have, and why is 7 / 2 equal to 3?
C has + - * / for maths. When both numbers are int, / gives a whole number and throws away the decimal part. So 7 / 2 is 3, not 3.5. % gives the remainder: 7 % 2 is 1.
To get a decimal answer, make one side a double, for example (double) total / 3. This is called a cast.
Comparisons (> < >= <= == !=) give 1 for true and 0 for false. && means AND, || means OR, ! means NOT. Shortcuts: x += 5 adds 5 to x, and x++ adds 1.
Try it: change balls to 100 and run again. Watch the extra balls and the two run rates.
Overs played: 20 Extra balls: 0 Run rate (int /): 12 Run rate (double): 12.25 Is it 200 or more? 1 Over 200 AND under 300? 1 After a four: 249 runs in 121 balls
💡 C has no true/false keywords by default: 0 means false, any other number means true.
How do if/else, for and while loops work in C?
If you learnt Python or JavaScript, these will feel familiar. In C, the condition goes inside round brackets ( ), and the block goes inside curly braces { }.
A for loop has three parts: start (int i = 1), keep-going test (i <= 5) and step (i++). A while loop repeats as long as its condition is true.
This program gives a grade with if / else if / else, prints a times table with for, and counts down with while.
Try it: change marks to 58 and the table number to 7, then run again.
Grade A2 9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 Exam starts in 3... Exam starts in 2... Exam starts in 1... Start writing!
💡 C also has do { ... } while (condition); which always runs the block at least once.
How do you write your own function in C?
A function is a named block of code you can call again and again. In C, you write the return type first, then the name, then the parameters with their types: double average(int a, int b, int c).
void means the function returns nothing. A function must be declared before main uses it. Here we write the functions above main, so C already knows them.
return sends a value back to the caller. Each call works on its own copies of the arguments.
Try it: add a call to print_report for a third student and run again.
Riya: total 259, average 86.33 Aman: total 216, average 72.00
💡 const char *name is how C passes text to a function. Lessons 2 and 3 explain the * and strings.
🤔 Predict the output
Think like C: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
int total = 250;
printf("%d\n", total / 3);Question 2. What does this print?
int a = 17;
printf("%d %d\n", a % 5, a > 20);Question 3. What does this print?
for (int i = 1; i <= 10; i += 3) {
printf("%d ", i);
}
printf("\n");🧩 Your challenge
Write a report card program. Read marks of 3 subjects with scanf. Write a function average() that returns the average as a double, and a function grade() that returns 'A' for 75 or more, 'B' for 50 or more, else 'C'. For inputs 78, 85 and 92 the output ends with: Average: 85.00 Grade: A
Maths marks: 78 Science marks: 85 English marks: 92 Average: 85.00 Grade: A
⚠️ Common mistakes (and how to fix them)
❌ Wrong
int marks = 90
printf("%d\n", marks);Line 1: error: expected ';' at end of declaration
✅ Right
int marks = 90;
printf("%d\n", marks);Every C statement must end with a semicolon. The compiler stops and points to the line where it expected the ;.
❌ Wrong
int marks = 72;
if (marks = 100) {
printf("Full marks!\n");
} else {
printf("Marks: %d\n", marks);
}Compiles and runs, but prints: Full marks! ⏎
✅ Right
int marks = 72;
if (marks == 100) {
printf("Full marks!\n");
} else {
printf("Marks: %d\n", marks);
}= puts 100 into marks, and 100 counts as true, so the wrong branch runs. Use == to compare.
❌ Wrong
int total = 250;
double avg = total / 3;
printf("%.2f\n", avg);Compiles and runs, but prints: 83.00 ⏎
✅ Right
int total = 250;
double avg = total / 3.0;
printf("%.2f\n", avg);total / 3 is int division, so the decimals are lost BEFORE the result goes into the double. Divide by 3.0 instead.
📌 Remember
- A C program starts at main; #include <stdio.h> gives you printf and scanf.
- Every variable has a type: int for whole numbers, double for decimals, char for one character.
- printf uses %d, %f, %c; scanf needs & before the variable and %lf for a double.
- int / int drops the decimal part; use 3.0 or a (double) cast for a decimal answer.
- if/else and for/while loops look like JavaScript; conditions go in ( ) and blocks in { }.
- A function has a return type, a name and typed parameters; void means it returns nothing.
Finished the quiz and the challenge?
Questions students ask
Is C still used in 2026?
Yes. Operating system kernels such as Linux, device drivers, and code in small devices like washing machines and car parts are often written in C. Many other languages, like Python, are themselves built in C. Learning C also shows you how memory really works.
What is the difference between printf and scanf in C?
printf prints values to the screen. scanf reads values the user types. Both use format codes like %d, but scanf needs the address of the variable, so you write &marks.
Why does C need data types when Python does not?
C works close to the hardware. The compiler must know how many bytes each variable needs and how to read those bytes. This makes C programs fast, but you must choose types yourself.
What is the difference between float and double in C?
Both hold decimal numbers. A double uses 8 bytes and keeps about 15 correct digits; a float uses 4 bytes and keeps about 6–7. Use double unless you have a reason to save memory.