Quick Revision
Compressed notes for fast review.
C at a glance
- Structured, procedural, fast, portable
- Dennis Ritchie (1972)
- Systems + general apps
Program structure
- Sections: comments, includes, globals, main(), user functions
Keywords / Identifiers / Constants
- Keywords: reserved words (int, if, return, ...)
- Identifiers: start letter/_; case-sensitive; not keywords
- Constants: 10, 3.14, 'A', "Hi"
Data types
- Basic: int, float, double, char
- Derived: arrays, pointers
- Void: no value
Operators (core)
- Arithmetic: + - * / %
- Relational:
==!=><>=<= - Logical: && || !
- Assignment: = += -= *= /=
- Inc/Dec: ++ --
- Conditional: ?:
Control structures
- Decision: if, if-else, switch
- Loops: for, while, do-while
- Jump: break, continue, return
Functions
- Reusable block, may return value
- Call by value (copy) vs call by reference (address)
Arrays
- Fixed-size, same type, 0-based index
Strings
- Char array ending with \0
- Common: strlen, strcpy, strcmp, strcat
Pointers (basics)
- Store addresses; dereference with *
File handling (basics)
- fopen, fclose, fprintf, fscanf
- Modes: "r" read, "w" write, "a" append, "r+" read+write
Key differences (fast)
- While vs Do-while: entry vs exit controlled; do-while runs at least once
- For vs While: count known vs unknown; for packs init/cond/update
- Array vs String: array any type; string is char array ending \0
- Array vs Pointer: array stores values; pointer stores address
- Call by Value vs Reference: copy vs address; safe vs can modify
- Local vs Global: inside function vs outside; limited life vs whole program
Common patterns (from practice)
- Sum/reverse/rotate arrays; merge arrays
- Prime check (count divisors); sum of primes
- Factorial loop; AP terms
- GCD (Euclid); sum of digits (recursive)
- File sums/stats; string palindrome/vowel count
Common Syntax Solutions
Basic Program Structure
#include <stdio.h>
int main()
{
// Variable declarations
int n, temp;
// Input
printf("Enter value ->");
scanf("%d", &n);
// Processing
// ...
// Output
printf("Result is %d", result);
return 0;
}Array Operations
// Array declaration and input
int arr[n];
printf("Enter %d numbers->", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Array traversal
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}Pointer Operations
// Pointer declaration and assignment
int *p = &arr[j];
int *p = arr; // points to first element
// Pointer dereferencing
if (*p > *(p + 1))
{
temp = *p;
*p = *(p + 1);
*(p + 1) = temp;
}
// Accessing elements via pointer
largest = *(p + n - 1);
smallest = *p;Loop Patterns
// For loop - known iterations
for (int i = 0; i < n; i++)
{
// process arr[i]
}
// Nested loops - sorting
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
// bubble sort logic
}
}
// While loop - unknown iterations
while (test != 0)
{
// process until condition
test /= 10;
}Conditional Statements
// Simple if-else
if (count == 2)
{
printf("Prime\n");
}
else
{
printf("Not Prime\n");
}
// Multiple conditions
if (num == 1 || num <= 0)
{
printf("Invalid input\n");
}
else if (count == 2)
{
printf("Prime\n");
}
else
{
printf("Not Prime\n");
}File Operations
// File opening and reading
FILE *file;
file = fopen("filename.txt", "r");
if (file != NULL)
{
while (fscanf(file, "%d", &x) == 1)
{
// process each number
}
fclose(file);
}String Operations
// String length calculation
int length = 0;
while (str[length] != '\n')
{
length++;
}
// String palindrome check
for (int i = 0; i < length; i++)
{
if (str[i] != str[length - 1 - i])
{
count = 1;
break;
}
}Common Algorithms
// Prime check
int count = 0;
for (int j = 1; j <= num; j++)
{
if (num % j == 0)
{
count++;
}
}
if (count == 2) // prime
// Sum of digits
int sum = 0;
while (temp != 0)
{
sum += temp % 10;
temp /= 10;
}
// Factorial
int factorial = 1;
for (int i = 1; i <= n; i++)
{
factorial *= i;
}Input/Output Patterns
// Multiple inputs
scanf("%d %d", &a, &b);
// Array input with prompt
printf("Enter %d numbers->", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Formatted output
printf("Largest is %d and smallest is %d", largest, smallest);