C Programming
Unit 1: Problem Solving with Computer
Problem analysis, algorithms, flowcharts, history of C, the program development cycle, and how to install GCC on Windows and Mac.
Problem solving with computer
A computer is an electronic device that solves problems by executing a set of instructions called a program. Computers do not understand problems on their own — a programmer must analyse the problem, design a solution, write a program in a programming language, and then run it to obtain the result.
The process of solving a problem using a computer follows a set of well-defined steps:
- Problem analysis — understand the problem clearly.
- Design an algorithm — outline a step-by-step solution.
- Draw a flowchart / write pseudocode — represent the algorithm visually or in plain English.
- Write the program — translate the algorithm into a programming language (such as C).
- Compile and execute — convert the program into machine code and run it.
- Test and debug — check the program for errors and verify correctness.
- Document and maintain — record what the program does and update it as needs change.
Problem analysis
Problem analysis is the first and most important phase of program development. It involves studying the problem carefully to fully understand what needs to be done before writing any code.
During problem analysis, the programmer identifies:
- Inputs — the data required by the program.
- Outputs — the results expected from the program.
- Processing — the operations needed to transform the inputs into the outputs.
- Constraints — any limits or special conditions (range of values, precision, etc.).
Example — finding the area of a circle:
- Input: radius
r - Process:
area = π * r * r - Output: value of
area
A clear understanding of the problem must be obtained before any code is written. Skipping this stage usually leads to bugs and re-work later on.
Algorithms
An algorithm is a finite, step-by-step procedure for solving a given problem. It is the core of any program — once the algorithm is correct, translating it into a programming language becomes straightforward.
Properties of a good algorithm
- Finiteness — it must terminate after a finite number of steps.
- Definiteness — each step must be clear and unambiguous.
- Input — it should accept zero or more inputs.
- Output — it must produce at least one output.
- Effectiveness — each step must be simple enough to be carried out.
Example — sum of two numbers
Step 1: Start
Step 2: Read two numbers A and B
Step 3: Compute SUM = A + B
Step 4: Display SUM
Step 5: Stop
Example — largest of two numbers
Step 1: Start
Step 2: Read two numbers A and B
Step 3: If A > B, then
Display A is largest
Else
Display B is largest
Step 4: Stop
Flowchart
A flowchart is a graphical representation of an algorithm. It uses standard symbols connected by arrows to show the flow of control from one step to the next.
Common flowchart symbols
| Symbol | Meaning |
|---|---|
| Oval | Start / Stop (terminator) |
| Parallelogram | Input / Output |
| Rectangle | Process / Computation |
| Diamond | Decision (yes / no) |
| Arrow | Flow of control |
| Circle | Connector |
Example — flowchart for the sum of two numbers
( Start )
|
v
[ Read A, B ]
|
v
[ SUM = A + B ]
|
v
[ Print SUM ]
|
v
( Stop )
Advantages of flowcharts
- Provide a clear visual picture of the logic.
- Help in detecting errors early.
- Serve as good documentation.
- Make program logic easy to communicate to others.
Pseudocode
Pseudocode is an informal way of describing an algorithm using plain English mixed with programming-like structures. It is easier to write than a flowchart and easier to read than actual code.
Example — check whether a number is even or odd:
BEGIN
READ n
IF n MOD 2 = 0 THEN
PRINT "Even"
ELSE
PRINT "Odd"
ENDIF
END
History of C
The C programming language was developed by Dennis Ritchie at Bell Laboratories in 1972. It evolved from two earlier languages — BCPL (Basic Combined Programming Language, by Martin Richards) and B (by Ken Thompson). C was originally designed to write the UNIX operating system, which is one of the reasons it became so widely used.
A short timeline:
- 1960 — ALGOL appears (parent of structured languages).
- 1967 — BCPL by Martin Richards.
- 1970 — B language by Ken Thompson.
- 1972 — C language by Dennis Ritchie.
- 1989 — ANSI C (C89) standard.
- 1999, 2011, 2018 — Updated C standards (C99, C11, C17).
Importance of C
C is one of the most influential programming languages ever created. Its key strengths are:
- It is a general-purpose, structured programming language.
- It supports both low-level (system) and high-level programming.
- It is portable — the same program can run on many different machines.
- It produces efficient and fast programs.
- It has a rich set of operators and built-in functions.
- It is the foundation of many modern languages such as C++, Java, C#, and Objective-C.
Applications of C
- Operating systems (UNIX, Linux, parts of Windows).
- Compilers and interpreters.
- Embedded systems and device drivers.
- Database systems (MySQL, SQLite).
- Graphics and game development.
Structure of a C program
A typical C program is organised into the following sections:
- Documentation section — comments describing the program.
- Preprocessor / link section —
#includedirectives. - Definition section —
#defineconstants and macros. - Global declaration section — variables and functions visible to the whole file.
main()function — execution always starts here.- User-defined functions — additional functions written by the programmer.
Example:
/* Documentation: simple C program */
#include <stdio.h> /* Link section */
#define PI 3.14 /* Definition */
int radius = 5; /* Global section */
int main(void) /* main function */
{
float area;
area = PI * radius * radius;
printf("Area = %.2f\n", area);
return 0;
}
Installing a C compiler (GCC)
To write and run C programs you need a C compiler. The most popular free compiler is GCC (GNU Compiler Collection).
Install GCC on Windows
On Windows, GCC is most commonly installed through MinGW-w64 or MSYS2. Two reliable options:
-
MSYS2 (recommended) — provides an easy way to install and update GCC. Download from https://www.msys2.org/. After installation, open the MSYS2 terminal and run:
pacman -S mingw-w64-ucrt-x86_64-gcc -
MinGW-w64 — the official GCC port for Windows. Project page: https://www.mingw-w64.org/
After installing, add the compiler’s bin folder to your PATH so that gcc is
recognised from any command prompt. To verify, open Command Prompt and run:
gcc --version
Install GCC on macOS
On macOS, the easiest way to get a working C compiler is to install Xcode Command Line
Tools, which include clang (a drop-in replacement for gcc).
Open the Terminal and run:
xcode-select --install
Alternatively, install the real GCC through Homebrew:
-
Install Homebrew first: https://brew.sh/
-
Then install GCC:
brew install gcc
Verify the installation:
gcc --version
Tip: Most online tutorials and textbooks assume
gccis available from the command line. Oncegcc --versionprints a version number, you are ready to compile C programs.
Coding, compilation and execution
The process of developing and running a C program involves the following stages:
- Coding — writing the source code in a text editor and saving it with the
.cextension (e.g.hello.c). - Preprocessing — the preprocessor handles directives such as
#includeand#define, producing an expanded source file. - Compilation — the compiler translates the preprocessed code into object code
(
.o/.obj). - Linking — the linker combines the object code with libraries to produce an
executable file (
.exeon Windows, no extension on Linux/Mac). - Execution — the operating system loads and runs the executable.
Example — compile and run with GCC:
gcc hello.c -o hello # compile and link
./hello # execute (Linux / Mac)
hello.exe # execute (Windows)
A minimal C program:
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
Debugging, testing and documentation
After writing a program, three more steps make it reliable and maintainable.
Debugging
Debugging is the process of finding and fixing errors in a program. C programs typically contain three kinds of errors:
- Syntax errors — code that violates language rules (e.g. missing semicolon). Caught by the compiler.
- Logical errors — code compiles and runs but produces incorrect results.
- Runtime errors — errors that occur while the program is running (e.g. division by zero, accessing an invalid memory address).
Testing
Testing is the process of running a program with various inputs to verify that it produces the expected output. Programmers usually test with:
- Normal cases — typical valid inputs.
- Boundary cases — smallest and largest valid values.
- Invalid cases — wrong or unexpected inputs.
Documentation
Documentation describes the program so that users and other programmers can understand it. It includes:
- Internal documentation — comments inside the source code.
- External documentation — manuals, user guides, and design documents.
Comment syntax in C:
/* This is a multi-line comment
used to document the program. */
// This is a single-line comment.