Your first C program

Introduction

C is the language most CSE departments teach first, because it shows how a computer really works: every variable is a piece of memory, and you control all of it.

Most operating systems, and the engines of many other languages, are written in C. Learning it means learning what the machine is really doing, and that knowledge makes every later language easier.

How it works

The smallest useful program

#include <stdio.h>

int main(void) {
    printf("Hello, StepVioz!\n");
    printf("I am learning C.\n");
    return 0;
}
Open in compiler

Press Visualize and step through with the → key.

  • #include <stdio.h> brings in the standard input/output library, where printf lives. Without it, C does not know what printf is.
  • int main(void) is where every C program starts. The computer runs the lines inside its braces, top to bottom.
  • printf("...") prints text. \n inside the quotes means "new line".
  • return 0; ends main and tells the operating system the program finished without problems.

Every statement ends with a semicolon ;. Forgetting one is the most common first error, and StepVioz points at the line where it is missing.

Syntax

#include <stdio.h>

int main(void) {
    printf("Hello\n");        // \n starts a new line
    printf("%d\n", 2 + 3);    // %d is replaced by a whole number
    printf("100%%\n");        // %% prints one percent sign
    printf("\"quoted\"\n");   // \" prints a double quote
    printf("C:\\temp\n");     // \\ prints one backslash
    return 0;
}

Example

printf can fill numbers into the text. Each %d is replaced by the next value after the quotes, in order:

#include <stdio.h>

int main(void) {
    printf("%d + %d = %d\n", 2, 3, 2 + 3);
    printf("7 times 6 is %d\n", 7 * 6);
    return 0;
}
Open in compiler

The calculation 7 * 6 happens first, then its result is printed. Watch the output panel: each line appears at exactly the step that printed it.

Line by line

  1. #include <stdio.h> makes printf available.
  2. int main(void) is where the program begins.
  3. The first printf has three %d markers and three values. 2 + 3 is worked out before printing.
  4. The second printf fills in 7 * 6.
  5. return 0; tells the operating system that everything went well.

Expected result

2 + 3 = 5
7 times 6 is 42

In the real world

C runs inside phones, cars, routers and washing machines, because it can work very close to the hardware. Tools such as Python are themselves written largely in C.

Common mistakes

  • Forgetting #include <stdio.h>: C says printf is not declared.
  • Forgetting the ; at the end of a line.
  • Writing Printf or print: C is case-sensitive, and the name is printf.
  • Forgetting \n: the next output then continues on the same line.

Best practices

  • Compile and run after every small change.
  • Fix the first error the compiler reports, then compile again.
  • Indent the code inside braces consistently.

Summary

A C program includes the headers it needs, starts in main, runs its statements in order and returns 0. printf prints text, and format codes such as %d place values inside that text.

Practice

Change the first example to print your name and your university on two separate lines.

Quick check

1. Where does every C program start running?
2. What does #include <stdio.h> give your program?
3. What does printf("%d + %d = %d", 2, 3, 2 + 3) print?

Key takeaways

  • Every C program starts in main.
  • Each statement ends with a semicolon.
  • printf replaces each %d with the next value.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.