UNIHIVE

Study Hub UNIPORT MEG 202

MEG 202 · Basic Software Engineering

Writing programs in C

Structure of a C program, variables and types, input and output, decisions, loops, functions and a complete engineering example.

Our notes

2 min read

Why C

C (Dennis Ritchie, Bell Labs, 1972) is compact, fast and close to the hardware, so it is used for operating systems, embedded controllers (microcontrollers in machines) and numerical code. Learning it teaches how memory and data types really work. FORTRAN, Pascal and Visual Basic follow the same ideas with different syntax.

Structure of a program

    #include <stdio.h>
    int main(void) {
        printf("Hello, engineers\n");
        return 0;
    }

  • The include line brings in the standard input/output library.
  • Every program starts at main.
  • Statements end with a semicolon; blocks are in braces.
  • Steps: write the source file (.c) → compile (for example gcc prog.c -o prog) → run.

Variables and types

    int n = 10; integer
    double force = 2.5e3; real number (double precision)
    char grade = 'A'; single character
Rules: declare before use; names are case-sensitive; integer division truncates (7 / 2 is 3, but 7.0 / 2 is 3.5).

Input and output

    scanf("%lf", &force);
    printf("Force = %.2f N\n", force);
Format codes: %d integer, %lf (read) or %f (print) double, %c character. The & gives the variable's address so scanf can store into it.

Decisions

    if (stress > yield)
        printf("Fails\n");
    else
        printf("Safe\n");
Comparison operators: ==, !=, <, >, <=, >=; logical: && (and), || (or), ! (not).

Loops

    for (i = 1; i <= 10; i++) { ... } counted loop
    while (error > tol) { ... } condition first
    do { ... } while (x < 0); body at least once

Functions

    double stress(double F, double A) {
        return F / A;
    }
Parameters are passed by value (copies); to let a function change a caller's variable, pass its address (a pointer).

A complete example

Compute the shear stress in solid shafts, τ = 16T / (πd³), for torques in a list, and flag any above 40 MPa.
    #include <stdio.h>
    #define PI 3.14159265
    int main(void) {
        double T[3] = {500.0, 900.0, 1500.0};
        double d = 0.05;
        for (int i = 0; i < 3; i++) {
            double tau = 16.0 * T[i] / (PI * d * d * d);
            printf("T = %.0f N m, tau = %.1f MPa%s\n",
                   T[i], tau / 1e6, tau > 40e6 ? " TOO HIGH" : "");
        }
        return 0;
    }
Hand check: d³ = 1.25 × 10⁻⁴ m³; πd³ = 3.927 × 10⁻⁴. For T = 500: τ = 8,000 / 3.927 × 10⁻⁴ = 20.4 MPa. For 900: 36.7 MPa. For 1,500: 61.1 MPa (flagged TOO HIGH). ✓

Good practice

Meaningful names, comments, consistent indentation, constants instead of "magic numbers", checking input, testing with known answers (as above), and keeping functions short.

Exams usually ask

  • Write a short C program with input, a loop and a decision.
  • Explain the difference between = and ==, and between integer and real division.
  • Trace a given program and state its output.

Common mistake

Writing if (x = 5) instead of if (x == 5). The first assigns 5 to x and is always true; the compiler may only warn.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All MEG 202 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.