Um momento
0x10Lesson 2 of 15

Your first program: Hello, world

Write, compile and run a complete C program, and understand every line of it.

10 min 6-question quiz 2 code exercises
By the end of this lesson you can
  • Explain each line of a minimal C program
  • Print text with printf, including newlines
  • Read a simple compiler error and fix it

Since K&R, the first program everyone writes prints a greeting. It is tiny, but every line of it matters. Press Run to compile and run it for real.

hello.c
1#include <stdio.h>
2
3int main(void) {
4    printf("Hello, world!\n");
5    return 0;
6}
Output
Hello, world!

Line by line

#include <stdio.h> tells the preprocessor to paste in the standard input/output header, which declares printf. Without it, the compiler does not know what printf is.

int main(void) starts the definition of a function named main. Every C program has exactly one main: it is where execution begins. int means main gives back (returns) a whole number, and void means it takes no inputs.

{ and } mark the start and end of main's body - the statements it runs, in order.

printf("Hello, world!\n"); calls the printf function to print text. The text in double quotes is a string literal. The semicolon ends the statement - almost every statement in C ends with one.

return 0; ends main and hands the number 0 back to the operating system. By convention, 0 means success; any other number signals an error.

Newlines and escape sequences

printf prints exactly what you give it - it does not add a line break at the end. The two characters \n inside a string are an escape sequence meaning "newline". Other useful ones are \t (tab), \\ (a backslash) and \" (a double quote inside a string).

newlines.c
1#include <stdio.h>
2
3int main(void) {
4    printf("one ");
5    printf("two\n");
6    printf("three\tfour\n");
7    printf("She said \"hi\"\n");
8    return 0;
9}
Output
one two
three	four
She said "hi"

Comments

Comments are notes for humans; the compiler ignores them. // comments run to the end of the line. /* ... */ comments can span several lines. Use them to explain why code does something, not to repeat what it obviously does.

1/* greet.c - prints a greeting.
2   Written for the NBK Tech World C track. */
3#include <stdio.h>
4
5int main(void) {
6    // Exit status 0 tells the shell everything went fine.
7    printf("Hi!\n");
8    return 0;
9}

Reading compiler errors

You will see a lot of compiler errors, and that is normal - even for experts. The trick is to read the first error carefully: it gives a file, line and column, then a message. Later errors are often just side effects of the first one. Try running this broken program:

broken.c
1#include <stdio.h>
2
3int main(void) {
4    printf("Hello, world!\n")
5    return 0;
6}

The compiler reports something like expected ';' before 'return'. It noticed the problem on the return line, but the actual mistake - a missing semicolon - is at the end of the line before it. Compilers often point just past the real mistake. Click Edit and experiment, add the semicolon, and run it again.

Key takeaways

  • Execution starts in main; returning 0 from it means success.

  • #include <stdio.h> makes printf available. printf does not add a newline - write \n yourself.

  • Statements end with ;. When you get a compiler error, fix the first one first, and check the line before it too.

Lesson quiz

6 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write real C

These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.

Exercise 1

Say hello

+25 XP

Write a program that prints exactly these two lines:

Hello, world!
I am learning C.
  • Prints both lines
main.c
Loading editor…

Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.

Exercise 2

Draw a box

+25 XP

Print this box made of characters, exactly as shown (5 characters wide, 3 lines tall):

+---+
|   |
+---+
  • Prints the box
main.c
Loading editor…

Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Sign in to ask questions, help other learners, and earn XP for taking part.

Loading posts…

Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: