C Programming Basics – A Beginner’s Guide
Introduction
C is one of the oldest and most powerful programming languages, created by Dennis Ritchie in 1972.
It is often called the “mother of programming languages” because many modern languages (C++, Java, Python) are influenced by it.
Why learn C?
It teaches programming fundamentals.
It is fast and efficient.
It gives you control over hardware.
Characteristics of C Language
Simple and structured – Easy to understand, programs are modular.
Portable – Same C program can run on different machines.
Middle-level language – Combines features of high-level (easy to code) and low-level (close to hardware).
Compiled language – Code is converted into machine code by a compiler.
Rich library – Provides many built-in functions.
Structure of a C Program
Every C program follows a standard structure:
#include <stdio.h> // Header file
int main() { // Main function
printf("Hello, World!"); // Output statement
return 0; // Exit program
}Explanation:
#include <stdio.h>→ Header file that allows input/output functions.int main()→ Starting point of every C program.printf()→ Function to display output.return 0;→ Ends the program successfully.
Basic C Concepts
Variables and Data Types
Variables are used to store data. Each has a type.
int age = 20; // Integer
float marks = 85.5; // Decimal number
char grade = 'A'; // CharacterInput and Output
Output:
printf()Input:
scanf()
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age); // User input
printf("You are %d years old", age);
return 0;
}Operators in C
Arithmetic:
+ - * / %Relational:
== != > < >= <=Logical:
&& || !
Example:
int a = 10, b = 20;
printf("%d", a + b); // Output: 30Control Statements
If-Else
if(age >= 18) {
printf("You can vote");
} else {
printf("Not eligible to vote");
}Switch
switch(day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid day");
}Loops in C
For Loop
for(int i = 1; i <= 5; i++) {
printf("%d ", i);
}Output: 1 2 3 4 5
While Loop
int i = 1;
while(i <= 5) {
printf("%d ", i);
i++;
}Do-While Loop
int i = 1;
do {
printf("%d ", i);
i++;
} while(i <= 5);Functions in C
Functions break programs into smaller parts.
#include <stdio.h>
void greet() {
printf("Hello, welcome to C programming!");
}
int main() {
greet();
return 0;
}Example Program – Find Sum of Two Numbers
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}Input: 5 7
Output: Sum = 12
Advantages of C Programming
Fast execution
Portable across systems
Foundation for other languages
Powerful with memory management
Limitations of C
No built-in support for Object-Oriented Programming (OOP).
Manual memory management can be complex.
Programs can be error-prone without proper handling.
Quick Recap (Cheat Sheet)
C is the foundation language for programming.
Programs always start with
#include <stdio.h>andmain().Variables store data (int, float, char).
Control structures: if-else, switch.
Loops: for, while, do-while.
Functions: help in modular programming.