Loading...
Loading...
C ek compiled, procedural, general-purpose programming language hai jo 1972 mein Dennis Ritchie ne Bell Labs mein banai.
Kyon C seekhein?
Program Structure:
#include <stdio.h> // Header file — printf, scanf
#include <stdlib.h> // malloc, free, exit
int main() { // Entry point — every C program starts here
printf("Hello, BCA World!\n");
return 0; // 0 = success, non-zero = error
}
Compilation process:
Source (.c) → [Preprocessor] → [Compiler] → [Assembler] → [Linker] → Executable
// Basic data types
int age = 20; // 4 bytes, -2B to +2B
float gpa = 8.5f; // 4 bytes, ~7 decimal digits
double pi = 3.14159265; // 8 bytes, ~15 decimal digits
char grade = 'A'; // 1 byte, ASCII character
long population = 7800000000L; // 8 bytes
// Type modifiers
short int x = 100; // 2 bytes
unsigned int u = 4294967295; // no negative, 0 to ~4B
long double ld = 3.14L; // 10-16 bytes
// Constants
#define PI 3.14159 // Preprocessor constant (no memory)
const int MAX = 100; // Const variable (has memory, type-safe)
Format Specifiers (printf/scanf): | Type | Specifier | |------|-----------| | int | %d | | float | %f | | double | %lf | | char | %c | | string | %s | | pointer | %p | | hex | %x |
// Arithmetic
int a = 10, b = 3;
printf("%d\n", a + b); // 13
printf("%d\n", a - b); // 7
printf("%d\n", a * b); // 30
printf("%d\n", a / b); // 3 (integer division)
printf("%d\n", a % b); // 1 (remainder/modulus)
// Relational — return 0 or 1
printf("%d\n", a == b); // 0 (false)
printf("%d\n", a != b); // 1 (true)
printf("%d\n", a > b); // 1 (true)
// Logical
printf("%d\n", (a > 5) && (b < 5)); // 1 (AND)
printf("%d\n", (a < 5) || (b < 5)); // 1 (OR)
printf("%d\n", !(a > 5)); // 0 (NOT)
// Bitwise
printf("%d\n", 5 & 3); // 1 (101 & 011 = 001)
printf("%d\n", 5 | 3); // 7 (101 | 011 = 111)
printf("%d\n", 5 ^ 3); // 6 (101 ^ 011 = 110)
printf("%d\n", ~5); // -6 (bitwise NOT)
printf("%d\n", 5 << 1); // 10 (left shift = multiply by 2)
printf("%d\n", 8 >> 2); // 2 (right shift = divide by 4)
// Increment/Decrement
int x = 5;
printf("%d\n", x++); // 5 (print then increment)
printf("%d\n", ++x); // 7 (increment then print)
// if-else if-else
int marks = 75;
if (marks >= 90) printf("A+\n");
else if (marks >= 75) printf("A\n"); // This runs
else if (marks >= 60) printf("B\n");
else printf("Fail\n");
// switch-case (for discrete values)
char grade = 'B';
switch(grade) {
case 'A': printf("Excellent"); break;
case 'B': printf("Good"); break; // This runs
case 'C': printf("Average"); break;
default: printf("Try again");
}
// Ternary operator
int max = (a > b) ? a : b; // min one-liner
// Loops
// for loop
for (int i = 1; i <= 5; i++) {
printf("%d ", i); // 1 2 3 4 5
}
// while loop
int n = 1;
while (n <= 5) {
printf("%d ", n++);
}
// do-while (runs at least once)
int choice;
do {
printf("Enter choice (1-3): ");
scanf("%d", &choice);
} while (choice < 1 || choice > 3);
// break and continue
for (int i = 0; i < 10; i++) {
if (i == 5) break; // Exit loop when i=5
if (i % 2 == 0) continue; // Skip even numbers
printf("%d ", i); // 1 3
}
// Function declaration (prototype)
int add(int a, int b);
void printHello(void);
float calculateArea(float r);
// Function definition
int add(int a, int b) {
return a + b;
}
void printHello() { // void = no return value
printf("Hello from BCA!\n");
}
float calculateArea(float r) {
return 3.14159 * r * r;
}
// Recursive function — factorial
long factorial(int n) {
if (n <= 1) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
// factorial(5) = 5 × 4 × 3 × 2 × 1 = 120
// Call by value — original NOT modified
void increment(int x) {
x++; // Only local copy changed
}
// Call by reference (using pointer) — original IS modified
void incrementByRef(int *x) {
(*x)++; // Dereference and modify original
}
int main() {
int num = 10;
increment(num); // num still 10
incrementByRef(&num); // num now 11
printf("%d\n", num); // 11
}
// 1D Array
int marks[5] = {85, 90, 78, 92, 88};
float avg = 0;
for (int i = 0; i < 5; i++)
avg += marks[i];
avg /= 5;
// 2D Array (Matrix)
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("%d\n", matrix[1][2]); // 6
// Strings — char array ending with '\0'
char name[20] = "BCA Student";
printf("%s\n", name); // BCA Student
printf("%lu\n", strlen(name)); // 11 (without \0)
// String functions (string.h)
char s1[20] = "Hello";
char s2[] = "World";
strcat(s1, s2); // s1 = "HelloWorld"
strcpy(s1, "New"); // s1 = "New"
strcmp("abc","abc"); // 0 (equal), <0 (s1<s2), >0 (s1>s2)
strlen("Hello"); // 5
// String input
char city[50];
printf("Enter city: ");
fgets(city, 50, stdin); // Safe — prevents buffer overflow
// Note: fgets includes '\n', use scanf("%49s", city) for single word
int x = 42;
int *p = &x; // p = address of x (e.g., 0x7ffee4b8)
printf("%d\n", x); // 42 — value of x
printf("%p\n", &x); // 0x7ffee4b8 — address of x
printf("%p\n", p); // 0x7ffee4b8 — value of p (= address of x)
printf("%d\n", *p); // 42 — value at address p (dereference)
*p = 100; // Modify x through pointer
printf("%d\n", x); // 100
// Pointer arithmetic
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Points to arr[0]
printf("%d\n", *ptr); // 10
printf("%d\n", *(ptr+1)); // 20 (moves 4 bytes forward)
ptr++;
printf("%d\n", *ptr); // 20
// Array as pointer
for (int i = 0; i < 5; i++) {
printf("%d ", *(arr + i)); // Same as arr[i]
}
// Dynamic memory allocation
int *dynArr = (int*)malloc(5 * sizeof(int)); // Heap allocation
if (dynArr == NULL) { printf("Memory error"); exit(1); }
for (int i = 0; i < 5; i++) dynArr[i] = i * 10;
free(dynArr); // Always free! Memory leak warna
dynArr = NULL; // Good practice
// Structure — each member has own memory
struct Student {
int rollNo;
char name[50];
float gpa;
}; // Note: semicolon required
struct Student s1 = {101, "Rahul", 8.5};
printf("%s - GPA: %.2f\n", s1.name, s1.gpa);
// Typedef for easier use
typedef struct {
int x, y;
} Point;
Point p = {10, 20};
printf("(%d, %d)\n", p.x, p.y);
// Structure with pointer
typedef struct Node {
int data;
struct Node *next; // self-referential
} Node;
// Union — shared memory
union Data {
int i; // 4 bytes
float f; // 4 bytes
char c; // 1 byte
}; // sizeof(union Data) = 4 (max member)
union Data d;
d.i = 42;
printf("%d\n", d.i); // 42
d.f = 3.14f; // Now i is corrupted!
printf("%f\n", d.f); // 3.14
#include <stdio.h>
// Write to file
FILE *fp = fopen("data.txt", "w"); // Open for writing
if (fp == NULL) {
printf("Cannot open file!");
exit(1);
}
fprintf(fp, "Roll: %d, Name: %s\n", 101, "Alice");
fclose(fp); // Always close!
// Read from file
fp = fopen("data.txt", "r");
char line[100];
while (fgets(line, 100, fp) != NULL) {
printf("%s", line);
}
fclose(fp);
// File modes:
// "r" — read, "w" — write (overwrite), "a" — append
// "rb","wb" — binary mode
// fprintf / fscanf
fp = fopen("marks.txt", "w");
for (int i = 0; i < 3; i++) {
int roll; float gpa;
scanf("%d %f", &roll, &gpa);
fprintf(fp, "%d %.2f\n", roll, gpa);
}
fclose(fp);
Q: void pointer kya hota hai? A: void *p — kisi bhi data type ka address store kar sakta hai. Generic pointer. Dereference karne se pehle type cast karna padta hai: (int)p.
Q: Stack aur Heap memory mein kya fark hai C mein? A: Stack — local variables, function calls (automatic management, fast, limited size). Heap — malloc/calloc se dynamic allocation, programmer manages, larger, slower.
Q: Dangling pointer kya hota hai? A: Jab pointer ek freed memory location ko point karta rahe — use-after-free bug. free(ptr); ptr = NULL; se prevent karo.
Complete C Programming notes for BCA Sem 1 — Variables, Operators, Control Flow, Arrays, Strings, Functions, Pointers, File Handling with examples, viva questions, and exam prep.
40 pages · 2.0 MB · Updated 2026-03-11
C sabse foundational language hai — memory management, pointers, data structures sab C se samajh aate hain. C seekhne ke baad Java, Python, C++ asaan ho jaate hain.
Pointer ek variable hai jo doosre variable ki memory address store karta hai. int *p = &x; — p mein x ka address, *p se x ki value access hoti hai.
Call by value: function copy milti hai variable ki — original nahi badlta. Call by reference: pointer pass hota hai — original value badal sakti hai.
Array name essentially ek pointer hai first element ki taraf. arr[i] aur *(arr+i) dono equivalent hain C mein.
Structure: sabhi members ke liye alag memory hoti hai — total size = sum of members. Union: sabhi members ek hi memory share karte hain — size = largest member.
Database Management System Notes — BCA Sem 3
Database Management
Computer Networks Notes — BCA Sem 4
Computer Networks
Java Programming Notes — BCA Sem 4
Java Programming
Operating Systems Notes — BCA Sem 5
Operating Systems
Web Development Notes — BCA Sem 5
Web Development
Your feedback helps us improve notes and tutorials.