C Get Started
Before you can run C code, you need a way to write it and a compiler to turn it into a working program. This page walks you through the setup and your first successful run.
C is a compiled language, which means the code you write is not run directly. Instead, a program called a compiler translates your C file into an executable that the computer can run. The most common free compiler is GCC.
The Steps to Run C
- Write your code in a plain text file that ends with .c
- Compile the file with a C compiler such as GCC.
- Run the resulting program from your terminal.
- Read the output and fix any errors the compiler reports.
Writing the File
Create a file named hello.c and type the program below. This is the traditional first program that every C learner writes.
hello.c
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}Compiling and Running
Open a terminal in the folder where you saved the file and run these two commands. The first builds the program, and the second runs it.
Terminal commands
gcc hello.c -o hello
./helloIf everything worked, you will see Hello, World! printed in the terminal. Here is a slightly larger program you can try next; it prints two separate messages.
Two messages
#include <stdio.h>
int main() {
printf("C is compiled.");
printf(" And it is fast!");
return 0;
}Exercise: C Get Started
What must be installed before you can compile and run C code?