C Programming Solutions
Chapter 3: Looping
1. while vs. do-while:
- while checks condition first; may not execute.
- do-while executes at least once.
Example:
while (x < 5) { ... }
do { ... } while (x < 5);
2. for, while, do-while:
- for: Known iterations.
- while: Unknown iterations.
- do-while: Executes at least once.
Chapter 4: Conditional & Branching
1. switch-case:
switch(x) {
case 1: ...; break;
default: ...
}
2. break vs. continue:
- break: Exits loop.
- continue: Skips iteration.
Example:
for (int i=1; i<=5; i++) {
if (i == 3) continue;
printf("%d ", i);
}
Chapter 5: Functions & Parameters
1. Call by Value vs. Reference:
- Value: Function copies value.
- Reference: Function modifies original.
Example:
void update(int *x) { *x = 10; }
2. Recursion:
int factorial(int n) {
if (n==0) return 1;
return n * factorial(n-1);
}
Chapter 6: Arrays, Strings, Structures
1. One-Dimensional vs. Two-Dimensional Arrays:
int arr[5] = {1, 2, 3, 4, 5};
int mat[2][3] = {{1, 2, 3}, {4, 5, 6}};
2. Pointers:
int a = 10, *ptr = &a;
printf("%d", *ptr); // 10
3. File Handling:
FILE *fp = fopen("[Link]", "w");
fprintf(fp, "Hello");
fclose(fp);
Chapter 7: Advanced Concepts
1. Dynamic Memory Allocation:
int *ptr = (int*) malloc(5 * sizeof(int));
free(ptr);
2. Command-line Arguments:
int main(int argc, char *argv[]) { printf("%s", argv[1]); }
3. Stack vs. Heap:
- Stack: Automatic allocation, fast.
- Heap: Manual allocation, slower but flexible.
4. typedef & enum:
typedef unsigned int uint;
enum Days {SUN, MON, ...}; enum Days today = WED;