| Data Type | Size | Range | Format |
|---|---|---|---|
| int | 2 or 4 bytes | -32768 to 32767 (2B) / -2B to 2B (4B) | %d |
| float | 4 bytes | 3.4E-38 to 3.4E+38 | %f |
| double | 8 bytes | 1.7E-308 to 1.7E+308 | %lf |
| char | 1 byte | -128 to 127 | %c |
| long int | 4 bytes | -2,147,483,648 to 2,147,483,647 | %ld |
| short int | 2 bytes | -32768 to 32767 | %hd |
| void | 0 bytes | No value | — |
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | 1 (true) |
| != | Not equal to | 5 != 3 | 1 (true) |
| > | Greater than | 7 > 3 | 1 (true) |
| < | Less than | 3 < 7 | 1 (true) |
| >= | Greater than or equal | 5 >= 5 | 1 (true) |
| <= | Less than or equal | 3 <= 7 | 1 (true) |
Relational operators return 1 (true) or 0 (false). Used in conditions of if, while, for.
%d — Format specifier for integer (int) values. Used in printf/scanf to read/write integer data.
%f — Format specifier for float values. Displays 6 decimal places by default.
#include <stdio.h> void main() { int age = 20; float salary = 25000.50; printf("Age = %d\n", age); // Output: Age = 20 printf("Salary = %f\n", salary); // Output: Salary = 25000.500000 printf("Salary = %.2f\n", salary); // Output: Salary = 25000.50 }
| Operator | Name | Example (a=10, b=3) | Result |
|---|---|---|---|
| + | Addition | a + b | 13 |
| - | Subtraction | a - b | 7 |
| * | Multiplication | a * b | 30 |
| / | Division | a / b | 3 (integer division!) |
| % | Modulus (remainder) | a % b | 1 |
| Operator | Name | Example (a=5=0101, b=3=0011) | Result |
|---|---|---|---|
| & | AND | a & b = 0001 | 1 |
| | | OR | a | b = 0111 | 7 |
| ^ | XOR | a ^ b = 0110 | 6 |
| ~ | Complement | ~a | -6 |
| << | Left shift | a << 1 = 1010 | 10 |
| >> | Right shift | a >> 1 = 0010 | 2 |
The conditional operator is the only ternary operator in C (takes 3 operands).
#include <stdio.h> void main() { int a = 10, b = 20; int max = (a > b) ? a : b; printf("Max = %d", max); // Output: Max = 20 // Equivalent if-else: if(a > b) max = a; else max = b; }
Token — The smallest individual unit in a C program that is meaningful to the compiler. A program is a collection of tokens.
Types of Tokens:
Keyword — A word that has a fixed, predefined meaning in C. Cannot be used as variable names. C has 32 keywords.
| Symbol Shape | Name | Use |
|---|---|---|
| Oval / Rounded rect | Terminal | Start / End of flowchart |
| Rectangle | Process | Arithmetic / Assignment operations |
| Diamond | Decision | Condition check (if/while) — Yes/No branches |
| Parallelogram | Input/Output | scanf / printf operations |
| Arrow | Flow line | Shows direction of flow |
| Circle | Connector | Connects parts of large flowcharts |
#include <stdio.h> void main() { int n, i, binary[32], count = 0; printf("Enter a number: "); scanf("%d", &n); printf("Decimal: %d\n", n); printf("Octal: %o\n", n); printf("Hexadecimal: %x\n", n); /* Binary conversion */ int temp = n; while(temp > 0) { binary[count++] = temp % 2; temp = temp / 2; } printf("Binary: "); for(i = count - 1; i >= 0; i--) printf("%d", binary[i]); printf("\n"); }
#include <stdio.h> void main() { float p, r, si; int n; printf("Enter Principal, Time (years), Rate: "); scanf("%f %d %f", &p, &n, &r); si = (p * n * r) / 100; printf("Simple Interest = %.2f", si); }
#include <stdio.h> void main() { int n; printf("Enter number: "); scanf("%d", &n); if(n > 0) printf("Positive"); else if(n < 0) printf("Negative"); else printf("Zero"); }
Nested if-else means an if-else inside another if-else block.
#include <stdio.h> void main() { int a, b, c; printf("Enter three numbers: "); scanf("%d %d %d", &a, &b, &c); if(a > b) { if(a > c) printf("Largest = %d", a); else printf("Largest = %d", c); } else { if(b > c) printf("Largest = %d", b); else printf("Largest = %d", c); } }
#include <stdio.h> void main() { int choice; float a = 10, b = 3; printf("1.Add 2.Sub 3.Mul 4.Div: "); scanf("%d", &choice); switch(choice) { case 1: printf("Sum = %.2f", a+b); break; case 2: printf("Diff = %.2f", a-b); break; case 3: printf("Product = %.2f", a*b); break; case 4: printf("Quotient = %.2f", a/b); break; default: printf("Invalid choice"); } }
Execution order: 1) Initialize (once) → 2) Check condition → 3) Execute body → 4) Update → back to 2
#include <stdio.h> void main() { int i; /* Print 1 to 10 */ for(i = 1; i <= 10; i++) { printf("%d ", i); } /* Multiple statements in init/update */ for(i = 10; i >= 1; i--) { // reverse loop printf("%d ", i); } }
/* while — may not execute */ int i = 10; while(i < 5) { // false, body skipped printf("%d", i); } /* do-while — always executes once */ do { printf("%d", i); // prints 10 once } while(i < 5); // then checks — false, stops
break — Immediately exits the current loop or switch block. Execution continues after the loop.
continue — Skips the remaining statements of the current iteration and moves to the next iteration.
#include <stdio.h> void main() { int i; /* break example */ for(i=1; i<=10; i++) { if(i == 5) break; // stops at 5 printf("%d ", i); // prints: 1 2 3 4 } /* continue example */ for(i=1; i<=10; i++) { if(i == 5) continue; // skips 5 printf("%d ", i); // prints: 1 2 3 4 6 7 8 9 10 } }
#include <stdio.h> void main() { int n, a = 0, b = 1, c, i; printf("Enter number of terms: "); scanf("%d", &n); printf("%d %d ", a, b); for(i = 2; i < n; i++) { c = a + b; printf("%d ", c); a = b; b = c; } } // Output for n=8: 0 1 1 2 3 5 8 13
#include <stdio.h> int fib(int n) { if(n <= 1) return n; return fib(n-1) + fib(n-2); } void main() { int i, n; printf("Enter terms: "); scanf("%d", &n); for(i = 0; i < n; i++) printf("%d ", fib(i)); }
#include <stdio.h> void main() { int n, i, flag = 0; printf("Enter a number: "); scanf("%d", &n); if(n < 2) { printf("Not prime"); return; } for(i = 2; i <= n/2; i++) { if(n % i == 0) { flag = 1; break; } } if(flag == 0) printf("%d is Prime", n); else printf("%d is Not Prime", n); }
Function — A self-contained block of code that performs a specific task, can accept input (parameters), and may return a value. Functions promote reusability and modularity.
int add(int a, int b); // Declaration (prototype) void main() { printf("%d", add(3, 4)); // Function call } // Definition int add(int a, int b) { return a + b; }
/* Call by Value — original unchanged */ void addVal(int x) { x = x + 10; } /* Call by Reference — original changes */ void addRef(int *x) { *x = *x + 10; } void main() { int a = 5; addVal(a); // a is still 5 printf("%d\n", a); // 5 addRef(&a); // a is now 15 printf("%d\n", a); // 15 }
Recursion — When a function calls itself repeatedly until a base condition (termination condition) is reached.
Every recursive function needs: 1) Base case (stopping condition) 2) Recursive call (moves toward base case)
#include <stdio.h> int factorial(int n) { if(n == 0 || n == 1) // base case return 1; return n * factorial(n - 1); // recursive call } void main() { int n; printf("Enter n: "); scanf("%d", &n); printf("%d! = %d", n, factorial(n)); } // factorial(4) = 4*factorial(3) // = 4*3*factorial(2) // = 4*3*2*factorial(1) // = 4*3*2*1 = 24
Advantages of recursion: Code is simpler and shorter; solves complex problems (trees, backtracking) elegantly; mirrors mathematical definitions.
#include <stdio.h> void add(int a, int b) { printf("Sum = %d\n", a + b); } void multiply(int a, int b) { printf("Product = %d\n", a * b); } void main() { int x, y; printf("Enter two numbers: "); scanf("%d %d", &x, &y); add(x, y); multiply(x, y); }
#include <stdio.h> void calculate(float basic) { float da, hra, gross; da = 0.10 * basic; // 10% DA hra = 0.083 * basic; // 8.3% HRA gross = basic + da + hra; printf("Basic = %.2f\n", basic); printf("DA = %.2f\n", da); printf("HRA = %.2f\n", hra); printf("Gross = %.2f\n", gross); } void main() { int empno; float basic; printf("Enter Employee No and Basic Salary: "); scanf("%d %f", &empno, &basic); printf("Employee No: %d\n", empno); calculate(basic); }
Array — A collection of elements of the same data type stored in contiguous memory locations, accessed using a single variable name and an index.
Advantages:
#include <stdio.h> void main() { int a[5] = {10, 20, 30, 40, 50}; int i; // a[0]=10, a[1]=20, ..., a[4]=50 for(i = 0; i < 5; i++) printf("a[%d] = %d\n", i, a[i]); }
A 2D array stores elements in a table format (rows and columns). Also called a matrix.
#include <stdio.h> void main() { /* Initialization at declaration */ int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int i, j; for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) printf("%d ", a[i][j]); printf("\n"); } // Access element at row 1, col 2: a[1][2] = 6 }
#include <stdio.h> void main() { int a[3][3], b[3][3], c[3][3], i, j; printf("Enter Matrix A (3x3):\n"); for(i=0; i<3; i++) for(j=0; j<3; j++) scanf("%d", &a[i][j]); printf("Enter Matrix B (3x3):\n"); for(i=0; i<3; i++) for(j=0; j<3; j++) scanf("%d", &b[i][j]); /* Addition */ for(i=0; i<3; i++) for(j=0; j<3; j++) c[i][j] = a[i][j] + b[i][j]; printf("Sum Matrix:\n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("%d\t", c[i][j]); printf("\n"); } }
#include <stdio.h> void main() { int a[3][3], b[3][3], c[3][3] = {0}; int i, j, k; /* Input a and b matrices here (same as above) */ /* Multiplication: c[i][j] = sum of a[i][k]*b[k][j] */ for(i=0; i<3; i++) for(j=0; j<3; j++) for(k=0; k<3; k++) c[i][j] += a[i][k] * b[k][j]; printf("Result:\n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) printf("%d\t", c[i][j]); printf("\n"); } }
#include <stdio.h> void main() { int a[10], i, max; printf("Enter 10 numbers:\n"); for(i=0; i<10; i++) scanf("%d", &a[i]); max = a[0]; // assume first is largest for(i=1; i<10; i++) { if(a[i] > max) max = a[i]; } printf("Largest = %d", max); }
#include <stdio.h> void main() { int a[10], i, j, temp, n = 10; printf("Enter 10 numbers:\n"); for(i=0; i<n; i++) scanf("%d", &a[i]); /* Bubble sort */ for(i=0; i<n-1; i++) { for(j=0; j<n-i-1; j++) { if(a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } printf("Sorted: "); for(i=0; i<n; i++) printf("%d ", a[i]); }
A string in C is an array of characters terminated by a null character '\0'.
/* Method 1: char array with initialization */ char name[20] = "Hello"; /* Method 2: char array with char list */ char name[] = {'H','e','l','l','o','\0'}; /* Method 3: pointer to string literal */ char *name = "Hello"; /* Input using scanf (no spaces) */ scanf("%s", name); /* Input using gets (includes spaces) */ gets(name); /* Output */ printf("%s", name); puts(name); // same as printf with newline
strlen(s) — Returns the length of string s (number of characters before '\0').
strcpy(dest, src) — Copies src string into dest string, including '\0'.
#include <stdio.h> #include <string.h> void main() { char s1[20] = "Hello"; char s2[20]; printf("Length = %lu\n", strlen(s1)); // 5 strcpy(s2, s1); printf("Copied: %s\n", s2); // Hello }
strcmp(s1, s2) — Compares two strings. Returns 0 if equal, positive if s1 > s2, negative if s1 < s2.
strcat(s1, s2) — Appends (concatenates) s2 to the end of s1. s1 must be large enough.
#include <stdio.h> #include <string.h> void main() { char s1[50] = "Hello"; char s2[] = " World"; /* strcmp */ if(strcmp(s1, s2) == 0) printf("Strings are equal\n"); else printf("Strings are different\n"); /* strcat */ strcat(s1, s2); printf("Concatenated: %s\n", s1); // Hello World }
#include <stdio.h> #include <string.h> void main() { char s1[100], s2[50]; printf("Enter string 1: "); scanf("%s", s1); printf("Enter string 2: "); scanf("%s", s2); printf("Length of s1 = %lu\n", strlen(s1)); printf("Length of s2 = %lu\n", strlen(s2)); strcat(s1, s2); printf("Concatenated string: %s\n", s1); }
#include <stdio.h> #include <string.h> void main() { char s[50], rev[50]; printf("Enter a string: "); scanf("%s", s); strcpy(rev, s); strrev(rev); // reverses string if(strcmp(s, rev) == 0) printf("\"%s\" is a Palindrome", s); else printf("\"%s\" is NOT a Palindrome", s); } // "madam" reversed = "madam" → Palindrome // "hello" reversed = "olleh" → Not palindrome
#include <stdio.h> void main() { char src[50], dest[50]; int i = 0; printf("Enter string: "); scanf("%s", src); while(src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; // important: add null terminator printf("Copied: %s", dest); }
Pointer — A variable that stores the memory address of another variable instead of storing a data value directly.
& (Address-of operator) — Returns the memory address of a variable. Used to initialize pointers.
* (Dereference/Indirection operator) — Accesses/modifies the value at the address stored in a pointer. Also used to declare a pointer variable.
#include <stdio.h> void main() { int a = 50; int *p; // p is a pointer to int p = &a; // p stores address of a printf("Value of a = %d\n", a); // 50 printf("Address of a= %p\n", &a); // e.g. 0x7ffd printf("p holds = %p\n", p); // same address printf("Value via p = %d\n", *p); // 50 (dereferencing) *p = 100; // changes a through pointer printf("New a = %d\n", a); // 100 }
Arithmetic operations that can be performed on pointers:
| Operation | Meaning | Example (int *p, 4-byte int) |
|---|---|---|
| p++ or p+1 | Move to next element | Address increases by 4 |
| p-- or p-1 | Move to previous element | Address decreases by 4 |
| p + n | Move n elements forward | Address + n*sizeof(int) |
| p2 - p1 | Number of elements between | Returns integer count |
#include <stdio.h> void main() { int a[5] = {10,20,30,40,50}; int *p = a; // p points to a[0] printf("%d\n", *p); // 10 (a[0]) p++; printf("%d\n", *p); // 20 (a[1]) p += 2; printf("%d\n", *p); // 40 (a[3]) p--; printf("%d\n", *p); // 30 (a[2]) }
#include <stdio.h> void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void main() { int x, y; printf("Enter two numbers: "); scanf("%d %d", &x, &y); printf("Before: x=%d y=%d\n", x, y); swap(&x, &y); printf("After: x=%d y=%d\n", x, y); }
#include <stdio.h> void main() { int a[5] = {10,20,30,40,50}; int *p, i; p = a; // or p = &a[0]; printf("Array elements using pointer:\n"); for(i = 0; i < 5; i++) printf("a[%d] = %d\n", i, *(p+i)); /* These are all equivalent: */ // a[i] == *(a+i) == *(p+i) == p[i] }
#include <stdio.h> void main() { int a, b, product; int *p1, *p2; printf("Enter two numbers: "); scanf("%d %d", &a, &b); p1 = &a; p2 = &b; product = (*p1) * (*p2); printf("Product = %d", product); }
Structure — A user-defined data type that groups together variables of different data types under a single name. Each variable inside is called a member.
Features:
#include <stdio.h> struct student { int roll_no; char name[50]; float marks; }; void main() { struct student s; printf("Enter roll no, name, marks: "); scanf("%d %s %f", &s.roll_no, s.name, &s.marks); printf("Roll No: %d\n", s.roll_no); printf("Name: %s\n", s.name); printf("Marks: %.2f\n", s.marks); }
An array of structures allows storing multiple records of the same structure type.
#include <stdio.h> struct book { int book_no; char book_title[50]; float book_price; }; void main() { struct book b[10]; int i; for(i = 0; i < 10; i++) { printf("Enter Book %d (no title price): ", i+1); scanf("%d %s %f", &b[i].book_no, b[i].book_title, &b[i].book_price); } printf("\n--- Book List ---\n"); for(i = 0; i < 10; i++) { printf("%d %-20s %.2f\n", b[i].book_no, b[i].book_title, b[i].book_price); } }
#include <stdio.h> struct DATE { int day; int month; int year; }; void main() { /* Initialization at declaration */ struct DATE d = {15, 8, 2024}; printf("Date: %d/%d/%d", d.day, d.month, d.year); // Output: Date: 15/8/2024 }
#include <stdio.h> #define PI 3.14159 struct circle { float radius; float area; float perimeter; }; void main() { struct circle c; printf("Enter radius: "); scanf("%f", &c.radius); c.area = PI * c.radius * c.radius; c.perimeter = 2 * PI * c.radius; printf("Area = %.2f\n", c.area); printf("Perimeter = %.2f\n", c.perimeter); }
The arrow operator (->) is used to access structure members through a pointer to a structure.
ptr->member is equivalent to (*ptr).member
#include <stdio.h> struct student { int roll; char name[30]; }; void main() { struct student s = {10, "Riya"}; struct student *p = &s; // pointer to struct /* Two equivalent ways to access: */ printf("%d %s\n", (*p).roll, (*p).name); // old way printf("%d %s\n", p->roll, p->name); // arrow way (preferred) }
#include <stdio.h> struct student { int roll_no; char name[50]; float m1, m2, m3; }; void main() { struct student s; float total, percent; printf("Enter Roll No: "); scanf("%d", &s.roll_no); printf("Enter Name: "); scanf("%s", s.name); printf("Enter marks in 3 subjects: "); scanf("%f %f %f", &s.m1, &s.m2, &s.m3); total = s.m1 + s.m2 + s.m3; percent = (total / 300.0) * 100; printf("Roll No: %d\n", s.roll_no); printf("Name: %s\n", s.name); printf("Total: %.2f\n", total); printf("Percent: %.2f%%\n", percent); }
#include <stdio.h> struct account { int account_no; char account_type[20]; // Savings/Current char account_name[50]; float account_balance; }; void main() { struct account a; printf("Enter Account No: "); scanf("%d", &a.account_no); printf("Enter Account Type: "); scanf("%s", a.account_type); printf("Enter Account Name: "); scanf("%s", a.account_name); printf("Enter Balance: "); scanf("%f", &a.account_balance); printf("\nAccount No: %d\n", a.account_no); printf("Account Type: %s\n", a.account_type); printf("Account Name: %s\n", a.account_name); printf("Balance: %.2f\n", a.account_balance); }
#include <stdio.h> void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } void main() { int x, y; scanf("%d %d", &x, &y); swap(&x, &y); printf("x=%d y=%d", x, y); }
#include <stdio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; printf("Enter Matrix A:\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("Enter Matrix B:\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; printf("Sum:\n"); for(i=0;i<3;i++){for(j=0;j<3;j++)printf("%d ",c[i][j]);printf("\n");} }
#include <stdio.h> int add(int a, int b) { return a+b; } void main() { int x,y; scanf("%d %d",&x,&y); printf("Sum=%d",add(x,y)); }
#include <stdio.h> int fact(int n) { if(n<=1) return 1; return n*fact(n-1); } void main() { int n; scanf("%d",&n); printf("%d!=%d",n,fact(n)); }
#include <stdio.h> int fib(int n){if(n<=1)return n;return fib(n-1)+fib(n-2);} void main(){ int i,n; scanf("%d",&n); for(i=0;i<n;i++) printf("%d ",fib(i)); }
#include <stdio.h> void main(){ int n; scanf("%d",&n); if(n>0) printf("Positive"); else if(n<0) printf("Negative"); else printf("Zero"); }
#include <stdio.h> struct student { int roll; char name[50]; float marks; }; void main(){ struct student s; scanf("%d %s %f",&s.roll,s.name,&s.marks); printf("Roll:%d Name:%s Marks:%.2f",s.roll,s.name,s.marks); }
#include <stdio.h> void main(){ int a,b,c; scanf("%d%d%d",&a,&b,&c); if(a>b){ if(a>c)printf("Largest=%d",a); elseprintf("Largest=%d",c); } else{ if(b>c)printf("Largest=%d",b); elseprintf("Largest=%d",c); } }
#include <stdio.h> #include <string.h> void main(){ char s1[100],s2[50]; scanf("%s%s",s1,s2); printf("Len1=%lu Len2=%lu\n",strlen(s1),strlen(s2)); strcat(s1,s2); printf("Concat:%s",s1); }
#include <stdio.h> void main(){ int n,i; scanf("%d",&n); for(i=1;i<=10;i++) printf("%d x %d = %d\n",n,i,n*i); }
#include <stdio.h> #define PI 3.14159 struct circle { float r,a,p; }; void main(){ struct circle c; scanf("%f",&c.r); c.a=PI*c.r*c.r; c.p=2*PI*c.r; printf("Area=%.2f Perimeter=%.2f",c.a,c.p); }
#include <stdio.h> void main(){ int n,i,sum=0; scanf("%d",&n); for(i=1;i<=n;i++) sum+=i; printf("Sum=%d",sum); } /* Sum of digits of a number */ void sumDigits(){ int n,sum=0; scanf("%d",&n); while(n!=0){ sum+=n%10; n/=10; } printf("Sum of digits=%d",sum); }
#include <stdio.h> void main(){ char c; scanf("%c",&c); if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'|| c=='A'||c=='E'||c=='I'||c=='O'||c=='U') printf("Vowel"); else printf("Consonant"); }
#include <stdio.h> void main(){ float basic,da,ta,gross; printf("Enter basic salary: "); scanf("%f",&basic); da=0.05*basic; // 5% DA ta=0.15*basic; // 15% TA gross=basic+da+ta; printf("DA=%.2f TA=%.2f Gross=%.2f",da,ta,gross); }
#include <stdio.h> #include <math.h> void main(){ float a,b,c,disc,r1,r2; scanf("%f%f%f",&a,&b,&c); disc = b*b - 4*a*c; if(disc > 0){ r1=(-b+sqrt(disc))/(2*a); r2=(-b-sqrt(disc))/(2*a); printf("Roots: %.2f and %.2f",r1,r2); } else if(disc==0){ r1=-b/(2*a); printf("Equal roots: %.2f",r1); } else printf("Imaginary roots"); }