C Program Structure (JNNC Technologies)

This lesson has detailed description about C program structure. It’s important for you before proceeding to learn more advanced lessons of C programming.
Basically 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 & 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: 2013-11-15
Description:
Writes the words "Hello, World!" on the screen */
  
#include<stdio.h>

int main()
{
    /* first C program */
    printf("Hello, World!\n");
    return 0;
}Run 
or in different way
/* Author: www.w3schools.in
Date: 2013-11-15
Description:
Writes the words "Hello, World!" on the screen */

#include<stdio.h>
#include<conio.h> 

void main()
{
    /* first C program */
    printf("Hello, World!\n");
    return;
}
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. Comments are ignored by the compiler and used by others to understand the code.
or
This is a comment block, which is ignored by the compiler. Comment can used anywhere in program to add info about program or code block, which will be helpful for developers to easily understand the existing code in the future.
#include<stdio.h>stdio is standard for input / output, this allows us to use some commands which includes a file called stdio.h.
or
This is a preprocessor command. that notify 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 a 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.
BracesTwo curly brackets “{…}” are used to group all statements together.
or
Curly braces which shows how much the main() function has its scope.
printf()It is a function in C, which prints text on the screen.
or
This is another pre-defined function of C which is used to be displayed text string in the screen.
return 0At the end of the main function returns value 0.

Basic Structure of C Program

The example discussed above, illustrate how a simple C program looks like and how the program segment works. A C program may contain one or more sections which is figured above.
The Documentation section usually contains the collection of comment lines giving the name of the program, author’s or programmer’s name and few other details. The second part is the link-section which gives instruction to the compiler to connect to the various functions from system library. The Definition section describes all the symbolic-constants. The global declaration section is used to define those variables that are 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 that will be 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 execution of program begins at the opening brace ‘{‘ and ends with the closing brace ‘}’. Also it has to be noted that all the statements of these two parts needs to be terminated with semi-colon.
The sub-program section deals with all user defined functions that are called from the main(). These user defined functions are declared and defined usually after the main() function.


0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();