Beginner · Getting started with C · topic 3 of 60 · 20 min
The structure of a C program
Explanation
Hello, world, line by line
#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 */| Part | Meaning |
|---|---|
| #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
/* 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
#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 */Several functions
#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;
}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
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).
#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;
}Practice (0/3 done)
Which line has the bug? 1: #include <stdio.h> 2: int main(void) { 3: printf("Hi\n") 4: return 0; 5: }
What does return 0; at the end of main mean?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Data types and their sizesGetting started with C · same module
- What C is and why it still mattersGetting started with C · same module
- Your first program: output, input and the Main methodC# and .NET · Getting started with C# and .NET
- Functions: declare, define, call and returnC++ Programming (modern C++ and DSA) · Decisions, loops and functions
- Functions, statements and expressionsRust Programming · Types, control flow and functions
- print() and commentsPython Programming · First steps in Python
Questions people ask about this
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.
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.
What does a function without -> return?Related
Answer: The unit type ()
Without a return type, the function returns ().
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
- 2 more topics in “Getting started with C” before its exam: Data types and their sizes, Input and output: printf and scanf.
- Module 1 exam: Getting started with C · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Operators and control flow. 60 topics left in the course before the final exam.