C Programming Handbook.
C Programming Handbook.
1. Simple and Efficient: C language ko samajhna aur use karna aasan hai, aur yeh high-
level aur low-level dono tarah ki functionalities ko support karti hai.
2. Portable: C programs ko kisi bhi system pe run kiya ja sakta hai jahan C compiler
available ho.
3. Rich Library: C ke paas ek acchi khaasi standard library hai jisme commonly used
functions included hain.
4. Modularity: C programs ko modules mein divide kiya ja sakta hai jisse reusability badhti
hai aur maintenance easy ho jata hai.
5. Middle-Level Language: C ko ek middle-level language bola jata hai kyunki yeh low-level
memory access ko allow karti hai jaise pointers ke through, aur saath hi high-level
constructs bhi provide karti hai.
Hello, World!
Explanation:
data_type variable_name;
Example:
int age;
float salary;
char grade;
int: Yeh integer type variable hai jo whole numbers ko store karta hai.
float: Yeh floating-point numbers ko store karta hai, yaani decimal values.
char: Yeh single character ko store karta hai.
int: Integer type, jo whole numbers ko store karta hai. Example: 5, -10.
float: Floating-point type, jo decimal numbers ko store karta hai. Example: 3.14, -0.001.
char: Character type, jo ek single character store karta hai. Example: 'A', 'b'.
double: Double-precision floating-point type, jo zyada accurate decimal numbers store
karta hai. Example: 3.14159265359.
Example:
#define PI 3.14
2.4 Operators in C
Operators woh symbols hote hain jo operations perform karte hain. C mein kuch common
operators hain:
1. Arithmetic Operators:
+ : Addition (jodna)
- : Subtraction (ghatana)
* : Multiplication (guna)
/ : Division (bhag)
% : Modulus (remainder nikalna)
Example:
int a = 10, b = 5;
int sum = a + b; // sum will be 15
2. Relational Operators:
== : Equal to
!= : Not equal to
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Less than or equal to
Example:
int x = 5, y = 10;
if (x < y) {
// This condition is true
}
3. Logical Operators:
Example:
int a = 5, b = 10;
if (a > 0 && b > 0) {
// This condition is true
}
4. Assignment Operators:
= : Simple assignment
+= : Add and assign
-= : Subtract and assign
*= : Multiply and assign
/= : Divide and assign
%= : Modulus and assign
Example:
int x = 10;
x += 5; // x will be 15
Example:
int i = 1;
i++; // i will be 2
#include <stdio.h>
int main() {
int num1 = 10, num2 = 20;
int sum = num1 + num2; // Addition
int diff = num2 - num1; // Subtraction
int prod = num1 * num2; // Multiplication
int quot = num2 / num1; // Division
printf("Sum = %d\n", sum);
printf("Difference = %d\n", diff);
printf("Product = %d\n", prod);
printf("Quotient = %d\n", quot);
return 0;
}
Sum = 30
Difference = 10
Product = 200
Quotient = 2
Is program mein humne variables declare kiye hain, aur un par arithmetic operations perform
karke results ko output kiya hai.
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
int sum = a + b; // Sequential execution
printf("Sum = %d\n", sum);
return 0;
}
Sum = 15
1. if Statement
if statement ko use karke aap kisi condition ko check kar sakte hain. Agar condition true hoti
hai, to uske andar ka code execute hota hai.
Syntax:
if (condition) {
// Code to be executed if condition is true
}
Example:
Number is positive
2. if-else Statement
if-else statement mein agar condition true hoti hai to if block execute hota hai, aur agar
false hoti hai to else block execute hota hai.
Syntax:
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
Example:
3. else-if Ladder
else-if ladder tab use hoti hai jab aapko multiple conditions check karni ho. Har if ke baad
ek else-if statement ho sakti hai, aur aakhir mein ek else block ho sakta hai.
Syntax:
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else if (condition3) {
// Code for condition3
} else {
// Code if all conditions are false
}
Example:
int num = 0;
if (num > 0) {
printf("Number is positive\n");
} else if (num < 0) {
printf("Number is negative\n");
} else {
printf("Number is zero\n");
}
Number is zero
4. switch Statement
switch statement multiple conditions ko check karne ke liye use hoti hai, par yeh if-else se
thoda different hota hai. Yeh ek variable ki value ko check karta hai aur uske hisaab se code
execute karta hai.
Syntax:
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
...
default:
// Code if no case matches
}
Example:
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
Wednesday
1. for Loop
for loop use hota hai jab aapko kisi block of code ko fixed number of times execute karna ho.
Syntax:
Example:
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
1
2
3
4
5
2. while Loop
while loop tab use hota hai jab aapko pata ho ki koi condition true hai par aapko number of
iterations nahi pata. Yeh loop tab tak execute hota hai jab tak condition true rahe.
Syntax:
while (condition) {
// Code to be executed
}
Example:
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
1
2
3
4
5
3. do-while Loop
do-while loop while loop se different hota hai kyunki yeh pehle ek baar code ko execute
karta hai phir condition check karta hai. Isliye yeh loop at least ek baar toh zaroor execute hota
hai.
Syntax:
do {
// Code to be executed
} while (condition);
Example:
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
1
2
3
4
5
#include <stdio.h>
int main() {
int number = 2;
return 0;
}
Number is two
Number is two
Iteration 1
Iteration 2
Iteration 3
Is program mein humne decision-making aur looping control structures ko use karke output
generate kiya hai.
Chapter 4: Functions
4.1 Introduction to Functions
Functions C programming mein ek reusable block of code hote hain jo specific task perform
karte hain. Functions ka use karke aap apne code ko modular aur manageable bana sakte
hain. Aap ek function ko ek bar define karke, usko program ke multiple jagah use kar sakte
hain.
Syntax:
return_type function_name(parameters) {
// Code to be executed
}
Example:
#include <stdio.h>
// Function definition
void greet() {
printf("Hello, World!\n");
}
int main() {
greet(); // Function call
return 0;
}
Hello, World!
Explanation:
void greet(): Yeh function definition hai. void return type ko indicate karta hai ki function
kuch bhi return nahi karega.
greet();: Yeh function call hai jo main function se greet function ko execute karta hai.
Syntax:
Example:
#include <stdio.h>
Sum = 15
Explanation:
int add(int a, int b): Yeh function do integer parameters ko accept karta hai aur unka sum
return karta hai.
add(5, 10): Yeh function call hai jo 5 aur 10 ko parameters ke roop mein pass karta hai.
Example:
#include <stdio.h>
int main() {
int result = multiply(4, 5); // Function call
printf("Product = %d\n", result);
return 0;
}
Product = 20
Explanation:
int multiply(int x, int y): Yeh function do integer values ko multiply karta hai aur result
return karta hai.
return x * y;: Yeh statement function se result return karta hai.
Syntax:
return_type function_name(parameters) {
if (condition) {
// Recursive call
function_name(parameters);
}
// Base case
}
Example:
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1; // Base case
else
return n * factorial(n - 1); // Recursive call
}
int main() {
int result = factorial(5); // Function call
printf("Factorial = %d\n", result);
return 0;
}
Factorial = 120
Explanation:
int factorial(int n): Yeh function factorial calculate karta hai using recursion.
factorial(n - 1): Yeh recursive call hai jo function ko apne aap ko call karta hai.
#include <stdio.h>
int main() {
int length = 10;
int width = 5;
int rect_area = area(length, width); // Function call
printf("Area of rectangle = %d\n", rect_area);
return 0;
}
Area of rectangle = 50
Is program mein humne area function ko define kiya hai jo rectangle ka area calculate karta
hai aur main function se call kiya hai.
data_type array_name[array_size];
Example:
Array elements ko index ke through access kiya jata hai. Array indexing 0 se start hoti hai.
Example:
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
printf("First element: %d\n", arr[0]); // Accessing first element
printf("Second element: %d\n", arr[1]); // Accessing second element
return 0;
}
First element: 10
Second element: 20
5.2 Multidimensional Arrays
Multidimensional arrays ko 2D (two-dimensional) aur 3D (three-dimensional) arrays ke roop
mein declare kiya ja sakta hai. Yeh arrays tab use hote hain jab aapko grid-like structure ko
represent karna ho.
data_type array_name[rows][columns];
Example:
#include <stdio.h>
int main() {
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
return 0;
}
5.3 Strings in C
Strings C mein character arrays ke roop mein store kiye jate hain. String ko '\0' (null
character) se terminate kiya jata hai jo string ki end ko indicate karta hai.
Syntax:
char string_name[size];
Initialization:
Example:
#include <stdio.h>
int main() {
char str[] = "Hello";
printf("First character: %c\n", str[0]); // Accessing first character
printf("Second character: %c\n", str[1]); // Accessing second character
return 0;
}
First character: H
Second character: e
C library mein string functions ka ek set hota hai jo strings ko manipulate karne mein help karte
hain. Yeh functions <string.h> header file mein define hote hain.
Examples:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
printf("Length of string: %lu\n", strlen(str));
return 0;
}
Length of string: 5
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello";
char destination[10];
strcpy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}
Copied string: Hello
#include <stdio.h>
#include <string.h>
int main() {
// Array example
int arr[4] = {10, 20, 30, 40};
for (int i = 0; i < 4; i++) {
printf("Array element at index %d: %d\n", i, arr[i]);
}
// String example
char name[20] = "Programming";
printf("String: %s\n", name);
printf("Length of string: %lu\n", strlen(name));
char copy[20];
strcpy(copy, name);
printf("Copied string: %s\n", copy);
return 0;
}
Is program mein humne arrays aur strings ko declare kiya, initialize kiya, aur unhe manipulate
kiya.
Great! Let’s move on to the next chapter.
data_type *pointer_name;
pointer_name = &variable_name;
Example:
#include <stdio.h>
int main() {
int num = 10;
int *ptr; // Pointer declaration
ptr = # // Pointer initialization
return 0;
}
Value of num: 10
Address of num: [memory address]
Value at address ptr: 10
Explanation:
int *ptr: Yeh pointer declaration hai jo integer type ke memory address ko store kar sakta
hai.
ptr = &num: Yeh num variable ke address ko ptr pointer mein store karta hai.
printf("Address of num: %p\n", (void*)ptr): Yeh ptr ke address ko print karta hai.
printf("Value at address ptr: %d\n", *ptr): Yeh ptr ke address pe stored value ko print
karta hai.
Example:
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr;
return 0;
}
Value at index 0: 10
Value at index 1: 20
Value at index 2: 30
Explanation:
int *ptr = arr: Yeh arr array ke starting address ko ptr pointer mein store karta hai.
*(ptr + i): Yeh pointer arithmetic se ptr ko i position pe shift karta hai aur value access
karta hai.
6.3 Pointers and Arrays
Arrays aur pointers closely related hote hain. Array ke naam ko pointer ke roop mein treat kiya
jata hai jo array ke starting address ko represent karta hai.
Example:
#include <stdio.h>
int main() {
int arr[4] = {1, 2, 3, 4};
int *ptr = arr;
return 0;
}
Array element 0: 1
Array element 1: 2
Array element 2: 3
Array element 3: 4
Explanation:
int *ptr = arr: Yeh arr array ke starting address ko ptr pointer mein store karta hai.
*(ptr + i): Yeh pointer arithmetic se arr ke elements ko access karta hai.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
return 0;
}
Array element 0: 1
Array element 1: 2
Array element 2: 3
Array element 3: 4
Array element 4: 5
Explanation:
#include <stdio.h>
void display() {
printf("Function called through pointer\n");
}
int main() {
void (*func_ptr)(); // Function pointer declaration
func_ptr = display; // Function pointer initialization
func_ptr(); // Function call using pointer
return 0;
}
Explanation:
void (*func_ptr)(): Yeh function pointer declaration hai jo display function ke address ko
store kar sakta hai.
func_ptr = display: Yeh function pointer ko display function ke address se initialize
karta hai.
func_ptr(): Yeh function pointer se function ko call karta hai.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int size;
printf("You entered:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Is program mein user se array size aur elements input karte hain, dynamically memory allocate
karte hain, aur un elements ko print karte hain.
File Modes:
Example:
#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;
}
return 0;
}
Hello, File Handling!
Explanation:
fopen("example.txt", "w"): example.txt file ko write mode mein open karta hai.
fprintf(file, "Hello, File Handling!\n"): File mein data write karta hai.
fclose(file): File ko close karta hai.
1. Using fscanf()
Syntax:
Example:
#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 buffer[100];
fscanf(file, "%s", buffer); // Reading from file
printf("Read from file: %s\n", buffer);
return 0;
}
Read from file: Hello,
Explanation:
fscanf(file, "%s", buffer): File se string read karta hai aur buffer mein store karta hai.
2. Using fgets()
Syntax:
Example:
#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 buffer[100];
fgets(buffer, sizeof(buffer), file); // Reading line from file
printf("Read from file: %s", buffer);
return 0;
}
Explanation:
fgets(buffer, sizeof(buffer), file): File se ek line read karta hai aur buffer mein store
karta hai.
1. Using fprintf()
Syntax:
Example:
#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;
}
return 0;
}
Explanation:
fprintf(file, "Hello, File Handling!\n"): File mein formatted data write karta hai.
2. Using fputs()
Syntax:
Example:
#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;
}
return 0;
}
Explanation:
fputs("Hello, File Handling!\n", file): File mein string write karta hai.
Example:
#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;
}
return 0;
}
Explanation:
#include <stdio.h>
int main() {
// Writing to a file
FILE *file = fopen("data.txt", "w");
if (file == NULL) {
printf("Error opening file for writing\n");
return 1;
}
char buffer[100];
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
fclose(file);
return 0;
}
File Handling in C
Is program mein pehle file mein data write karte hain aur phir usi file se data read karte hain.
1. Defining a Structure
Syntax:
struct structure_name {
data_type member1;
data_type member2;
// Other members
};
Example:
#include <stdio.h>
// Defining a structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
struct Person person1; // Creating a structure variable
// Assigning values
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 5.9;
// Accessing values
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f\n", person1.height);
return 0;
}
Explanation:
struct Person: Yeh structure ko define karta hai jo name , age , aur height members ko
include karta hai.
person1: Yeh Person type ka structure variable hai.
person1.name: Structure member ko access karta hai aur value assign karta hai.
Example:
#include <stdio.h>
struct Person {
char name[50];
int age;
struct Address address; // Nested structure
};
int main() {
struct Person person1;
// Assigning values
strcpy(person1.name, "Jane Doe");
person1.age = 28;
strcpy(person1.address.street, "123 Main St");
strcpy(person1.address.city, "New York");
// Accessing values
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Address: %s, %s\n", person1.address.street, person1.address.city);
return 0;
}
Explanation:
struct Address: Yeh ek nested structure define karta hai jo address ke details ko store
karta hai.
struct Person: Yeh Address structure ko include karta hai.
8.3 Unions
Unions C programming mein ek special data type hote hain jo multiple data types ko ek single
memory location mein store karte hain. Union ke through aap ek hi time pe sirf ek member ko
store kar sakte hain, lekin memory space save hoti hai.
1. Defining a Union
Syntax:
union union_name {
data_type member1;
data_type member2;
// Other members
};
Example:
#include <stdio.h>
// Defining a union
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
// Assigning values
data.i = 10;
printf("Integer: %d\n", data.i);
return 0;
}
Integer: 10
Float: 220.50
String: Hello, World!
Explanation:
union Data: Yeh union ko define karta hai jo i , f , aur str members ko include karta
hai.
data: Yeh Data type ka union variable hai.
data.i, data.f, data.str: Union ke members ko access karte hain. Notice how assigning a
value to one member overwrites the value of previous members.
#include <stdio.h>
// Defining a structure
struct Person {
char name[50];
int age;
};
int main() {
struct Person person1 = {"Alice", 25};
printPerson(person1); // Passing structure to function
return 0;
}
Name: Alice
Age: 25
Explanation:
void printPerson(struct Person p): Yeh function ek structure ko parameter ke roop mein
accept karta hai.
Example:
#include <stdio.h>
// Defining a structure
struct Person {
char name[50];
int age;
};
int main() {
struct Person person1 = createPerson("Bob", 30); // Returning structure
from function
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
return 0;
}
Name: Bob
Age: 30
Explanation:
struct Person createPerson(char name[], int age): Yeh function ek structure ko return
karta hai.
// Defining a structure
struct Book {
char title[50];
char author[50];
int year;
};
// Defining a union
union Value {
int i;
float f;
char str[20];
};
int main() {
// Using structure
struct Book book1;
strcpy(book1.title, "The Great Gatsby");
strcpy(book1.author, "F. Scott Fitzgerald");
book1.year = 1925;
// Using union
union Value val;
val.i = 100;
printf("Integer value: %d\n", val.i);
val.f = 98.6;
printf("Float value: %.2f\n", val.f);
strcpy(val.str, "Union Example");
printf("String value: %s\n", val.str);
return 0;
}
Is program mein humne ek structure aur ek union ko define kiya, unka use kiya aur unke
members ko print kiya.
Example:
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
FILE *file = fopen("nonexistentfile.txt", "r");
if (file == NULL) {
// Print the error message
perror("Error opening file");
printf("Error code: %d\n", errno);
} else {
// File operations
fclose(file);
}
return 0;
}
Error opening file: No such file or directory
Error code: 2
Explanation:
fopen("nonexistentfile.txt", "r"): Agar file nahi milti hai, to NULL return hota hai aur
errno set hota hai.
perror("Error opening file"): Error message ko print karta hai jo errno ke value se
associated hota hai.
printf("Error code: %d\n", errno): Error code ko print karta hai.
1. #include Directive
Syntax:
#include <header_file>
#include "user_defined_file"
Example:
Explanation:
2. #define Directive
Syntax:
#define MACRO_NAME value
Example:
#include <stdio.h>
#define PI 3.14
int main() {
printf("Value of PI: %.2f\n", PI);
return 0;
}
Explanation:
#define PI 3.14: PI macro ko 3.14 value assign karta hai. Compiler isko replace kar
deta hai.
3. Conditional Compilation
Syntax:
#ifdef MACRO_NAME
// Code to compile if MACRO_NAME is defined
#else
// Code to compile if MACRO_NAME is not defined
#endif
Example:
#include <stdio.h>
#define DEBUG
int main() {
#ifdef DEBUG
printf("Debug mode is enabled.\n");
#endif
printf("Program execution.\n");
return 0;
}
Explanation:
#ifdef DEBUG: Yeh check karta hai ki DEBUG macro define hai ya nahi. Agar hai, to
debug related code execute hoga.
Syntax:
Example:
#include <stdio.h>
int main() {
int num = 5;
printf("Square of %d is %d\n", num, SQUARE(num));
return 0;
}
Square of 5 is 25
Explanation:
#define SQUARE(x) ((x) * (x)): Yeh macro function x ka square calculate karta hai.
9.4 Example Program: Error Handling and Preprocessor
Directives
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
FILE *file = fopen(FILE_NAME, "r");
if (file == NULL) {
perror("Error opening file");
printf("Error code: %d\n", errno);
return 1;
}
#ifdef DEBUG
printf("File opened successfully.\n");
#endif
fclose(file);
return 0;
}
Is program mein, file handling aur error handling ke saath preprocessor directives aur macro
function ka use kiya gaya hai.
Syntax:
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Assigning values
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
// Printing values
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
1 2 3 4 5
Explanation:
Syntax:
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Printing values
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
0 0 0 0 0
Explanation:
calloc(n, sizeof(int)): n integers ke liye memory allocate karta hai aur initialize karta hai.
Syntax:
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Reallocating memory
n = 10;
arr = (int *)realloc(arr, n * sizeof(int));
if (arr == NULL) {
printf("Memory reallocation failed\n");
return 1;
}
// Printing values
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr); // Freeing allocated memory
return 0;
}
1 2 3 4 5 6 7 8 9 10
Explanation:
10.2 Multi-threading
C mein multi-threading ko implement karne ke liye pthread library ka use kiya jata hai.
Threads ek program ke andar parallel tasks ko execute karte hain.
Example:
#include <stdio.h>
#include <pthread.h>
int main() {
pthread_t thread1;
const char *message = "Hello from the thread!";
return 0;
}
Hello from the thread!
Explanation:
2. Thread Synchronization
Multi-threading mein data consistency aur race conditions ko handle karne ke liye
synchronization techniques ka use kiya jata hai.
Example:
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
int counter = 0;
int main() {
pthread_t thread1, thread2;
pthread_mutex_init(&mutex, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
Counter: 1
Counter: 2
Explanation:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main() {
pthread_t thread1;
int *arr = (int *)malloc(5 * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Initializing array
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
return 0;
}
1 2 3 4 5
Is program mein, memory allocation aur threading ke concepts ka use kiya gaya hai.
THE END