Unit 1: Fundamentals of Computer Programming with C

Table of Contents

1.1 Fundamentals: Data Types, Expressions, Operations

Data Types

Data types define the type and size of data a variable can store.

TypeDescriptionExample
intInteger (whole number)int age = 21;
floatSingle-precision floating point (decimal number)float price = 19.99;
doubleDouble-precision floating point (more precise)double pi = 3.14159265;
charSingle characterchar grade = 'A';

Expressions

An expression is a combination of variables, constants, and operators that evaluates to a single value. Example: (a + b) / 2.

Operations (Operators)

1.2 Input and Output (I/O)

Handled using standard library functions from <stdio.h>.

Output: printf()

Used to print formatted output to the console.

printf("Hello, %s! You are %d years old.\n", "Alex", 25);
Output: Hello, Alex! You are 25 years old.
  • %d for integers
  • %f for floats/doubles
  • %c for characters
  • %s for strings
  • \n for a new line

Input: scanf()

Used to read formatted input from the console.

Critical: scanf() requires the address of the variable, using the & (address-of) operator.
int age;
printf("Enter your age: ");
scanf("%d", &age);

1.3 Writing Simple C Programs

A basic C program structure:

// 1. Preprocessor Directive
#include <stdio.h>

// 2. Main function (entry point of the program)
int main() {
    // 3. Variable declaration
    int num1 = 10;
    int num2 = 20;
    int sum;

    // 4. Logic
    sum = num1 + num2;

    // 5. Output
    printf("The sum of %d and %d is %d\n", num1, num2, sum);

    // 6. Return statement
    return 0;
}

1.4 Control Structures

These control the flow of program execution.

Decision Making

  • if-else: Used for conditional branching.
    if (score >= 50) {
        printf("Pass\n");
    } else {
        printf("Fail\n");
    }
  • switch: Used to select one of many code blocks to be executed.
    Common Mistake: Forgetting the break; statement in a switch case, which causes "fall-through."
    switch (grade) {
        case 'A':
            printf("Excellent\n");
            break;
        case 'B':
            printf("Good\n");
            break;
        default:
            printf("Try harder\n");
    }

Loops (Iteration)

  • while loop (Entry-controlled): Executes as long as the condition is true.
    int i = 1;
    while (i <= 5) {
        printf("%d ", i);
        i++;
    }
  • do-while loop (Exit-controlled): Always executes at least once.
    int i = 1;
    do {
        printf("%d ", i);
        i++;
    } while (i <= 5);
  • for loop: The most common loop for a known number of iterations.
    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }
  • Nested Loops: A loop inside another loop. Used for 2D patterns, matrices, etc.

Jump Statements

  • break: Immediately exits the innermost loop or switch statement.
  • continue: Skips the rest of the current iteration and moves to the next iteration of the loop.
  • goto: Jumps to a labeled statement.
    Using goto is highly discouraged as it makes code unreadable and hard to debug (spaghetti code).

1.5 Solving Elementary Programming Problems

Using the above structures to solve problems from mathematics and statistics.

Example: Find the factorial of a number (Mathematics)

int n = 5;
long factorial = 1;
for (int i = 1; i <= n; i++) {
    factorial = factorial * i;
}
printf("Factorial of %d = %ld\n", n, factorial);

Example: Calculate the average of 5 numbers (Statistics)

int count = 5;
float sum = 0;
float num;
printf("Enter %d numbers:\n", count);
for (int i = 0; i < count; i++) {
    scanf("%f", &num);
    sum += num;
}
printf("Average = %.2f\n", sum / count);