C/C++ Basics

Hello World and Program Structure

Both languages begin execution in main, but their standard libraries expose different output APIs.

What is Hello World and Program Structure?

Both languages begin execution in main, but their standard libraries expose different output APIs.

Compare the smallest complete C and C++ programs.

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

C
Run code →
main.c
#include <stdio.h>

int main(void) {
    printf("Hello from C!\n");
    return 0;
}
Expected output
Hello from C!
C++
Run code →
main.cpp
#include <iostream>

int main() {
    std::cout << "Hello from C++!\n";
    return 0;
}
Expected output
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

C exposes small procedural APIs and makes representation details explicit.

C++

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.