Part 3: Keywords and Advanced Programming Concepts (120 mins)
Topic 1: Keywords in C - The Language's Reserved Words
Definition: Keywords are predefined or reserved words that have special meanings to the C compiler. They are part of the language syntax and cannot be used as identifiers (variable names, function names, etc.).
⚠️ Important Rule
Keywords cannot be used as identifiers. The compiler will generate an error if you try to use them as variable names, function names, or any other user-defined elements.
Complete List of C Keywords (32 Keywords)
Data Type Keywords
Control Flow Keywords
Storage Class Keywords
Type Qualifiers & Others
Common Error: Using Keywords as Identifiers
#include <stdio.h>
int main() {
// ERROR: 'return' is a keyword, cannot be used as
variable name
int return = 10;
printf("%d\n", return);
return 0;
}
Compilation Error:
./Solution.c: In function 'main':
./Solution.c:4:9: error: expected identifier or '(' before
'return'
./Solution.c:5:20: error: expected expression before 'return'
Topic 2: Categorized Keywords with Examples
1. Data Type Keywords
Used to declare variables of specific types
#include <stdio.h>
int main() {
char letter = 'A';
int age = 25;
float price = 99.99;
double preciseValue = 3.14159265359;
short smallNumber = 100;
long bigNumber = 1000000L;
unsigned int positiveOnly = 40000;
signed int canBeNegative = -50;
printf("Char: %c, Int: %d, Float: %.2f\n", letter, age,
price);
return 0;
}
2. Control Flow Keywords
Used to control the flow of program execution
if-else Example
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
printf("Positive number\n");
} else if (number < 0) {
printf("Negative number\n");
} else {
printf("Zero\n");
}
return 0;
}
for loop Example
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration
4
Iteration 5
while loop Example
#include <stdio.h>
int main() {
int count = 1;
while (count <= 3) {
printf("Count: %d\n", count);
count++;
}
return 0;
}
3. Storage Class Keywords
Determine the scope, visibility, and lifetime of variables
auto (Default)
void demoFunction() {
auto int localVar = 10; // 'auto' is optional
(default)
printf("Local variable: %d\n", localVar);
}
static Example
#include <stdio.h>
void counter() {
static int count = 0; // Retains value between calls
count++;
printf("Count: %d\n", count);
}
int main() {
counter(); // Count: 1
counter(); // Count: 2
counter(); // Count: 3
return 0;
}
extern Example
// File1.c
int globalVar = 100; // Global variable definition
// File2.c
extern int globalVar; // Declaration (defined in another file)
void display() {
printf("Global variable: %d\n", globalVar);
}
4. Type Qualifiers & Utility Keywords
const Example
#include <stdio.h>
int main() {
const float PI = 3.14159;
const int MAX_SIZE = 100;
// PI = 3.14; // ERROR: cannot modify const variable
printf("PI value: %.2f\n", PI);
return 0;
}
sizeof Example
#include <stdio.h>
int main() {
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n",
sizeof(double));
printf("Size of char: %zu bytes\n", sizeof(char));
int arr[10];
printf("Size of array: %zu bytes\n", sizeof(arr));
return 0;
}
Topic 3: Control Flow in Depth
Switch-Case Statement
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch(day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day!\n");
}
return 0;
}
Nested Control Structures
#include <stdio.h>
int main() {
int number, i, j;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
for(i = 1; i <= number; i++) {
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
} else {
printf("Please enter a positive
number.\n");
}
return 0;
}
Topic 4: Difference Between Keywords and Identifiers
Feature | Keywords | Identifiers |
---|---|---|
Definition | Reserved words with specific meaning in C syntax | User-defined names for variables, functions, etc. |
Usage | Part of language grammar, cannot be changed | Created by programmer as needed |
Examples |
int , return , if ,
while
|
age , calculateTotal ,
studentName
|
As Variable Names | ❌ Cannot be used | ✅ Can be used (if not a keyword) |
Modification | Fixed by language specification | Can be defined, redefined, and reused |
Purpose | Define program structure and control flow | Identify and reference program elements |
Topic 5: Advanced Programming Examples
Example 1: Number Classification Program
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// Check multiple conditions
if (number == 0) {
printf("The number is zero.\n");
} else {
if (number > 0) {
printf("Positive");
} else {
printf("Negative");
}
if (number % 2 == 0) {
printf(" and even.\n");
} else {
printf(" and odd.\n");
}
}
return 0;
}
Example 2: Simple Calculator using Switch
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch(operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 /
num2;
} else {
printf("Error!
Division by zero.\n");
return 1;
}
break;
default:
printf("Error! Invalid
operator.\n");
return 1;
}
printf("Result: %.2lf %c %.2lf = %.2lf\n", num1,
operator, num2, result);
return 0;
}
Example 3: Prime Number Checker
#include <stdio.h>
int main() {
int num, i, isPrime = 1; // Assume prime initially
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n",
num);
}
return 0;
}
Part 6: Hands-On Practice Exercises
Exercise 1: Fibonacci Series Generator
Problem: Write a program to generate the first N numbers of the Fibonacci series
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
Formula: Each number is the sum of the two preceding ones
Exercise 2: Factorial Calculator
Problem: Calculate the factorial of a number using loops
Formula: n! = n × (n-1) × (n-2) × ... × 1
Example: 5! = 5 × 4 × 3 × 2 × 1 = 120
Exercise 3: Number Guessing Game
Problem: Create a game where the computer generates a random number and the user has to guess it
Features:
- Generate random number between 1-100
- Give hints ("Too high", "Too low")
- Count number of attempts
- Ask to play again
Part 7: Common Errors and Debugging Tips
Error 1: Missing break in switch statement
switch(choice) {
case 1:
printf("One");
// Missing break - will fall through to case
2!
case 2:
printf("Two");
break;
}
Error 2: Infinite loops
int i = 0;
while (i < 5) {
printf("%d\n", i);
// Forgot to increment i - infinite loop!
}
Error 3: Using = instead of == in conditions
int x = 5;
if (x = 10) { // Assignment, not comparison!
printf("This will always execute\n");
}
Part 8: Summary and Key Takeaways
What We Covered Today:
- Keywords: All 32 reserved words and their categories
- Control Flow: if-else, switch-case, loops (for, while, do-while)
- Storage Classes: auto, static, extern, register
- Advanced Programming: Practical examples with multiple control structures
- Error Handling: Common mistakes and debugging techniques
Key Rules to Remember:
- Keywords cannot be used as identifiers
- Always use break in switch cases (unless intentional fall-through)
- Use == for comparison, = for assignment
- static variables retain their value between function calls
- const variables cannot be modified after initialization
Next Session Preview:
In our next class, we'll explore:
- Arrays and strings
- Functions and parameter passing
- Pointers and memory management
- More complex data structures
💡 Pro Tip: Memory Aid for Keywords
Remember this sentence: "Can I Forget Long Double Strings?" - The first letters match common data type keywords: char, int, float, long, double, short.
📋 Quick Reference: Most Used Keywords
int
- Integer variablesfloat
- Floating-point numberschar
- Character dataif/else
- Conditional executionfor
- Counting loopswhile
- Conditional loopsreturn
- Function returnvoid
- No type/return value