K&R C Exercise 1-1a: The “hello, world” Program

Exercise 1-1. Run the “hello, world” program on your system. Experiment with leaving out parts of the program to see what error messages you get.

This is the very first program in The C Programming Language by Kernighan and Ritchie — and for many people, the first C program they ever type. The exercise has two jobs: get the program running, then deliberately break it to learn what the compiler tells you. That second part is the one most students skip, and it is the more valuable lesson.

What Every Line Does

Here is the program as it appears on page 6 of K&R:

#include <stdio.h>

main()
{
    printf("hello, world\n");
}

#include <stdio.h> — This line tells the preprocessor to paste in the contents of the standard I/O header file before compilation begins. That header contains the declaration of printf: it tells the compiler what arguments printf accepts and what it returns. Without it, the compiler has never heard of printf and will warn you — or, in strict ANSI mode, refuse to compile.

main() — Every C program starts here. main is the function the operating system calls when your program runs. The parentheses are the function’s parameter list (empty here). In modern ANSI C we write int main(void) to be explicit that it takes no arguments and returns an integer status code to the OS.

printf("hello, world\n"); — This calls the standard library function printf with one string argument. The \n at the end is an escape sequence for a newline character. It moves the terminal cursor to the start of the next line after printing. Without it, the program still works — but your shell prompt appears on the same line as the output, which looks wrong and can corrupt terminal state on some systems. The semicolon at the very end terminates the statement; in C, every statement ends with one.

The braces { } mark the beginning and end of main‘s body. Everything between them is the function’s code.

The ANSI C Version

/* K&R Exercise 1-1a: the hello, world program               */
/* Compile: gcc -ansi -Wall hello.c -o hello                 */

#include <stdio.h>

int main(void)
{
    printf("hello, world\n");
    return 0;
}

Two small changes from K&R’s original: int main(void) instead of bare main(), and an explicit return 0;. K&R wrote for C78; in ANSI C (C89/C90) and all later standards, main must return int. Returning 0 tells the shell the program succeeded.

Compile and Run

gcc -ansi -Wall hello.c -o hello
./hello

The flags: -ansi enforces ANSI C89 compliance; -Wall enables all common warnings. If your code compiles cleanly with both flags, it is solid.

Sample Output

hello, world

What Error Messages You Get — The Real Lesson

The exercise asks you to deliberately break the program. Here are four things to try, with the actual compiler output you will see.

1. Leave out #include <stdio.h>

/* hello.c — missing the #include line */

int main(void)
{
    printf("hello, world\n");
    return 0;
}
hello.c: In function 'main':
hello.c:4:5: warning: implicit declaration of function 'printf'
              [-Wimplicit-function-declaration]
    4 |     printf("hello, world\n");
      |     ^~~~~~
hello.c:4:5: note: include '<stdio.h>' or provide a declaration of 'printf'

The compiler has never seen a declaration for printf, so it guesses — it “implicitly declares” a function that returns int and accepts any arguments. For printf this happens to work on most systems because the actual function signature is close enough, but it is undefined behaviour in ANSI C. The compiler tells you exactly how to fix it in the note line.

2. Leave out the semicolon after printf

#include <stdio.h>

int main(void)
{
    printf("hello, world\n")   /* <-- no semicolon */
    return 0;
}
hello.c: In function 'main':
hello.c:6:5: error: expected ';' before 'return'
    6 |     return 0;
      |     ^~~~~~

The compiler reads printf(...) and expects a semicolon next. Instead it finds return, which makes no sense as a continuation of the expression, so it reports an error at the next line — not the line where the semicolon is missing. This off-by-one between the error location and the actual mistake is one of the most common sources of confusion for beginners.

3. Leave out the closing brace }

#include <stdio.h>

int main(void)
{
    printf("hello, world\n");
    return 0;
    /* closing brace missing */
hello.c: In function 'main':
hello.c:7:1: error: expected declaration or statement at end of input

The compiler reaches the end of the file while still inside main‘s body (waiting for the closing brace), so it reports an error at the end of input. The message sounds cryptic at first, but it almost always means a missing } somewhere above.

4. Leave out the \n (no error — but surprising output)

#include <stdio.h>

int main(void)
{
    printf("hello, world");   /* no \n */
    return 0;
}
hello, world$

No error, no warning. The program compiles and runs fine. But the output has no trailing newline, so your shell prompt ($ or %) appears on the same line immediately after “world”. This is not just cosmetic: POSIX defines a text file as a sequence of lines each ending with a newline, and some tools misbehave on output that lacks one. Always end your printf strings that produce a full line with \n.

What This Exercise Teaches

  • Header files declare, not define. #include <stdio.h> does not “load” printf — it gives the compiler the function’s signature so it can check your call. The actual printf code lives in the C standard library, linked automatically.
  • Statements end with semicolons. Braces ({ }) after function definitions and control structures do not take a semicolon; individual statements inside them do.
  • Error messages point near the mistake, not always at it. When you see “expected ‘;'” or “expected ‘}'”, look at the line above or the surrounding block, not just the line number the compiler gives.
  • A missing \n is a logic error, not a syntax error. The compiler cannot know whether you meant to print a newline. That is your job as the programmer.
  • main returns a status code. return 0 means success. The shell can check this with echo $? immediately after running a program.

Set Up Your C Environment

This is Exercise 1-1 — if you are setting up C for the first time, start here:


Chapter 1 Solutions  | 
Exercise 1-1b →

Book:

The C Programming Language, 2nd Ed — Kernighan & Ritchie

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>