Practical Programming Manual for
Beginners: Learning C Fundamentals
with Code::Blocks
SECTION A: Getting Started
Chapter 1: Introduction to C Programming
Objectives:
- Understand the history and structure of C.
- Learn how to write and run your first C program.
Theory:
- C is a general-purpose programming language developed by Dennis Ritchie.
- It is structured, statically typed, and widely used in system and application development.
Sample Program:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Exercise 1.1:
Write a program to print your name, your course, and your favorite hobby.
Solution:
#include <stdio.h>
int main() {
printf("Name: John Doe\n");
printf("Course: Computer Science\n");
printf("Hobby: Playing Chess\n");
return 0;
}
[Date]
1
SECTION B: C Basics
Chapter 2: Data Types and Variables
Theory:
- int, float, char, double
- Variables store data used in a program.
Example:
int age = 20;
float height = 5.9;
char grade = 'A';
Exercise 2.1:
Declare and initialize variables for your age, GPA, and first initial. Print them.
Solution:
#include <stdio.h>
int main() {
int age = 18;
float gpa = 3.75;
char initial = 'J';
printf("Age: %d\nGPA: %.2f\nInitial: %c\n", age, gpa, initial);
return 0;
}
SECTION C: Input and Output
Chapter 3: Using scanf and printf
Example:
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("You entered: %d\n", number);
Exercise 3.1:
Ask the user to input their name and age, and then display it.
Solution:
[Date]
2
#include <stdio.h>
int main() {
char name[50];
int age;
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("Hello %s, you are %d years old.\n", name, age);
return 0;
}
SECTION D: Control Structures
Chapter 4: If-Else Statements
Example:
int number = 10;
if (number > 0) {
printf("Positive number");
} else {
printf("Non-positive number");
}
Exercise 4.1:
Ask the user for a number and determine if it is even or odd.
Solution:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even number\n");
else
printf("Odd number\n");
return 0;
[Date]
3
SECTION E: Looping Constructs
Chapter 5: For and While Loops
For Loop Example:
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
Exercise 5.1:
Write a program to calculate the factorial of a number using a for loop.
Solution:
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}
SECTION F: Functions
Chapter 6: Creating and Using Functions
Example:
void greet() {
printf("Hello from function!\n");
}
int main() {
greet();
return 0;
}
[Date]
Exercise 6.1:
Write a function that adds two numbers and returns the result.
4
Solution:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 5);
printf("Sum: %d\n", result);
return 0;
}
SECTION G: Arrays and Strings
Chapter 7: Arrays and Basic String Handling
Array Example:
int numbers[5] = {1, 2, 3, 4, 5};
String Example:
char name[] = "Alice";
printf("Name: %s\n", name);
Exercise 7.1:
Read 5 integers into an array and find the largest.
Solution:
#include <stdio.h>
int main() {
int arr[5], max;
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
max = arr[0];
for (int i = 1; i < 5; i++) {
if (arr[i] > max)
[Date]
max = arr[i];
5
}
printf("Largest number: %d\n", max);
return 0;
}
Appendix
Installing Code::Blocks:
1. Visit www.codeblocks.org
2. Download “Code::Blocks with MinGW setup.”
3. Install with default options.
4. Open Code::Blocks, go to File > New > Project > Console Application > C.
5. Start coding!
C Keywords Cheat Sheet:
int, float, char, return, if, else, for, while, do, switch, case, break, continue, void
Common Errors and Debugging Tips:
- Missing semicolons
- Unmatched braces
- Using undeclared variables
- Mismatched format specifiers in printf/scanf
Extra Practice Questions:
1. Write a program to reverse a number.
2. Write a program to check for prime numbers.
3. Write a program to find the sum of digits of a number.
Sample Mini Projects:
- Simple calculator
- Student grading system
- Quiz application
[Date]
6
Sample Mini Projects
1. Simple Calculator
#include <stdio.h>
int main() {
char op;
float num1, num2;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &op);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch(op) {
case '+': printf("Result: %.2f\n", num1 + num2); break;
case '-': printf("Result: %.2f\n", num1 - num2); break;
case '*': printf("Result: %.2f\n", num1 * num2); break;
case '/':
if (num2 != 0)
printf("Result: %.2f\n", num1 / num2);
else
printf("Error! Division by zero.\n");
break;
default: printf("Invalid operator.\n");
}
return 0;
}
2. Student Grading System
Input marks and print corresponding grade.
#include <stdio.h>
int main() {
int marks;
printf("Enter student's marks (0–100): ");
scanf("%d", &marks);
[Date]
if (marks >= 70) printf("Grade: A\n");
else if (marks >= 60) printf("Grade: B\n");
7
else if (marks >= 50) printf("Grade: C\n");
else if (marks >= 45) printf("Grade: D\n");
else printf("Grade: F\n");
return 0;
}
3. Simple Quiz Application
Ask multiple-choice questions and display score.
#include <stdio.h>
int main() {
char answer;
int score = 0;
printf("Q1: What is the capital of France?\n");
printf("a) Berlin\nb) Madrid\nc) Paris\nd) Rome\n");
printf("Your answer: ");
scanf(" %c", &answer);
if (answer == 'c' || answer == 'C') score++;
printf("\nQ2: 2 + 2 = ?\n");
printf("a) 3\nb) 4\nc) 5\nd) 6\n");
printf("Your answer: ");
scanf(" %c", &answer);
if (answer == 'b' || answer == 'B') score++;
printf("\nYou scored %d out of 2\n", score);
return 0;
}
[Date]