5 min read
Your First C Program
Prerequisites: Installing a C compiler
Create a file named hello.c:
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}
Compile and run it from the terminal:
gcc hello.c -o hello
./hello
Common mistakes
- Forgetting
return 0;— most compilers allow this and assume0(success) automatically inmain, but writing it explicitly makes the program's intent clear. - Missing the
#include <stdio.h>line —printfis declared in that header; without it, the compiler doesn't know whatprintfis.
Practice
Change the program to print your own name instead of "Hello, world!".
Show solution
#include <stdio.h>
int main(void) {
printf("Ada\n");
return 0;
}