0% found this document useful (0 votes)
4 views9 pages

C Programming Guide

This document serves as a beginner's guide to C programming, outlining essential syntax, structure, and exam tips for writing effective C code. Key topics include the C program skeleton, data types, input/output functions, decision-making with if/else and switch statements, loops, functions, and arrays, along with must-know programs and a quick reference section. It emphasizes the importance of correct structure and logic in programming to earn marks in exams.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

C Programming Guide

This document serves as a beginner's guide to C programming, outlining essential syntax, structure, and exam tips for writing effective C code. Key topics include the C program skeleton, data types, input/output functions, decision-making with if/else and switch statements, loops, functions, and arrays, along with must-know programs and a quick reference section. It emphasizes the importance of correct structure and logic in programming to earn marks in exams.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C PROGRAMMING

Beginner's Syntax Guide & Exam Cheatsheet


Based on CSC 305 Past Questions — FUTA School of Computing
Write it short. Write it right. Get the marks.

1. The C Program Skeleton


Every C program must follow this structure. Never skip any part of it.

EVERY C PROGRAM MUST LOOK LIKE THIS

#include <stdio.h> // always needed

int main() {
// your code goes here
return 0;
}

EXAM TIP: Even if your logic is wrong, correct structure earns marks. Always write this first.

Header When to Add It


#include <stdio.h> Always — enables printf() and
scanf()
#include <math.h> When using pow(), sqrt(), log()
#include <string.h> When using strlen(), strcpy(),
strcmp()

2. Variables & Data Types


A variable stores a value. Declare its type before using it.

DECLARING VARIABLES

int x; // whole number e.g. 5, -3, 100


float r; // decimal e.g. 3.14, 0.5
char c; // one character e.g. 'A', 'z'
char name[50]; // text/string e.g. "David"

EXAM TIP: Declare ALL variables at the top of main(), before writing any other code.

3. Input & Output


printf() shows output. scanf() reads keyboard input.

PRINTF & SCANF

printf("Enter a number: "); // show message, no newline


scanf("%d", &x); // read integer into x
scanf("%f", &r); // read float into r

printf("Answer = %d\n", x); // print integer


printf("Rate = %.2f\n", r); // print float, 2 decimal places
printf("Name = %s\n", s); // print string

Symbol Type Example


%d int printf("%d", x) → 42
%f float printf("%f", r) →
3.140000
%.2f float printf("%.2f", r) →
3.14
%c char printf("%c", c) → A
%s string printf("%s", s) →
David

EXAM TIP: Always use & in scanf for int and float. Never use & for char arrays (strings).

4. if / else — Making Decisions


Runs different code based on a condition being true or false.

EXAM EXAMPLE: Add 100 if x >= 10, subtract 50 otherwise


#include <stdio.h>

int modify(int x) {
if (x >= 10)
return x + 100;
else
return x - 50;
}

int main() {
int x;
printf("Enter x: ");
scanf("%d", &x);
printf("Result = %d\n", modify(x));
return 0;
}

Operator Meaning Example


== equals x == 10
!= not equal x != 0
> greater than x > 5
>= greater or equal x >= 10
< less than x < 100
&& AND (both true) x>0 && x<10
|| OR (one true) x<0 || x>100

5. switch — Multiple Choices


Cleaner than many if/else when checking one variable against fixed values.

EXAM EXAMPLE: Award Bursary by Student Level

#include <stdio.h>

int main() {
int level;
printf("Enter level: ");
scanf("%d", &level);

switch (level) {
case 100: printf("Bursary: N5000\n"); break;
case 200: printf("Bursary: N7000\n"); break;
case 300: printf("Bursary: N9000\n"); break;
case 400: printf("Bursary: N11000\n"); break;
case 500: printf("Bursary: N13000\n"); break;
default: printf("Invalid level\n");
}
return 0;
}

EXAM TIP: Always write 'break;' after each case. Always write 'default:' at the end.

6. Loops
for — when you know the count
FOR LOOP

for (int i = 1; i <= 5; i++) {


printf("%d\n", i); // prints 1 2 3 4 5
}

while — checks condition BEFORE running


WHILE LOOP (from past paper: print i, decrease by 1 until i = 4)

int i = 10;
while (i >= 4) {
printf("%d\n", i);
i--;
}

do-while — runs AT LEAST ONCE


DO-WHILE LOOP (useful for menus and input validation)

