1.Introduction
C is a high-performance, low-level, general-purpose programming language. It is widely used for system programming, embedded systems, operating systems, game development, and high-performance applications. Key Features of C:- Fast & Efficient – Direct hardware interaction
- Low-Level Memory Control – Pointers, Memory Allocation
- Portable & Cross-Platform – Runs on multiple OS
- Used in Operating Systems – Linux, Windows Kernel
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
#include <stdio.h> – Includes standard I/O library
int main() – Main function, execution starts here
printf() – Prints output to the console
return 0; – Returns 0 (successful execution)
2.Data Types & Variables
Basic Data TypesType | Size | Range |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
float | 4 bytes | ±3.4E-38 to ±3.4E+38 |
double | 8 bytes | ±1.7E-308 to ±1.7E+308 |
char | 1 byte | 0 to 255 (ASCII values) |
int a = 10; // Integer variable
float b = 3.14; // Floating point
char c = 'A'; // Character
3.Operators in C
Arithmetic OperatorsOperator | Description | Example |
+ | Addition | a + b |
– | Subtraction | a – b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |
Operator | Description | Example |
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
4.Control Statements
If-Else Condition
if (a > b) {
printf("a is greater");
} else {
printf("b is greater");
}
switch (choice) {
case 1: printf("Option 1"); break;
case 2: printf("Option 2"); break;
default: printf("Invalid choice");
}
Loop Type | Syntax Example |
For Loop | for (int i=0; i<5; i++) { printf(“%d”, i); } |
While Loop | while (x < 10) { x++; } |
Do-While Loop | do { x++; } while (x < 10); |
5.Functions in C
Function Declaration & Definition
#include <stdio.h>
// Function declaration
void greet();
int main() {
greet(); // Function call
return 0;
}
// Function definition
void greet() {
printf("Hello, Function!\n");
}
6.Arrays & Strings
1D Array Example
int numbers[5] = {1, 2, 3, 4, 5};
printf("%d", numbers[2]); // Output: 3
int matrix[2][2] = {{1, 2}, {3, 4}};
printf("%d", matrix[1][1]); // Output: 4
#include <stdio.h>
#include <string.h>
char name[] = "Alice";
printf("%s", name); // Output: Alice
7.Pointers in C
Pointer Declaration & Usage
int a = 10;
int *ptr = &a;
printf("%d", *ptr); // Output: 10
- *ptr – Dereferencing (accessing value)
- &a – Address-of operator (getting memory location)
Operation | Example |
Increment Pointer | ptr++ |
Decrement Pointer | ptr– |
Add Integer to Pointer | ptr + 2 |
8.Dynamic Memory Allocation ️
Using malloc() & free()
int *ptr = (int*) malloc(5 * sizeof(int)); // Allocates memory for 5 integers
free(ptr); // Releases allocated memory
int *arr = (int*) calloc(5, sizeof(int)); // Allocates 5 blocks of memory
arr = realloc(arr, 10 * sizeof(int)); // Resizes array
free(arr); // Frees memory
9.File Handling in C
Opening & Writing to a File
FILE *file = fopen("data.txt", "w");
fprintf(file, "Hello, File!");
fclose(file);
FILE *file = fopen("data.txt", "r");
char str[100];
fgets(str, 100, file);
printf("%s", str);
fclose(file);
10.Common C Functions & Libraries
Library | Functions |
<stdio.h> | printf(), scanf(), fgets() |
<stdlib.h> | malloc(), free(), rand() |
<string.h> | strlen(), strcpy(), strcmp() |
<math.h> | sqrt(), pow(), sin(), cos() |
- Learn Syntax & Data Types.
- Understand Pointers & Memory Management.
- Work with Functions & Modular Code.
- Handle Files & IO Operations.
- Use Data Structures (Linked Lists, Stacks, Queues).
- Practice Problem-Solving (LeetCode, Codeforces, Hackerrank).