Skip to content

Beginner · Getting started with C · topic 3 of 60 · 20 min

The structure of a C program

Explanation

A C program is a set of functions, and execution starts at main. #include lines bring in declarations from header files such as stdio.h. Statements end with semicolons, braces group them into blocks, and comments are written with /* … */ or //. main returns an int: 0 for success.

Hello, world, line by line

c
#include <stdio.h>          /* 1: bring in declarations for printf, scanf … */

int main(void)              /* 2: the program starts here; it returns an int */
{                           /* 3: start of main's body (a block) */
    printf("Namaste!\n");   /* 4: call printf; \n is a new line; ; ends the statement */
    return 0;               /* 5: exit status 0 = success */
}                           /* 6: end of main */
PartMeaning
#include <stdio.h>A preprocessor directive: paste in the standard input/output header so the compiler knows printf exists.
int main(void)The function where every C program starts. int: it returns a whole number. void: it takes no arguments.
{ }A block: a group of statements treated as one unit.
printf(...);A function call statement. Every statement ends with a semicolon.
return 0;Ends main and reports success to the operating system.

Comments and layout

c
/* A block comment
   can span several lines. */
// A line comment (C99 and later)
int total = 0;   // explain WHY, not what: "start at 0 because the cart may be empty"

C ignores spaces, tabs and new lines between tokens, so you could write a program on one line. Do not: indent the inside of every block by 4 spaces (or a consistent style) so the structure is visible. Many teams use clang-format to format automatically.

main with arguments

c
#include <stdio.h>

int main(int argc, char *argv[])   /* argc: count of arguments; argv: the arguments as strings */
{
    printf("Program: %s, arguments: %d\n", argv[0], argc - 1);
    if (argc > 1) printf("First argument: %s\n", argv[1]);
    return 0;
}
/* ./greet Ravi  →  Program: ./greet, arguments: 1  /  First argument: Ravi */
You will understand char *argv[] fully after pointers (module 5).

Several functions

c
#include <stdio.h>

int square(int n);              /* a declaration (prototype): tells the compiler it exists */

int main(void) {
    printf("%d\n", square(12)); /* 144 */
    return 0;
}

int square(int n) {             /* the definition: the actual code */
    return n * n;
}
Careful: Common first mistakes: a missing semicolon (the error is often reported on the NEXT line), a missing closing brace (errors pile up at the end of the file), writing Main or main() with a capital or wrong spelling, and forgetting #include <stdio.h> (modern compilers then refuse to compile the call to printf: "implicit declaration of function").
Think of it like this: A C program is like a recipe book: #include lines are references to standard techniques described in other books, each function is one recipe, and main is the recipe you always start cooking from. The other recipes are used only when main calls them.
Tip: If main reaches its closing brace without return, C99 and later behave as if it returned 0. Writing return 0; explicitly is still clearer for beginners.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

The structure of a C program · 1/6

Program structure

In this lesson you will learn the parts of every C program.

Prefer a video by a teacher? Find free videos on “structure of c program main function include stdio.h explained” on YouTube ↗

In real life: A command-line GST calculator

A small accounting office uses a tiny C tool: ./gst 2500 prints the tax and total. It has a helper function and reads its argument from argv, converting the text to a number with atof (module 4 shows the safer strtod).

c
#include <stdio.h>
#include <stdlib.h>

double with_gst(double amount) { return amount * 1.18; }

int main(int argc, char *argv[]) {
    if (argc < 2) { printf("Usage: %s <amount>\n", argv[0]); return 1; }
    double amount = atof(argv[1]);
    printf("Total with GST: %.2f\n", with_gst(amount));
    return 0;
}
./gst 2500 prints Total with GST: 2950.00

Practice (0/3 done)

Task 1 · Question

Which line has the bug? 1: #include <stdio.h> 2: int main(void) { 3: printf("Hi\n") 4: return 0; 5: }

Task 2 · Question

What does return 0; at the end of main mean?

Task 3 · Practice

Write greet.c whose main takes argc and argv. If a name is given (./greet Asha) print "Namaste, Asha!", otherwise print "Usage: ./greet <name>" and return 1. Add a function void line(void) that prints a row of 20 dashes, declared with a prototype above main.

Quick quiz

Question 1

1.Where does every C program start executing?

Question 2

2.What is #include <stdio.h>?

Question 3

3.In int main(int argc, char *argv[]), what is argv[0]?

Question 4

4.What is a function prototype?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Where does every C program start executing?

Answer: The main function

main is the entry point.

What is #include <stdio.h>?

Answer: A preprocessor directive that inserts a header file

It brings in declarations like printf.

In int main(int argc, char *argv[]), what is argv[0]?

Answer: The program name

User arguments start at argv[1].

What is a function prototype?

Answer: A declaration that tells the compiler a function's name, return type and parameters

It allows calls before the definition.

What ends a statement in C#?Related

Answer: A semicolon ;

Every C# statement ends with a semicolon.

From Your first program: output, input and the Main method

Which include style is used for your own header file?Related

Answer: #include "myfile.h"

Quotes search your project folder first; angle brackets are for standard and system headers.

From Functions: declare, define, call and return

What does a function without -> return?Related

Answer: The unit type ()

Without a return type, the function returns ().

From Functions, statements and expressions

Which symbol starts a comment in Python?Related

Answer: #

In Python a comment starts with #. The others are used in other languages.

From print() and comments

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Getting started with C” before its exam: Data types and their sizes, Input and output: printf and scanf.
  2. Module 1 exam: Getting started with C · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Operators and control flow. 60 topics left in the course before the final exam.

← Previous: Compiling with gcc