Lesson 1 of 5

Your First C Program and the Compiler

Why Learn C?

C is one of the most influential programming languages ever created. Operating systems, databases, and even other programming languages are written in C. Learning it teaches you how memory and the machine actually behave.

The Classic First Program

Every C program begins with a main function — the place where execution starts.

#include <stdio.h>

int main(void) {
    printf("Hello, DCCPS!\n");
    return 0;
}

Reading the Code Line by Line

  • #include <stdio.h> brings in the standard input/output library so we can use printf.
  • int main(void) defines the main function; int means it returns an integer.
  • printf prints text. The \n is a newline character that moves to the next line.
  • return 0; reports success to the operating system.

Compilation: From Source to Program

Unlike Python, C is a compiled language. You cannot run the source code directly. A program called a compiler translates your C file into machine code first.

  1. Write your code in a file ending in .c, for example hello.c.
  2. Compile it: gcc hello.c -o hello
  3. Run the resulting program: ./hello

The -o hello option names the output program hello. If you skip it, the compiler creates a file called a.out by default.

Statements and Semicolons

In C, almost every statement ends with a semicolon. Forgetting it is the most common beginner error and produces a compile-time message.

A compile-time error stops your program from being built at all. This is actually helpful — the compiler catches many mistakes before the program ever runs.

C is case sensitive. main and Main are different names, and int must be written in lower case.

Next
How did you find this lesson?

Discussion

0
To join the discussion, comment and react, log in or create an account.

No comments yet — be the first to leave one!