Structure of C Program with Examples

Structure of C Program with Examples

In this post, I am going to explain Basic Structure of C Program for learners.

Please remember below commands and syntax to write C program.

Commands Description
#include <stdio.h> This is a header file. that includes input and output related function
int main() This is the main function from where execution of any C program starts.
{ This indicates the beginning of the main function.
/*_some_comments_*/ whatever written inside the command “/*   */” section. won’t be considered for compilation and execution.
printf(“Hello World! “); printf command prints the output onto the screen.
getch(); This command will waits for any keyboard input from user.
return 0;
This command terminates C program (main function) and returns 0.
}
This indicates the end of the main function.

Hello World C Program:

This is a very simple and basic program in C programming language. This C program displays “Hello World!” in the output window. And, all syntax and commands in C programming are case sensitive. Also, each statement should be ended with semicolon (;) which is a statement terminator.

#include <stdio.h>
int main()
{
/* Our first simple C basic program */
printf("Hello World! ");
getch();
return 0;
}
OUTPUT:
Hello World!

How to Write a C Program Step by Step with Examples
Follow below steps to write any C program to get the output. This is common to all C program and there is no exception whether its a small C program or very big C program.

  1. Creation
  2. Compilation
  3. Execution or Run
  4. Get the Output

If you want to create, compile and execute C programs in your computer, you have to install C compiler in your computer. after that, you can start to execute your own C programs in your computer.

Basic Structure of C Program:
Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections/parts which are mentioned below.

  • Documentation section
  • Link Section
  • Definition Section
  • Global declaration section
  • Function prototype declaration section
  • Main function
  • User defined function definition section

EXAMPLE C PROGRAM TO COMPARE ALL THE SECTIONS:
You can compare all the sections of a C program with the below C program.

/*
Documentation section
C programming basics & structure of C programs
Author: http://jkmonline.com
Date : 13/08/2018
*/
#include <stdio.h> /* Link section */
int total = 0; /* Global declaration, definition section */
int multiply (int, int); /* Function declaration section */
int main () /* Main function */
{
printf ("This is a C basic program \n");
total =multiply (5, 2);
printf ("Multiplication of two numbers : %d \n", total);
return 0;
}

int multiply (int a, int b) /* User defined function */
{
return a * b; /* definition section */
}
OUTPUT:
This is a C basic program
Multiplication of two numbers : 10
Sections Description
Documentation section We can give comments about the program, creation or modified date, author name etc in this section. The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation.
Example : /* comment line1 comment line2 comment 3 */
Link Section Header files that are required to execute a C program are included in this section
Definition Section In this section, variables are defined and values are set to these variables.
Global declaration section Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section.
Function prototype declaration section Function prototype gives many information about a function like return type, parameter names used inside the function.
Main function Every C program is started from main function and this function contains two major sections called declaration section and executable section.
User defined function section User can define their own functions in this section which perform particular task as per the user requirement.

Related posts

Leave a Comment