BETA Try the new lesson view
Code

Your first program

In the repository create the folder practice/01 (right-click practice → New → Directory) and two files in it.

practice/01/CMakeLists.txt is one line: build a program called p01_hello from main.c.

add_executable(p01_hello main.c)

practice/01/main.c is the program itself. Line by line:

  • #include <stdio.h> brings in the standard input/output library. That is where printf is declared.
  • int main(void) is the function every C program starts from. void means it takes nothing, int that it returns a whole number.
  • { and } wrap the function body: the statements that run one after another.
  • printf("Hello, world!\n"); prints text. \n means a new line. Every statement ends with a semicolon ;.
  • return 0; says the program finished successfully: 0 means all good, anything else means an error.

Run it

  1. File → Reload CMake Project: CLion finds the new folder.
  2. At the top, next to ▶, pick p01_hello and click ▶ (Shift+F10).

The Run window opens at the bottom:

C:\...\build\debug\practice\01\p01_hello.exe
Hello, world!

Process finished with exit code 0

exit code 0 is your return 0;.

Works? Make a commit: Add first program.