C Basic Program Structure

Every C program follows a simple structure. Let us understand it with one small program.

#include <stdio.h>      // header file

int main(void) {        // main function
    int age = 20;       // variable
    printf("Age: %d\n", age);   // statement
    return 0;           // end of program
}

Output:

Output of the above program is

Age: 20

Parts of the program

PartMeaning
#include <stdio.h>Adds the standard input output header file, so we can use printf() and scanf().
int main(void)The main function. Every C program starts running from here.
{ }Curly braces start and end a block of code.
;Semicolon ends a statement.
return 0;Sends 0 to the system. Zero means the program ended without error.

Rules to remember

  • C is case sensitive. Main and main are not the same.
  • Every statement must end with a semicolon.
  • A program can have only one main() function.
  • Write the code in small readable lines and use indentation.
Note: Some old books write void main(). The correct and standard form is int main(void) or int main() with return 0;.

Next: C Comments.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is C language?
What are the main features of C language?
What is the basic structure of a C program?
What is the difference between a compiler and an interpreter?
What is the difference between int main() and int main(void) in C?