Exercise 0: Hello World
Programming Workshop in C/C++ (67315) -- C Exercise 0
Topics: Introduction to C, Compilation, main Function, Standard I/O.
1. Overview
This is the introductory exercise for the course. The goal is to verify that your development environment is set up correctly and that you can write, compile, and run a basic C program.
You will write a simple program that prints output to the console using printf.
2. Key Concepts
2.1 The main Function
Every C program begins execution from the main function. It must return an integer -- by convention, 0 (or EXIT_SUCCESS) indicates successful execution:
int main() {
// your code here
return 0;
}
2.2 Including Headers
To use standard I/O functions like printf, you must include the appropriate header:
#include <stdio.h>
2.3 Printing Output with printf
The printf function prints formatted output to the console. It uses format specifiers to control how values are displayed:
| Specifier | Type |
|---|---|
%d | int |
%ld | long |
%f | float / double |
%s | string (char*) |
%c | char |
Example:
printf("Hello, World!\n");
printf("%d\n", 42);
printf("%ld\n", (long) 1 / 10);
2.4 Compilation
Compile your program using gcc:
gcc -Wall -Wextra -Wvla -std=c99 hello_world.c -o hello_world
Flags explained:
-Wall-- enable most common warnings-Wextra-- enable additional warnings-Wvla-- warn about variable-length arrays-std=c99-- use the C99 standard
Then run the compiled program:
./hello_world
3. File Structure
hello_world.c
A single source file containing the main function:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
4. What to Pay Attention To
- Make sure to return
0frommain-- this signals successful execution to the operating system - Always include
<stdio.h>when usingprintf - Compile with all warning flags enabled -- fix any warnings before submitting
- The
\nat the end ofprintfoutput ensures a newline after your text
This exercise establishes the basic workflow you will use throughout the course: write code, compile with strict warning flags, and run. Get comfortable with this cycle early on.