Module 1 – Structured Programming (C
Language) Notes
*## Module 1 – Structured Programming (C Language)
(Mixed: Detailed Explanation + Exam Emphasis + Coding Practice)
Arranged in the same order we learned in the chat
1. Structure of a C Program
A basic C program always includes: - Header files (for library functions) - main() function (starting
point of execution) - Statements (code logic) - return statement (end of program)
Syntax:
#include <stdio.h> // header file
int main() { // starting point
printf("Hi"); // program body
return 0; // program ends
}
Why must every C program have main()?
• The OS starts execution from main() .
• The compiler builds the program, but the operating system decides where execution begins.
Exam Note:
The main() function is the entry point of every C program.
Practice Questions:
1. Write a C program that prints your name.
2. Modify the basic structure to also print your age.
3. Write a program that prints two lines using two printf statements.
2. Compiler vs Interpreter
Compiler:
• Translates the entire program at once into machine code.
1
• Produces an executable file.
• Faster execution.
Interpreter:
• Translates code line by line.
• No executable file.
• Slower execution.
Exam Differences:
Compiler Interpreter
Whole program at once Line by line
Creates executable No executable
Errors after compilation Errors during execution
Faster Slower
Practice:
Identify whether the following languages are compiled or interpreted: - Python → ? - C → ? - Java → ?
(trick: hybrid)
3. Variables & Naming Rules
A variable is a named memory location.
Rules for valid variable names:
✔ Must start with letter or underscore ✔ Can contain letters, digits, underscore ❌ Cannot start with
digit ❌ Cannot contain special characters like -, @, # ❌ Cannot use keywords (int, for, return, etc.)
Examples:
Valid:
int number1;
int _count;
int Age;
Invalid:
int total-cost; // '-' not allowed
int 7value; // cannot start with digit
int return; // keyword
2
Practice:
State whether the following are valid or invalid: 1. _value 2. [Link] 3. marks1 4. double
4. Data Types & Sizes
Basic Data Types:
Type Size
char 1 byte
int 4 bytes
float 4 bytes
double 8 bytes
Exam Note:
Data types tell the compiler how much memory to allocate and what kind of data the
variable will store.
Memory Diagram:
char c; // 1 byte
---------
| X |
---------
int x; // 4 bytes
---------------------
| X | X | X | X |
---------------------
5. ASCII System
C stores characters as numbers internally using ASCII.
Common ASCII values:
Character ASCII
'A' 65
'a' 97
3
Character ASCII
'0' 48
space 32
Examples:
char c = 65;
printf("%c", c); // prints A
printf("%d", 'A'); // prints 65
Practice:
1. What does printf("%c", 97); print?
2. What is ASCII of 'B'?
3. Convert 'A' to lowercase using ASCII trick.
6. Lvalues & Rvalues
Lvalue: "Location Value"
• Has a memory address.
• Can appear on left side of = .
Examples:
x = 10;
arr[2] = 5;
*ptr = 20;
Rvalue: “Right-side Value”
• Temporary values.
• Cannot appear on left of assignment.
Examples:
10
x + y
7 * 3
Example:
4
arr[2] = a + b;
- Lvalue → arr[2] - Rvalue → a + b
7. Operator Precedence
C evaluates operators in this order:
1. ()
2. * / %
3. + -
4. < > <= >=
5. == !=
6. &&
7. ||
8. =
Example:
10 + 2 * 3 → 10 + (2 * 3) → 16
Practice:
Evaluate:
10 + 20 * 3 - 4 / 2
Use precedence + left-to-right rule.
8. Operator Associativity
Determines direction when precedence is same.
Left to Right:
• + - * / % < > == != && ||
Right to Left:
• =
• +=, -=, *=
• unary ++x , --x
Example:
5
a = b = c = 5;
Right-to-left:
a = (b = (c = 5));
9. Type Conversion (Implicit & Explicit)
When two data types meet in an expression, C converts both to the higher-precision type.
Hierarchy:
char → int → float → double → long double
Examples:
5 + 2.5 → float
7 + 3.0 → double
'A' + 2 → int
Explicit Conversion (Casting):
(float)10 / 4; // 2.5
10. Control Structures (if-else)
Syntax:
if (condition) {
// runs if true
} else {
// runs if false
}
Example:
int x = 5;
if (x > 10)
6
printf("Big");
else
printf("Small");
Output: Small
Else-if Chain:
if (a > b)
printf("A");
else if (a == b)
printf("Equal");
else
printf("B");
11. Loops (for, while, do-while)
for loop: Best when iterations are known.
for (int i = 0; i < 5; i++)
printf("%d", i);
while loop: Runs while condition is true.
while (x > 0)
x--;
do-while loop: Runs at least once.
do {
printf("Hello");
} while (0);
Practice:
1. Predict output:
int i = 0;
do {
printf("%d", i);
i++;
} while (i < 0);
7
Answer: 0
Mistakes You Made (With Corrections)
Understanding your mistakes is the fastest way to improve. Below are the mistakes you made in
Module 1 topics along with the corrected explanations.
✅ Mistake 1: Thinking output of code with char b = a; was a
instead of A
Your answer: a
Correct: A
Why you were wrong: - ASCII of 'A' = 65 - ASCII of 'a' = 97 - You saw char b = 65; but
thought it's lowercase.
Correct logic: - %c prints character corresponding to ASCII value. - 65 → 'A' (NOT 'a' ).
✅ Mistake 2: Believing 10 + 20 happens before 20 * 3
Your answer: You evaluated addition first.
Correct: Multiplication happens first.
Why: Operator precedence:
* / % → before + -
So:
10 + 20 * 3 → 10 + 60 → 70
✅ Mistake 3: Wrong order of evaluation in precedence question
You wrote:
4/2
20*3
8
10+20
3-4
Correct order:
1) 20 * 3
2) 4 / 2
3) 10 + (result)
4) (result) - (result)
Because same precedence ops evaluate left→right.
✅ Mistake 4: Wrong answer for if-else chain
You answered A for:
if (a > b) → 10 > 20 (false)
else if (a == b) → 10 == 20 (false)
else → B
Correct output: B
✅ Mistake 5: Believing result of 4 + 3.0 is float
Correct: double
Why: - Decimal without f = double. - int + double → double.
Extra Easy → Tough Questions (For Each Topic)
These are additional practice problems to strengthen your understanding.
✅ 1. Structure of C Program – Practice
Questions
Easy
1. Write a program to print your name.
2. Write a program to print two lines.
9
Medium
1. Write a program to print:
Hello
C Programming
using two printf statements.
Tough
1. Without using semicolon, print "Hello". (Trick question: use if , switch , or loops.)
✅ 2. Compiler vs Interpreter – Practice
Questions
Easy
1. Is Python interpreted or compiled?
2. Does C create an executable file?
Medium
1. Write two differences between compiler and interpreter.
Tough
1. Explain why compiled languages are generally faster.
✅ 3. Variables & Data Types – Practice Questions
Easy
1. Declare an int, float, char variable.
2. Which one is valid? _age , 2value , double , marks_1
Medium
1. Predict output:
char c = 'A' + 32;
printf("%c", c);
10
Tough
1. Write a program to swap two numbers without using a third variable.
✅ 4. ASCII – Practice Questions
Easy
1. Print ASCII of 'Z' .
2. What is ASCII of '0' ?
Medium
1. Convert A to a using ASCII trick.
2. Print characters from A to Z using ASCII in a loop.
Tough
1. Write a program to check if a character is uppercase without using ctype.h . Hint: ASCII range.
✅ 5. Lvalues & Rvalues – Practice Questions
Easy
Identify lvalue and rvalue:
x = y + 2;
Medium
Which one is invalid?
10 = x;
Why?
Tough
Explain:
*(p + 2) = 10;
Why is this valid?
11
✅ 6. Operator Precedence – Practice Questions
Easy
Evaluate mentally:
2 + 3 * 4
Medium
Predict output:
printf("%d", 10 > 5 + 2);
Tough
Evaluate:
10 + 20 / 5 * 3 - 2
Step-by-step.
✅ 7. Associativity – Practice Questions
Easy
What is the associativity of + ?
Medium
Predict:
a = b = c = 2;
printf("%d", a);
Tough
Explain:
12
int x = 10, y = 20, z = 30;
printf("%d", x < y < z);
(This is a classic trick question.)
✅ 8. Type Conversion – Practice Questions
Easy
What is type of 7.0 ?
Medium
Predict:
printf("%f", 5 / 2.0);
Tough
Explain:
printf("%f", (float)(5/2));
Why does this output 2.000 instead of 2.500?
Next Section Coming in Notes:
Arrays (1D & 2D), Strings, Functions, Call by Value/Reference,
Recursion, Storage Classes.
(Will be added when you say “continue notes” or “add arrays section”.)**
Arrays (1D & 2D), Strings, Functions, Call by Value/Reference,
Recursion, Storage Classes.
(Will be added in next update upon request.)
13