C All Notes
C All Notes
🟢 HISTORY OF C LANGUAGE
C became the foundation for many modern languages: C++, Java, C#, Objective-C, and
more.
Standardized by ANSI in 1989 as ANSI C (also called C89), later ISO C (C90, C99, C11,
C17).
Fun Facts:
The UNIX OS, Git, Linux Kernel, and Python Interpreter are written in C.
It is still one of the most used languages in embedded systems and operating systems.
🟢 BASICS OF C
1. Structure of a C Program
structure:
1
o <stdio.h>: standard input/output
int x = 10;
5. Keywords in C
These are reserved words with special meaning:
Examples: int, return, if, else, void, for, while, break, continue
int a;
scanf("%d", &a);
2
🔶 1. Bitwise Operations
Examples:
Extra Examples:
~5 → Bitwise NOT = -6
🔶 2. Command-line Arguments
Syntax:
Examples:
./program Akash
2. Add numbers:
int a = atoi(argv[1]);
int b = atoi(argv[2]);
printf("%d", a + b);
3
for(int i = 0; i < argc; i++)
printf("%s\n", argv[i]);
Steps:
1. Create myfunc.h
void greet();
2. Create myfunc.c
#include <stdio.h>
void greet() {
printf("Hi\n");
3. Use in main.c
#include "myfunc.h"
Examples:
String utilities
Constants declaration
🔶 4. Modular Programming
Examples:
1. Calculator app:
2. Student record:
3. E-commerce logic:
Compile:
4
🔶 5. Linked Lists
Structure:
struct Node {
int data;
};
Examples:
1. Insert at beginning
Stack (LIFO)
Queue (FIFO)
enqueue(), dequeue()
struct Node {
int data;
};
5
DATA TYPES & OPERATORS
Data
Description Example
Type
🔧 Type Modifiers
Type modifiers are used to alter the range and storage of data types.
Modifier Description
6
Modifier Description
Example:
Copy code
Operators in C/C++
Operato Exampl
Meaning
r e
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
Modulus
% a%b
(remainder)
Operato Exampl
Meaning
r e
== Equal to a == b
!= Not equal to a != b
7
Operato Exampl
Meaning
r e
Greater or
>= a >= b
equal
Operato
Meaning Example
r
` `
Operato
Meaning Example
r
` ` Bitwise OR
^ Bitwise XOR a ^ b
~ Bitwise NOT ~a
Operato
Meaning Example
r
= Assign x = 10
8
Operato
Meaning Example
r
x += 5 (same as x = x
+= Add and assign
+ 5)
Subtract and
-= x -= 3
assign
Modulus and
%= x %= 3
assign
Operato
Meaning Example
r
x++ or +
++ Increment by 1
+x
Decrement by
-- x-- or --x
1
🔁 Type Conversion
Example:
Copy code
int a = 5;
Syntax:
Copy code
float b = 5.67;
9
CONTROL FLOW
🔹 1. Conditional Statements
Statement Description
nested if if statement inside another if. Used for multiple levels of condition.
🔸 Syntax Examples:
Copy code
// if
if (a > 0) {
printf("Positive");
// if-else
if (a % 2 == 0)
printf("Even");
else
printf("Odd");
// nested if
if (a > 0) {
10
if (a < 100)
// else-if ladder
printf("A+");
printf("A");
else
printf("Below A");
// switch
switch(choice) {
default: printf("Invalid");
🔹 2. Looping Statements
Loop
Description
Type
🔸 Syntax Examples:
Copy code
// for loop
11
for (int i = 1; i <= 5; i++) {
// while loop
int i = 1;
while (i <= 5) {
i++;
// do-while loop
int i = 1;
do {
i++;
🔹 3. Jump Statements
Stateme
Description
nt
🔸 Syntax Examples:
Copy code
// break
12
for (int i = 0; i < 5; i++) {
if (i == 3) break;
// continue
if (i == 2) continue;
// goto
goto label;
label:
printf("Jumped here!");
// return
return a + b;
🧩 1. Arrays
✅ Types of Arrays:
Type Description
13
🔹 1D Array – Declaration & Initialization
Copy code
Copy code
Copy code
int mat[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Copy code
🧩 2. Strings
🔹 Declaration of Strings
Copy code
🔹 String Input/Output
14
c
Copy code
char name[50];
Functio
Description Example
n
🔸 Example:
Copy code
#include <stdio.h>
#include <string.h>
int main() {
char greet[20];
strcat(greet, name);
return 0;
15
}
FUNCTIONS
Functions allow you to divide a program into smaller, manageable sections that perform
specific tasks.
Term Description
Declaratio Tells the compiler about the function name, return type, and parameters (also
n called prototype).
✅ Example:
Copy code
// Declaration
// Definition
return a + b;
🔹 2. Function Calling
✅ Example:
Copy code
🔹 3. Call by Value
✅ Example:
16
c
Copy code
void change(int x) {
x = 50;
int main() {
int a = 10;
change(a);
🔹 4. Recursion
✅ Example: Factorial
Copy code
int factorial(int n) {
if (n == 0) return 1;
🔹 5. Storage Classes
Class Description
✅ Example:
17
Copy code
void example() {
x++;
Copy code
1 2 3 ...
POINTERS
🔹 1. Basics of Pointers
Concept Description
✅ Example:
Copy code
int a = 10;
🔹 2. Pointer Arithmetic
Operatio
Meaning
n
✅ Example:
18
Copy code
int *p = arr;
✅ Example:
Copy code
Copy code
*x = *x + 5;
int main() {
int a = 10;
update(&a);
19
🔹 5. NULL Pointer
✅ Example:
Copy code
🔹 6. const Pointer
Type Description
✅ Example:
Copy code
✅ Example:
Copy code
int a = 5;
int *p = &a;
20
🔹 1. Declaring and Using Structures
A structure is a user-defined data type that groups related variables of different types
under one name.
✅ Declaration:
Copy code
struct Student {
int roll;
char name[20];
float marks;
};
Copy code
s1.roll = 1;
strcpy(s1.name, "Akash");
s1.marks = 89.5;
🔹 2. Array of Structures
✅ Example:
Copy code
🔹 3. Nested Structures
✅ Example:
21
c
Copy code
struct Date {
};
struct Student {
int roll;
};
🔹 4. Pointer to Structure
✅ Example:
Copy code
A union is like a structure but all members share the same memory location.
✅ Declaration:
Copy code
union Data {
int i;
float f;
char str[20];
};
union Data d;
✅ Usage:
22
c
Copy code
d.i = 10;
Featur
Structure Union
e
Dynamic Memory Allocation (DMA) allows memory to be allocated during runtime, using
functions from the <stdlib.h> header.
Copy code
int *ptr;
Copy code
int *ptr;
23
ptr = (int*) calloc(5, sizeof(int)); // allocates and sets 5 integers to 0
🔹 3. realloc() — Reallocation
Copy code
Copy code
free(ptr);
⚠️Memory Leak
❌ Example:
Copy code
⚠️Dangling Pointer
❌ Example:
Copy code
✅ Summary Table:
24
Functio
Purpose Initialization Can Resize Must Free
n
Allocates +
calloc() ✅ (to 0) ✅ ✅
initializes
Resize memory
realloc() ❌ ✅ ✅
block
FILE HANDLING in C
File handling allows you to store and access data permanently in files instead of
temporary memory (RAM).
Functio
Description
n
✅ Example:
Copy code
FILE *fp;
🔹 2. File Modes
Mode Description
25
Mode Description
rb, wb,
Same as above but in binary mode.
ab
Functio
Description
n
✅ Examples:
Copy code
📤 Writing to File
Functio
Description
n
✅ Examples:
Copy code
26
FILE is a special structure that stores information about the file.
✅ Example:
Copy code
FILE *fp;
fp = fopen("data.txt", "w");
if (fp == NULL) {
} else {
fclose(fp);
✅ Summary Table:
fprintf(), fputs(),
Write Data Write to file
putc()
fscanf(), fgets(),
Read Data Read from file
getc()
Pointer to file
File Pointer FILE *fp
structure
PREPROCESSOR DIRECTIVES
Syntax Meaning
27
Syntax Meaning
#include
Includes a user-defined header file
"myfile.h"
✅ Constant Example:
Copy code
#define PI 3.14159
printf("%.2f", PI);
✅ Macro Example:
Copy code
🔹 3. Macros vs Constants
const float PI =
Syntax #define PI 3.14
3.14;
🔹 4. Conditional Compilation
Directive Description
28
Directive Description
#elif
✅ Example:
Copy code
#define DEBUG
#ifdef DEBUG
#endif
✅ Another Example:
Copy code
#define VALUE 10
printf("High value\n");
#else
printf("Low value\n");
#endif
✅ Summary Table:
Directive Purpose
29
Operato Example (a=5, Resul
Name
r b=3) t
` ` OR `a
^ XOR a^b→5^3→6
~ NOT ~a → ~5 → -6
Right
>> a >> 1 → 2
Shift
✅ Usage:
Copy code
int a = 5, b = 3;
🔹 2. Command-Line Arguments
Copy code
Ter
Meaning
m
✅ Example:
Copy code
if (argc > 1)
return 0;
To run:
30
bash
Copy code
./a.out hello
Copy code
Copy code
#include "mathutils.h"
return a + b;
Copy code
#include "mathutils.h"
int main() {
🔹 4. Modular Programming
✅ Benefits:
Easy to debug
Reusable
Organized
31
🔹 5. Linked Lists
✅ Node Definition:
Copy code
struct Node {
int data;
};
Copy code
head->data = 10;
head->next = NULL;
✅ Print List:
Copy code
temp = temp->next;
Copy code
stack[++top] = val;
32
}
int pop() {
return stack[top--];
Copy code
struct Node {
int data;
};
newNode->data = val;
newNode->next = *top;
*top = newNode;
Copy code
queue[++rear] = val;
int dequeue() {
return queue[++front];
Copy code
struct Node {
int data;
33
struct Node* next;
};
34
c
Copy code
#include <stdio.h>
#include <stdlib.h>
struct Student {
int roll;
char name[50];
float marks;
};
int main() {
struct Student s;
if (!fp) {
return 1;
scanf("%d", &s.roll);
scanf("%s", s.name);
scanf("%f", &s.marks);
fclose(fp);
return 0;
Output:
35
mathematica
Copy code
Copy code
#include <stdio.h>
int main() {
float a, b;
char op;
switch (op) {
return 0;
36
}
Output:
cpp
Copy code
Result = 40.00
Copy code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = val;
newNode->next = *head;
*head = newNode;
while (head) {
head = head->next;
printf("NULL\n");
int main() {
37
struct Node *head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
display(head);
return 0;
Output:
rust
Copy code
Copy code
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d", &n);
if (!arr) {
return 1;
scanf("%d", &arr[i]);
sum += arr[i];
38
}
free(arr);
return 0;
Output:
mathematica
Copy code
Enter 4 elements:
2468
Sum = 20
Copy code
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
Copy code
#include <stdio.h>
int main() {
int a = 5, b = 3;
return 0;
39
Copy code
#include <stdio.h>
int main() {
temp = a;
a = b;
b = temp;
return 0;
Copy code
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
return 0;
Copy code
#include <stdio.h>
int main() {
int a = 5, b = 10;
if (a > b)
else
return 0;
40
}
Copy code
#include <stdio.h>
int main() {
int num = 7;
if (num % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
Copy code
#include <stdio.h>
int main() {
printf("%d\n", i);
return 0;
Copy code
#include <stdio.h>
int main() {
int n = 5;
return 0;
41
}
Copy code
#include <stdio.h>
int main() {
int n = 5, fact = 1;
fact *= i;
return 0;
Copy code
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
int main() {
return 0;
Copy code
#include <stdio.h>
int main() {
int n = 7, flag = 1;
42
for (int i = 2; i < n; i++) {
if (n % i == 0) {
flag = 0;
break;
if (flag)
printf("Prime\n");
else
printf("Not Prime\n");
return 0;
Copy code
#include <stdio.h>
int main() {
int a = 0, b = 1, n = 10;
int c = a + b;
a = b;
b = c;
return 0;
Copy code
#include <stdio.h>
int main() {
43
int num = 1234, rev = 0;
while (num != 0) {
num /= 10;
return 0;
Copy code
#include <stdio.h>
int main() {
while (num != 0) {
num /= 10;
if (rev == orig)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
44