What you will learn
By the end of this lesson you should be able to explain what a C program is, describe how source code becomes an executable, and compile a small program. No previous programming experience is required.
Where C fits
C is a general-purpose programming language developed by Dennis Ritchie at Bell Labs in the early 1970s. It is closely associated with the development of Unix. Operating systems, embedded controllers and performance-sensitive libraries still use C because it offers direct control over data representation and memory.
C is not the same language as C++. The two share some syntax, but C does not provide C++ classes, references or standard-library containers. In this course, write and compile examples as C unless a lesson explicitly says otherwise.
From text to a running program
- Edit: save the source in a file such as
hello.c. This is readable text, not an executable. - Preprocess: process directives such as
#includeand macros. - Compile: translate the program and diagnose syntax or type problems.
- Assemble and link: create machine code and connect it with required library routines.
- Run: the operating system starts the resulting executable. A successful compilation does not prove that the program's logic is correct.
First example
#include
int main(void) {
printf("Hello, OnlineCBT!\n");
return 0;
} stdio.h declares the standard input/output functions used here. Execution enters main. The statement calling printf displays a message; \n ends the line. Returning zero from main reports successful termination.
Build and check
If GCC is already installed, open a terminal in the source file's directory and run:
gcc -std=c17 -Wall -Wextra -pedantic hello.c -o hello
./helloThe second command is for Unix-like shells. In Windows PowerShell, an executable built as hello.exe is normally started with .\hello.exe. An online compiler may provide a Run button instead. Expected output:
Hello, OnlineCBT!If the terminal says the compiler command was not found, that is an installation or PATH problem. If GCC reports a filename or line number, read the first diagnostic and check the source. Do not solve an error by removing unrelated lines.
Try it yourself
- Change the message to your first name and run the program again.
- Print two messages using two calls to
printf. - Temporarily remove the semicolon after the function call. Compile, read the diagnostic, then restore it.
Check your understanding
Is a .c file executable? No. It must normally be translated and linked before it can run. Why enable warnings? They point out suspicious code that may compile but behave incorrectly. Does every computer use identical type sizes? No. Later lessons use sizeof and standard limits rather than assuming a particular machine.
Next: inspect the structure of a complete program and practise writing your own Hello World from a blank file.