Module 1: I troductio to C Progra i g
Act as a professional Engineering Professor. Transform the following text into a high-quality A4 Revision Study
Note. Use a clean "Academic" theme. Ensure all code is in syntax-highlighted blocks. Use tables for analogies and
"Callout" boxes for the "Common Confusion" section.
1. W at i a Progra ?
A program is a sequence of instructions executed by a computer in order.
Step 1 Boil water Take two numbers from user
Step 2 Add tea leaves Add them together
Step 3 Add milk & sugar Show the result
Step 4 Serve hot Stop
W y C?
C is a "Manual" language. Unlike Python (Automatic), C gives you direct control over memory and hardware, making it
the foundation for Data Structures and Operating Systems.
2. T e Executio Workflow
C code must be translated before the computer can run it.
1. Write: Create hello.c (Human-readable).
2. Compile: The GCC Compiler translates C into Machine Language (0s and 1s).
3. Run: The computer executes [Link] (Machine-readable).
3. A ato y of a C Progra
#include <stdio.h> // Link the Standard I/O toolbox
int main() // The starting point of execution
{ // Start of code block
printf("Hello!"); // Print text to screen; end with semicolon
return 0; // Signal successful completion
} // End of code block
Key Co po e t Explai ed:
#include <stdio.h>: Imports the "Standard Input Output" library. Required for printf and scanf.
int main(): The entry point. Execution always begins here.
{ } (Braces): Defines the scope (boundaries) of a function.
; (Semicolon): The "Full Stop" of C. Every statement must end with this, or the compiler will throw an error.
4. Co o Begi er Co fu io
Q: W y i t before ai ?
A: It specifies that the function returns an integer. return 0; matches this by returning zero to the OS.
Q: W at appe if I forget #i clude?
A: The compiler won't recognize printf(). It’s like trying to use a tool that isn't in your toolbox.
5. Practice Lab
Problem 1: Predict the Output
#include <stdio.h>
int main() {
printf("My name is Ravi.\n");
printf("I am learning C.");
return 0;
}
Problem 2: Spot the 3 Errors
#include <stdio.h>
int main() {
printf("Hello World") // Error 1
printf("I love coding!"; // Error 2
return 0 // Error 3
}
Problem 3: Escape Sequences
\n creates a new line. Predict the layout:
#include <stdio.h>
int main() {
printf("Line 1\nLine 2\nLine 3");
return 0;
}
Module 2: Variable Data Type
1. W at i a Variable?
A variable is a named memory location (a "slot") that stores a specific type of data.
Analogy: Think of a school bag with specific compartments for books, bottles, and pens. Each slot only fits its
intended item.
Syntax: int age = 20;
int (Type) | age (Name) | 20 (Value)
2. T e 4 Mai Data Type
int Whole numbers 2 or 4 bytes int x = 10; %d
float Decimals 4 bytes float p = 3.14; %f
char Single character 1 byte char g = 'A'; %c
double High-precision 8 bytes double d = 1.9999; %lf
decimals
3. Na i g Rule (Ide tifier )
C is strict about how you name your variables.
age, my_age 1age Cannot start with a [Link]
_marks total marks No spaces allowed.
count1 total-marks No hyphens (only underscores).
_temp int, float Cannot use Reserved Keywords.
⚠ Warning: If you declare a variable (e.g., int x;) but don't initialize it (x = 5;), it will contain "Garbage
Value"—random data that can crash your program.
4. For at Specifier Output
Format specifiers act as placeholders inside printf.
#include <stdio.h>
int main() {
int age = 20;
float price = 49.99;
char grade = 'A';
printf("Age: %d\n", age); // %d replaces with int
printf("Price: %.2f\n", price); // %.2f limits to 2 decimal places
printf("Grade: %c\n", grade); // %c replaces with char
return 0;
}
5. Variable Modificatio
Variables can be overwritten. The old value is erased when a new one is assigned.
int score = 10; // score is 10
score = 50; // score is now 50 (10 is gone)
score = score + 5; // score becomes 55
6. Practice Lab
Problem 1: Predict Output
int x = 5, y = 10;
int z = x + y;
printf("Sum = %d", z);
Problem 2: Debugging (Spot 3 Errors)
int 2age = 25;
float salary = 50000
char first letter = 'R';
Problem 3: Logic Test
int a = 10;
a = a + 5;
a = a * 2; // What is the final value of 'a'?
Key Takeaway: Variables are containers. Choose the right Data Type for your container and always use the correct
Format Specifier to display it.
Module 3: I put Output (pri tf ca f)
1. T e Co u icatio Co cept
Programs interact with users through the Screen (Output) and the Keyboard (Input).
printf (The Screen): Shows information to the user.
scanf (The Keypad): Collects data from the user and stores it in memory.
2. Deep Dive: pri tf (Output)
The number of % placeholders must exactly match the number of variables provided.
\n New Line (Enter) Moves text to next line
\t Tab Space Creates a large horizontal gap
\" Double Quote Prints " inside a string
For atti g Deci al Place
Use %.[number]f to control float precision:
%.2f → Displays 2 decimal places (e.g., 49.99).
%.0f → Displays no decimal places (rounds to nearest whole number).
3. Deep Dive: ca f (I put)
scanf requires the Address-of Operator (&) to find the correct "room" in memory to store the data.
T e A alogy
Without &: You tell a delivery man to "Deliver to Ravi." (He gets lost; Ravi is a name, not a location).
With &: You tell the delivery man to "Deliver to Room 101." (He finds the address and completes the task).
Pro-Tip: When scanning a character (%c), always put a space before the % (e.g., scanf(" %c", &grade);) to
skip any "Enter" keys left in the system from previous inputs.
4. Full I put/Output Progra
#include <stdio.h>
int main() {
int age;
float marks;
char grade;
printf("Enter age, marks, and grade: "); // Note the space before %c
scanf("%d %f %c", &age, &marks, &grade);
printf("\n--- Student Report ---\n");
printf("Age : %d\n", age);
printf("Marks : %.2f\n", marks);
printf("Grade : %c\n", grade);
return 0;
}
5. T e "Safety C eckli t" (Co o Mi take )
Missing & in scanf: This is the #1 cause of program crashes for beginners.
Format Mismatch: Using %d for a float or %f for an int will result in "garbage" data.
Printing Uninitialized Variables: Never printf a variable before you have stored something in it using scanf or an
assignment.
6. Practice Lab
Problem 1: Column Alignment
Predict the output of these tabs:
printf("Name:\tAli\n");
printf("Roll:\t101\n");
Problem 2: Debugging (4 Mistakes)
scanf("%f", a); // Error 1: ?
scanf("%d", &b); // Error 2 (if b is float): ?
printf("Enter a: ") // Error 3: ?
Problem 3: Rounding Logic
If float price = 99.9999;, what does printf("%.0f", price); display?
Key Takeaway: printf needs the Value, but scanf needs the Address (&).
Module 4: Operator Expre io
1. Arit etic Operator
Arithmetic operators perform mathematical calculations.
+ Addition a+b 13
- Subtraction a-b 7
* Multiplication a*b 30
/ Division a/b 3 (Int division drops
decimals)
% Modulus a%b 1 (The remainder)
D The % (Modulus) Trick:
n % 2 == 0 → The number is EVEN.
n % 2 == 1 → The number is ODD.
2. I cre e t (++) a d Decre e t (--)
Used to increase or decrease a variable by exactly 1.
Post-Increment (a++): "Use the value first, then increase it."
Pre-Increment (++a): "Increase the value first, then use it."
3. Relatio al Logical Operator
These operators return 1 for True and 0 for False.
Relatio al (Co pari o )
== (Equal to) | != (Not equal to)
> (Greater) | < (Less)
>= (Greater/Equal) | <= (Less/Equal)
⚠ The Deadly Mistake:
a = 5 → Assignment (Sets a to 5).
a == 5 → Comparison (Checks if a is 5).Always use == inside if statements!
Logical (Co bi i g Co ditio )
&& (AND) Both must be true (5>2 && 10>5) → True
|| (OR) Either can be true (5>2 || 10<5) → True
! (NOT) Reverse the state !(5>2) → False
4. Operator Precede ce (W o ru fir t?)
C follows a specific order of operations, similar to BODMAS in math.
1. (), ++, -- (Highest Priority)
2. *, /, %
3. +, -
4. <, >, ==, !=
5. &&, ||
6. =, +=, -= (Lowest Priority)
Example: int result = 2 + 3 * 4; → 3* 4 happens first, so result is 14, not 20.
5. Practice Lab
Problem 1: Math Logic
If int a = 15, b = 4;, find:
a/b=?
a%b=?
Problem 2: The "Shortcuts"
int x = 10;
x += 5; // x = ?
x *= 2; // x = ?
x -= 3; // x = ?
Problem 3: Pre vs Post Output
int a = 5;
printf("%d\n", a++); // Output: ?
printf("%d\n", ++a); // Output: ?
Key Takeaway: Use ( ) whenever you are unsure about precedence to ensure your calculations are correct.
Module 5: Co ditio (Deci io Maki g)
1. T e if a d el e State e t
Conditions allow a program to take different paths based on whether a statement is True (1) or False (0).
Ba ic Sy tax
if: Executes a block only if the condition is true.
if-else: Executes the first block if true, and the else block if false.
else if: Used to check multiple conditions in a sequence. It stops at the first true condition it finds.
2. Ne ted Co ditio
An if statement inside another if statement is used for multi-layered checks.
Example: First check if a user is 18+ (Age), then check if they have a Valid ID (ID check).
3. T e witc State e t
A cleaner alternative to multiple if-else blocks when comparing one variable against exact values.
Key Rule :
case: The specific value being checked.
break: CRITICAL. It stops the execution from "falling through" to the next case.
default: Runs if none of the cases match (similar to an else).
⚠ The "Falling Domino" Warning:
Without a break; keyword at the end of a case, C will continue to execute every case below it until it hits a
break or the end of the switch.
4. Acade ic Applicatio : Your Teac er’ Code
You can now understand the "Guard Conditions" and "Menu Logic" used in your [Link] IT labs:
Guard Conditions: if (*n >= MAX) checks if an array is full before trying to insert data. This prevents memory
errors.
Menu Logic: Your teacher uses a switch(choice) to handle user input (1 for Display, 2 for Insert, 3 for Delete).
Logic Checking ranges (e.g., marks >= Checking exact values (e.g.,
90) choice == 1)
Complexity Using complex && or || Simple equality checks
5. Practice Lab
Problem 1: Logic Flow
If x = 85, which block executes first in an if-else if chain checking >=90, >=75, and >=60?
Problem 2: The break Test
Predict the output for x = 2 if case 2 has no break but case 3 does:
switch (x) {
case 1: printf("One");
case 2: printf("Two"); // No break here!
case 3: printf("Three"); break;
}
Problem 3: The Assignment Trap
Identify the error in if (marks = 90). (Hint: Look back at Module 4’s "Deadly Mistake").
Key Takeaway: Conditions turn a static script into a dynamic program. Use if for ranges and switch for menus.