C Get Started
The first lesson showed a C program. This lesson turns that program into one a computer can run, in three steps: write, compile, run.
The program shape
The program prints one line and stops. Its shape is the same one the first lesson showed.
#include <stdio.h>
int main() {
printf("Ready to begin.\n");
return 0;
}Two statements in order
The two prints run in order. Step one comes first, Step two second.
#include <stdio.h>
int main() {
printf("Step one\n");
printf("Step two\n");
return 0;
}Example
#include <stdio.h>
int main() {
printf("Ready to compile.\n");
printf("Ready to run.\n");
return 0;
}Key points
- In this course, a C program lives in a file named main.c.
- Install a C compiler first. On Linux and Mac, the compiler is called gcc.
- Use gcc main.c -o main to compile the program.
- Run the built program with ./main.
- The -o main part names the program main.
- A compiler reports errors instead of a program when the code has a mistake.
- Running starts inside int main().
- A return 0; at the end tells the computer the program ended well.
Two lines print in order. Both lines appear, so the program is ready to compile and ready to run.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which command compiles the program in main.c?