Basics of C Programming – A Beginner’s Guide
Welcome to the first article of this C programming series! Whether you're a student, a coding enthusiast, or preparing for technical interviews, understanding C lays a strong foundation for mastering any other language. This post is designed as a complete 1.5-hour lecture on the fundamentals of C programming.
🔰 Introduction to C Programming
- Language: C is a procedural, general-purpose programming language developed by Dennis Ritchie in 1972.
- Uses: Operating systems, embedded systems, compilers, games, system-level programming.
- Why Learn C: It's fast, memory-efficient, and builds a solid base for other languages like C++, Java, and Python.
🧱 Structure of a C Program
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
#include <stdio.h>
– Standard Input Output library.main()
– Entry point of any C program.printf()
– Prints output to the screen.return 0;
– Exits the program successfully.
💾 Variables, Data Types, and Constants
- Variables store data values:
int age = 21;
- Data Types:
Data Type | Description | Example |
---|---|---|
int | Integer | int a = 5; |
float | Decimal | float pi = 3.14; |
char | Character | char ch = 'A'; |
double | Large decimal | double d = 3.141592; |
- Constants:
const float PI = 3.14;
– Value cannot change.
➕ Operators and Expressions
Operators perform operations on variables.
- Arithmetic:
+ - * / %
- Relational:
== != > < >= <=
- Logical:
&& || !
int x = 10, y = 20;
if (x < y && y > 0) {
printf("Valid condition");
}
🔁 Control Structures
If-Else
if (a > b) {
printf("A is greater");
} else {
printf("B is greater");
}
Switch
switch(choice) {
case 1: printf("Start"); break;
case 2: printf("Stop"); break;
default: printf("Invalid choice");
}
Loops
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
📥 Input and Output
Input:
scanf("%d", &num);
Output:
printf("Value is %d", num);
Common Format Specifiers:
%d
– int%f
– float%c
– char%s
– string
🧪 Sample Program: 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\n", sum);
return 0;
}
✅ Summary
- C is a powerful, foundational language
- Use
main()
,printf()
, andscanf()
for basic programs - Understand control flow: if, for, while
- Use correct data types and operators
📝 Practice Problems
- Write a program to check even or odd.
- Write a program to find the maximum of three numbers.
- Write a program to calculate the area of a circle.