#include <stdio.h>
int main() {
printf("Hello ESE 124!\n");
return 0;
}
Common Errors:
Missing #include <stdio.h>
Typo in main or missing return 0;
Exercise: Modify the program to ask for the user's name and print "Hello, <name>!".
Solution
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
scanf("%49s", name);
printf("Hello, %s!\n", name);
return 0;
}
Data Types & ASCII
int age;
float temp;
char letter;
printf("ASCII of A is %d\n", 'A');
Common Errors:
Using wrong specifier: %f vs %d.
Buffer overflow when reading char name[].
Exercise: Print ASCII values of characters '0' through '9'.
Solution
#include <stdio.h>
int main() {
for (char c = '0'; c <= '9'; c++) {
printf("%c = %d\n", c, c);
}
return 0;
}
Control Statements
int x;
printf("Enter a number: ");
scanf("%d", &x);
if (x % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
Common Errors:
Missing braces { } for multi-line blocks.
Using = instead of == in conditions.
Exercise: Create a switch-case to print the day of the week for 1–7.
Solution
#include <stdio.h>
int main() {
int d;
scanf("%d", &d);
switch (d) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid\n");
}
return 0;
}
Arrays
int nums[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", nums[i]);
}
printf("\n");
Common Errors:
Index out of range (e.g., i <= 5).
Exercise: Write a function to find the maximum value in an array.
Solution
int find_max(int a[], int n) {
int mx = a[0];
for (int i = 1; i < n; i++) {
if (a[i] > mx) mx = a[i];
}
return mx;
}
int main() {
int arr[] = {3, 7, 2, 9, 5};
printf("Max = %d\n", find_max(arr, 5));
return 0;
}
Functions
int add(int a, int b) {
return a + b;
}
int main() {
printf("Sum = %d\n", add(2, 3));
return 0;
}
Common Errors:
Missing prototype if function defined after main.
Exercise: Implement a recursive function to compute factorial(n).
Solution
long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
printf("5! = %ld\n", factorial(5));
return 0;
}
Pointers
int x = 10;
int *p = &x;
printf("Value = %d\n", *p);
Common Errors:
Dereferencing a NULL or uninitialized pointer.
Exercise: Write a function to swap two integers via pointers.
Solution
swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
int main() {
int a = 5, b = 7;
swap(&a, &b);
printf("a=%d, b=%d\n", a, b);
return 0;
}
Function Pointers
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int main() {
int (*op)(int, int);
op = &add;
printf("5 + 3 = %d\n", op(5, 3));
op = ⊂
printf("5 - 3 = %d\n", op(5, 3));
return 0;
}
Common Errors:
Mismatched signature for function pointers
Omitting parentheses in calls: op(...)
Exercise: Write mul and divide, select via an array of pointers. Solution
#include <stdio.h>
int mul(int a, int b) { return a * b; }
int divide(int a, int b) { return a / b; }
int main() {
int (*ops[2])(int, int) = { &mul, ÷ };
printf("6 * 7 = %d\n", ops[0](6, 7));
printf("6 / 2 = %d\n", ops[1](6, 2));
return 0;
}