Hello World and Program Structure
Compare the smallest complete C and C++ programs.
What is Hello World and Program Structure?
Both languages begin execution in main, but their standard libraries expose different output APIs.
Important points
- Compile with warnings enabled and fix every warning.
- Know the lifetime and type of every value.
- Validate input and keep array bounds explicit.
C and C++ code examples
#include <stdio.h>
int main(void) {
printf("Hello from C!\n");
return 0;
}
Hello from C!
#include <iostream>
int main() {
std::cout << "Hello from C++!\n";
return 0;
}
Hello from C++!
C and C++ comparison
C uses the stdio function printf, while C++ uses the type-safe iostream object std::cout. Returning zero reports successful execution to the operating system.
C exposes small procedural APIs and makes representation details explicit.
C++ retains the low-level model while adding safer library types and abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add invalid and boundary-value inputs.
- Compile with -Wall -Wextra -Wpedantic.
- Move reusable declarations into a header and implementation file.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges