C Programming Full Notes
Table of Contents
1. Variables, Data Types, and I/O
2. Operators: Arithmetic, Relational, Logical
3. Control Structures: if, switch, loops
4. Functions
5. Arrays
6. Pointers
7. Dynamic Memory Allocation
8. Structures
9. File Handling
1. Variables, Data Types, and I/O
Variables in C store data, and each variable has a specific data type such as int, float, char, etc.
Common Data Types:
- int: For integers
- float: For floating-point numbers
- char: For single characters
Input/Output:
- printf: To display output
- scanf: To take input
Examples:
Example 1:
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is: %d", age);
return 0;
Example 2:
#include <stdio.h>
int main() {
float salary;
printf("Enter your salary: ");
scanf("%f", &salary);
printf("Your salary is: %.2f", salary);
return 0;
2. Operators: Arithmetic, Relational, Logical
Operators in C allow you to perform operations on variables.
- Arithmetic Operators: +, -, *, /, %
- Relational Operators: ==, !=, >, <, >=, <=
- Logical Operators: &&, ||, !
Examples:
Example 1:
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("Sum: %d", a + b);
return 0;
Example 2:
#include <stdio.h>
int main() {
int x = 5, y = 10;
if (x < y) {
printf("x is less than y");
} else {
printf("x is not less than y");
return 0;
3. Control Structures: if, switch, loops
Control structures in C allow decision-making and looping.
- if-else: Used for conditional execution.
- switch: Used for multi-way branching.
- Loops: while, for, do-while for repeating code.
Examples:
Example 1:
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
printf("Positive number");
} else {
printf("Negative number");
return 0;
Example 2:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i);
return 0;
4. Functions
Functions in C are blocks of code that perform specific tasks.
Syntax:
return_type function_name(parameters) {
// function body
Examples:
Example 1:
#include <stdio.h>
int add(int a, int b) {
return a + b;
int main() {
int sum = add(5, 10);
printf("Sum: %d", sum);
return 0;
}
Example 2:
#include <stdio.h>
void greet() {
printf("Hello, World!");
int main() {
greet();
return 0;
5. Arrays
Arrays in C store multiple values of the same type in a single variable.
Syntax:
data_type array_name[array_size];
Examples:
Example 1:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
Example 2:
#include <stdio.h>
int main() {
float prices[3] = {9.99, 19.99, 29.99};
for (int i = 0; i < 3; i++) {
printf("%.2f ", prices[i]);
return 0;
6. Pointers
Pointers in C store the address of variables.
Syntax:
data_type *pointer_name;
Examples:
Example 1:
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("Value of a: %d", *p); // Dereferencing pointer
return 0;
Example 2:
#include <stdio.h>
int main() {
float num = 10.5;
float *ptr = #
printf("Address of num: %p", ptr);
return 0;
7. Dynamic Memory Allocation
Dynamic memory allocation allows allocating memory during runtime using malloc and free.
Examples:
Example 1:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int*)malloc(sizeof(int));
*p = 25;
printf("Value: %d", *p);
free(p); // Freeing allocated memory
return 0;
Example 2:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 5;
int *arr = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
printf("%d ", arr[i]);
free(arr); // Free allocated memory
return 0;
8. Structures
Structures in C allow you to group variables of different types under a single name.
Syntax:
struct structure_name {
data_type member1;
data_type member2;
// ...
};
Examples:
Example 1:
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
struct Student s1;
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter age: ");
scanf("%d", &s1.age);
printf("Name: %s, Age: %d", s1.name, s1.age);
return 0;
Example 2:
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {10, 20};
printf("Point p1: (%d, %d)", p1.x, p1.y);
return 0;
9. File Handling
File handling in C allows you to read and write data to/from files.
Common functions:
- fopen: Opens a file
- fclose: Closes a file
- fprintf: Writes to a file
- fscanf: Reads from a file
Examples:
Example 1:
#include <stdio.h>
int main() {
FILE *fptr;
fptr = fopen("file.txt", "w");
fprintf(fptr, "Hello, World!");
fclose(fptr);
return 0;
Example 2:
#include <stdio.h>
int main() {
FILE *fptr;
char data[50];
fptr = fopen("file.txt", "r");
fscanf(fptr, "%s", data);
printf("Data from file: %s", data);
fclose(fptr);
return 0;