Install a C Compiler
To run C programs, you need a C compiler. Common choices are: - GCC (Linux, macOS via Homebrew/MacPorts, Windows via MinGW-w64 or WSL) - Clang (macOS via Xcode Command Line Tools, Linux packages) - MSVC (Windows, via Visual Studio or Build Tools) Quick checks after installation: - GCC: `gcc --version` - Clang: `clang --version` - MSVC: Open the "Developer Command Prompt for VS", then `cl`
Write Your First Program
Create a new file named `hello.c`. Copy the code into it. This simple program prints a message to the screen. The `\n` ensures the output ends with a newline.
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
Hello, World!
Compile the Program
Open your terminal (or command prompt) in the folder where `hello.c` is saved. Then run one of the following: GCC (Linux/macOS/MinGW-w64): - `gcc -std=c17 -Wall -Wextra -Wpedantic hello.c -o hello` Clang (Linux/macOS): - `clang -std=c17 -Wall -Wextra -Wpedantic hello.c -o hello` MSVC (Developer Command Prompt): - `cl /std:c17 /W4 hello.c` These commands produce an executable named `hello` (or `hello.exe` on Windows).
Run the Program
Once compiled, run the program: - On Linux/macOS: `./hello` - On Windows (PowerShell or CMD): `.\hello.exe`
Hello, World!
Troubleshooting Tips
- If `gcc` or `clang` isn't found, confirm installation and PATH settings. - On macOS, you may need: `xcode-select --install` for Command Line Tools. - On Windows with MSVC, always use the "Developer Command Prompt for VS" before running `cl`. - If your output appears without a newline, add `\n` in `printf`.