This section covers the basic building blocks of the C language.
Data types define the type and size of data a variable can store.
| Data Type | Keyword | Typical Size (bytes) | Description |
|---|---|---|---|
| Integer | int |
2 or 4 | Stores whole numbers (e.g., 5, -10). |
| Character | char |
1 | Stores a single character (e.g., 'a', '$'). |
| Floating Point | float |
4 | Stores single-precision numbers with decimals (e.g., 3.14). |
| Double | double |
8 | Stores double-precision numbers with decimals. |
| No Type | void |
N/A | Used for functions that return nothing or for generic pointers. |
These can be modified with qualifiers like short, long, signed, and unsigned.
An expression is a combination of variables, constants, and operators that evaluates to a single value.
+ (add), - (subtract), * (multiply), / (divide), % (modulus/remainder).== (equal to), != (not equal to), >, <, >=, <=. These evaluate to 1 (true) or 0 (false).&& (logical AND), || (logical OR), ! (logical NOT).= (assigns the value on the right to the variable on the left).C uses standard library functions for I/O. You must #include .
printf() is used to print formatted output to the console.
printf("Hello, World!\n");
printf("The number is: %d\n", myVariable);scanf() is used to read formatted input from the console.
int age;
printf("Enter your age: ");
scanf("%d", &age); // & is the "address-of" operator&) in scanf() is the most common bug for beginners. scanf needs the *address* of the variable to store the value.
A simple C program has a standard structure:
// 1. Preprocessor Directives #include// 2. Main function (execution starts here) int main() { // 3. Variable Declaration int a, b, sum; // 4. Program Logic / Statements printf("Enter two numbers: "); scanf("%d %d", &a, &b); sum = a + b; printf("The sum is: %d\n", sum); // 5. Return Statement return 0; // 0 indicates successful execution }
Control structures allow you to control the flow of your program's execution.
if Statement: Executes code if a condition is true.
if (age >= 18) {
printf("You can vote.");
}if-else Statement: Executes one block if true, another if false.
if (num % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}else if Ladder: Used for multiple exclusive conditions.
if (marks >= 90) {
printf("Grade A");
} else if (marks >= 80) {
printf("Grade B");
} else {
printf("Grade C");
}switch Statement: Efficiently checks a variable against a list of constant values.
switch (day) {
case 1:
printf("Monday");
break; // A break is crucial!
case 2:
printf("Tuesday");
break;
default:
printf("Invalid day");
}switch Mistake: Forgetting break causes "fall-through." If day is 1, the program will print "Monday", then "Tuesday", then "Invalid day".
while Loop: An entry-controlled loop. The condition is checked *before* the loop body executes.
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
// Output: 1 2 3 4 5do-while Loop: An exit-controlled loop. The loop body *always* executes at least once.
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
// Output: 1 2 3 4 5for Loop: The most common loop. It combines initialization, condition, and increment in one line.
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
// Output: 1 2 3 4 5break: Immediately terminates the innermost loop (for, while, do-while) or switch statement. continue: Skips the rest of the current loop iteration and moves to the next iteration. goto: Unconditionally jumps to a labeled statement.
if (error) {
goto cleanup;
}
// ...
cleanup:
printf("Cleaning up resources.");goto sparingly. It can make code very difficult to read and debug. It's generally considered bad practice but is mentioned in the syllabus.
A loop inside another loop. This is commonly used for working with 2D arrays or printing patterns.
// Prints a 3x3 square of stars
for (int i = 0; i < 3; i++) { // Outer loop (rows)
for (int j = 0; j < 3; j++) { // Inner loop (columns)
printf("* ");
}
printf("\n"); // Newline after each row
}
This unit requires applying the above control structures to solve elementary problems, such as:
for loop).for loop and if).while loop).while loop and %).n numbers.