Introduction to C Programming
C is a powerful, general-purpose programming language developed by Dennis Ritchie in
1972 at Bell Laboratories.
It was originally created to design system software such as operating systems and
compilers, particularly for UNIX.
Over the years, C has become the foundation for many modern programming languages,
including C++, Java, and Python.
It provides a fine balance between high-level readability and low-level hardware access,
making it ideal for learning programming fundamentals.
Features of C Language
1. Simple and Efficient – C is easy to learn and its syntax is clear. It allows programmers
to write efficient and optimized code.
2. Structured Language – C supports modular programming by dividing programs into
small, reusable functions.
3. Portable – A C program written on one machine can be run on another with minimal
changes, making it highly portable.
4. Rich Library – C offers a large number of built-in functions and operators that make
development faster.
5. Low-Level Access – C allows direct manipulation of hardware components through
pointers, making it close to assembly language.
6. Memory Management – Functions like malloc(), calloc(), realloc(), and free() allow
dynamic memory management.
7. Fast Execution – As a compiled language, C programs execute quickly and efficiently.
Basic Structure of a C Program
Every C program follows a specific structure that includes preprocessor directives, the
main() function, statements, and return statements.
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Explanation:
• #include <stdio.h> includes the Standard Input Output library that enables the use of
printf() and scanf().
• main() is the entry point of every C program.
• printf() displays the output on the screen.
• return 0 indicates successful program execution.
Basic C Programs
1. Basic C Program
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Explanation:
This is the simplest C program. It starts execution from the main() function and uses
printf() to display a message.
Every C program must have a main() function, and return 0 indicates that the program
ended successfully.
2. Program using if-else
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0)
printf("The number is even.");
else
printf("The number is odd.");
return 0;
}
Explanation:
The program takes a number from the user and checks whether it is even or odd using the
if-else statement.
If the remainder after dividing by 2 is 0, it prints "even"; otherwise, "odd".
3. Program using if-else-if ladder
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 90)
printf("Grade: A");
else if (marks >= 75)
printf("Grade: B");
else if (marks >= 50)
printf("Grade: C");
else
printf("Grade: Fail");
return 0;
}
Explanation:
This program assigns grades based on marks using an if-else-if ladder. Each condition is
checked in sequence until one is true.
This structure helps make multiple decisions efficiently.
4. Program using nested if-else
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c)
printf("%d is the largest.", a);
else
printf("%d is the largest.", c);
} else {
if (b > c)
printf("%d is the largest.", b);
else
printf("%d is the largest.", c);
}
return 0;
}
Explanation:
Nested if-else allows checking conditions inside another if statement.
Here, it finds the largest among three numbers by comparing them in nested conditions.
5. Program using for loop
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
Explanation:
The for loop is used when the number of iterations is known.
This program prints numbers from 1 to 5 using a loop with initialization, condition, and
increment parts.
6. Program using do-while loop
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
Explanation:
The do-while loop executes its statements at least once before checking the condition.
Even if the condition is false initially, the loop runs once.
7. Program using switch statement
#include <stdio.h>
int main() {
int choice;
printf("Enter a number between 1 and 3: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You chose One.");
break;
case 2:
printf("You chose Two.");
break;
case 3:
printf("You chose Three.");
break;
default:
printf("Invalid choice!");
}
return 0;
}
Explanation:
The switch statement is used for multi-way branching based on a variable's value.
Each case represents a possible value, and the default case executes if none of them
match.