MEG 202 · Basic Software Engineering
Our notes
2 min readWhy 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
-
Best pick Introduction to Computer Science, 4.2 Building C Programs
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 4.2 Building C Programs, CC BY-NC-SA 4.0 · licence
-
C (programming language)
(opens en.wikipedia.org in a new tab)
Wikipedia, C (programming language), CC BY-SA 4.0 · licence
-
NOUN CIT 215: Introduction to Programming Language
NOUN course book (PDF, about 6.2 MB: best on Wi-Fi). Syntax, semantics and translation.
(opens nou.edu.ng in a new tab)
-
NOUN CIT 237: Programming and Algorithms
NOUN course book (PDF, about 1.6 MB). Designing and writing algorithms.
(opens nou.edu.ng in a new tab)
-
OpenStax, Introduction to Computer Science (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Computer Science (whole free textbook), CC BY-NC-SA 4.0 · licence
Watch
-
C_01 Introduction to C Language | C Programming Tutorials
12 min · ≈ 54 MB · about ₦30
(opens youtube.com in a new tab)
-
Writing and executing your first program: C Programming Tutorial 03
9 min · ≈ 41 MB · about ₦23
(opens youtube.com in a new tab)
-
C Programming Tutorial 1 - Intro to C
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
-
Function Basics | C Programming Tutorial
16 min · ≈ 72 MB · about ₦40
(opens youtube.com in a new tab)
-
C Programming Tutorial for Beginners
3 h 46 min · ≈ 1017 MB · about ₦571
(opens youtube.com in a new tab)
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.