This tutorial has a detailed description of the C program structure. It's essential for you before proceeding to learn more advanced tutorials on C programming.



A C program involves the following sections:
  • Documentations (Documentation Section)
  • Preprocessor Statements (Link Section)
  • Global Declarations (Definition Section)
  • The main() function
    • Local Declarations
    • Program Statements and Expressions
  • User Defined Functions

Let's begin with a simple C program code.

Sample Code of C "Hello World" Program

Example:

/* Author: www.w3schools.in
Date: 2018-04-28
Description:
Writes the words "Hello, World!" on the screen */
  
#include<stdio.h>

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

Or in a different way

/* Author: www.w3schools.in
Date: 2013-11-15
Description:
Writes the words "Hello, World!" on the screen */

#include<stdio.h>

void main()
{
    printf("Hello, World!\n");
}

Program Output:

C program

The above example has been used to print Hello, World! Text on the screen.

Let's look into various parts of the above C program.

/* Comments */ Comments are a way of explaining what makes a program. The compiler ignores comments and is used by others to understand the code.

or

This is a comment block, which is ignored by the compiler. You can use comments anywhere in the program to add info about the program or code block, which will be helpful for developers to understand the existing code in the future easily.

Comments in C - Video Tutorial

Please watch this video tutorial to understand "C Comments" in more depth.

#include<stdio.h> stdio is standard for input/output, it allows us to use some functions that are included in a file called stdio.h.

or

This is a preprocessor command. That notifies the compiler to include the header file stdio.h in the program before compiling the source code.

int/void main() int/void is a return value, which will be explained in a while.
main() The main() is the main function where program execution begins. Every C program must contain only one main function.

or

This is the main function, which is the default entry point for every C program and the void in front of it indicates that it does not return a value.

Braces Two curly brackets "{...}" group all statements.

or

Curly braces show how much the main() function has its scope.

printf() It is a function in C, that prints text on the screen.

or

This is another predefined function of C that is used to display text string on the screen.

return 0 At the end of the main function returns value 0.

Basic Structure of the C Program

The example above illustrates how a simple C program looks and how the program segment works. A C program may contain one or more sections which are figured above.

The Documentation section usually contains a collection of comment lines giving the name of the program, the author's or programmer's name, and a few other details. The second part is the link section instructs the compiler to connect to the various functions from the system library. The Definition section describes all the symbolic constants. The global declaration section defines those variables used globally within the entire program and is used in more than one function. This section also declares all the user-defined functions. Then comes the main().

All C programs must have a main() which contains two parts:

  • Declaration part
  • Execution part

The declaration part is used to declare all variables used within the program. There needs to be at least one statement in the executable part, and these two parts are declared within the opening and closing curly braces of the main(). The program's execution begins at the opening brace '{' and ends with the closing brace '}'. Also, note that all the statements in these two parts need to be terminated using semicolons.

The sub-program section deals with all user-defined functions that are called from the main(). These user-defined functions are declared and usually defined after the main() function.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram