C Programming
C is a powerful and widely-used programming language. It is the foundation for many other languages
like C++, Java, and Python. Learning C helps you understand how computers work at a deeper level.
Below are simple and easy-to-understand notes with examples to get you started.
1. Basics of C Programming
Structure of a C Program
Every C program has a basic structure:
#include <stdio.h> // Header file for input/output functions
int main() { // Main function where execution begins
printf("Hello, World!"); // Print "Hello, World!" to the screen
return 0; // Indicates the program ended successfully
• #include <stdio.h>: Includes the standard input/output library.
• int main(): The main function is the entry point of the program.
• printf(): Used to display text on the screen.
• return 0;: Ends the program and returns 0 to the operating system.
Variables and Data Types
Variables are used to store data. Each variable has a data type that defines the kind of data it can hold.
Data Type Description Example
int Stores integers int age = 25;
float Stores decimal numbers float pi = 3.14;
char Stores single characters char grade = 'A';
double Stores large decimal numbers double bigNum = 12345.6789;
Example:
#include <stdio.h>
int main() {
int age = 25;
float pi = 3.14;
char grade = 'A';
double bigNum = 12345.6789;
printf("Age: %d\n", age); // %d for integers
printf("Pi: %f\n", pi); // %f for floats
printf("Grade: %c\n", grade); // %c for characters
printf("Big Number: %lf\n", bigNum); // %lf for doubles
return 0;
Input and Output
• printf(): Used to display output.
• scanf(): Used to take input from the user.
Example:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num); // & is used to store the input in the variable
printf("You entered: %d\n", num);
return 0;
}
2. Control Structures
If-Else Statements
Used to make decisions in your program.
Example:
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Number is positive.\n");
} else if (num == 0) {
printf("Number is zero.\n");
} else {
printf("Number is negative.\n");
return 0;
Loops
Loops are used to repeat a block of code.
For Loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i); // Prints numbers from 1 to 5
return 0;
While Loop
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i); // Prints numbers from 1 to 5
i++;
return 0;
Do-While Loop
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i); // Prints numbers from 1 to 5
i++;
} while (i <= 5);
return 0;
3. Functions
Functions are blocks of code that perform a specific task. They make your code reusable and organized.
Example:
#include <stdio.h>
// Function to add two numbers
int add(int a, int b) {
return a + b;
int main() {
int result = add(5, 3); // Call the function
printf("Sum: %d\n", result);
return 0;
4. Arrays
Arrays are used to store multiple values of the same type.
Example:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // Array of 5 integers
for (int i = 0; i < 5; i++) {
printf("%d\n", numbers[i]); // Print each element
return 0;
}
5. Pointers
Pointers are variables that store memory addresses.
Example:
#include <stdio.h>
int main() {
int num = 10;
int *ptr = # // Pointer to num
printf("Value of num: %d\n", num); // Output: 10
printf("Address of num: %p\n", ptr); // Output: Memory address of num
printf("Value using pointer: %d\n", *ptr); // Output: 10
return 0;
6. Strings
Strings are arrays of characters.
Example:
#include <stdio.h>
int main() {
char name[] = "John"; // String initialization
printf("Name: %s\n", name); // Output: John
printf("First character: %c\n", name[0]); // Output: J
return 0;
7. Structures
Structures allow you to group different data types together.
Example:
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s1 = {"Alice", 20, 85.5};
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Marks: %.2f\n", s1.marks);
return 0;
8. File Handling
C allows you to read from and write to files.
Example: Writing to a File
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w"); // Open file in write mode
if (file == NULL) {
printf("Error opening file!\n");
return 1;
fprintf(file, "Hello, File!"); // Write to file
fclose(file); // Close file
return 0;
}
Example: Reading from a File
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r"); // Open file in read mode
if (file == NULL) {
printf("Error opening file!\n");
return 1;
char text[100];
fgets(text, 100, file); // Read from file
printf("File Content: %s\n", text);
fclose(file); // Close file
return 0;
Key Takeaways
1. C is a structured and efficient programming language.
2. Learn the basics: variables, data types, input/output.
3. Master control structures: if-else, loops.
4. Use functions to make your code modular.
5. Explore advanced topics like pointers, arrays, and file handling.
Practice is key! Try writing small programs to reinforce your learning. Happy coding!