int x;
do {
printf("Enter positive number: ");
scanf("%d", &x);
} while (x <= 0); // keeps asking if x is not positive

Loop Use When Checks Condition


for Count is known (e.g. 5 Before each run
times)
while Count unknown, may skip Before each run
entirely
do-while Must run at least once After each run
(menus etc.)

7. Functions
Write once, call many times. Functions keep your code clean.

FUNCTION STRUCTURE

returnType functionName(paramType param) {


// code
return value; // leave out if returnType is void
}

EXAM EXAMPLE: Return Average of 5 Numbers

#include <stdio.h>

float average() {
int i;
float num, sum = 0;
for (i = 0; i < 5; i++) {
printf("Enter number %d: ", i+1);
scanf("%f", &num);
sum += num;
}
return sum / 5;
}

int main() {
printf("Average = %.2f\n", average());
return 0;
}

EXAM TIP: 'void' means function returns nothing. Always show the function being called
from main() in your answer.

8. Arrays
An array stores many values of the same type under one name. Index starts at 0.
EXAM EXAMPLE: Find Largest and Smallest from 100 Numbers

#include <stdio.h>

int main() {
int arr[100], i, max, min;

for (i = 0; i < 100; i++)


scanf("%d", &arr[i]);

max = min = arr[0];


for (i = 1; i < 100; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
printf("Max=%d Min=%d\n", max, min);
return 0;
}

9. Must-Know Programs from Past Papers


Mortgage Repayment (appears in EVERY paper)
R = A x (1 + r/100)^n / [100 x ((1 + r/100)^n - 1)]

#include <stdio.h>
#include <math.h>

int main() {
float A, r, R; int n;
printf("Enter Amount, Rate, Years: ");
scanf("%f %f %d", &A, &r, &n);
float top = pow(1 + r/100, n);
float bottom = 100 * (pow(1 + r/100, n) - 1);
R = A * (top / bottom);
printf("Repayment = %.2f\n", R);
return 0;
}

Fibonacci Numbers
F1=1, F2=1, every next term = sum of the two before it

#include <stdio.h>
int main() {
int n, f1=1, f2=1, next, i;
printf("How many terms? "); scanf("%d", &n);
printf("%d %d ", f1, f2);
for (i = 3; i <= n; i++) {
next = f1 + f2;
printf("%d ", next);
f1 = f2; f2 = next;
}
return 0;
}

Quadratic Roots
disc = b*b - 4*a*c (discriminant decides root type)

#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, disc, r1, r2;
printf("Enter a b c: "); scanf("%f %f %f", &a, &b, &c);
disc = b*b - 4*a*c;
if (disc > 0) {
r1 = (-b + sqrt(disc))/(2*a);
r2 = (-b - sqrt(disc))/(2*a);
printf("Roots: %.2f and %.2f\n", r1, r2);
} else if (disc == 0) {
printf("Equal root: %.2f\n", -b/(2*a));
} else { printf("Imaginary roots\n"); }
return 0;
}

Sum 1 to 1000, Skip multiples of 3 AND 5


USE: continue — skips the rest of the current loop turn

#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 1000; i++) {
if (i % 3 == 0 && i % 5 == 0) continue;
sum += i;
}
printf("Sum = %d\n", sum);
return 0;
}
10. Quick Reference
Operator Meaning Example Result
+ Add 5 + 3 8
- Subtract 5 - 3 2
* Multiply 5 * 3 15
/ Divide 9 / 2 4 (integer!)
% Remainder 9 % 2 1
++ Add 1 x++ x = x + 1
+= Add to self x += 5 x = x + 5

Keyword What it Does


return Sends a value back from a function
break Exits a loop or switch immediately
continue Skips current loop turn, goes to
next
void Function that returns nothing
int main() Every C program starts here

11. Exam Writing Checklist

[ ] Write #include <stdio.h> at the top


[ ] Write int main() { ... return 0; }
[ ] Add #include <math.h> when using pow() or sqrt()
[ ] Declare all variables before writing any code
[ ] Use & in scanf() for int and float — NOT for strings
[ ] Write break; after every case in switch
[ ] Put return inside every non-void function
[ ] End every statement with a semicolon ;
[ ] Use == to compare, not = (single = assigns a value)
[ ] Short on time? Write skeleton + comments — partial marks count!
Partial marks are real. A program with correct structure and clear logic earns more marks
than a blank page.

Good luck! You've got this.

You might also like