Page Nav

HIDE

Grid

GRID_STYLE

Classic Header

{fbt_classic_header}

Latest:

latest

C program Basic structure Overview printing a message

Basic structure of a C program Documentation Link section Definition Global declaration main function  Subprogram section 01 Documentation ...

C program Basic structure Overview printing a message

Basic structure of a C program

  1. Documentation
  2. Link section
  3. Definition
  4. Global declaration
  5. main function 
  6. Subprogram section

01 Documentation


Documentation section presents the name of the program, author and more details. This is a set of comment lines.

02 Link section


Link section provides instruction to the compiler. It links functions from the system library.


03 Definition


In the definition section all the symbolic constants are defined 

04 Global declaration


Global declaration is the section where global variables are declared. Some variables are used in multiple functions. Those variables are called global variables.

05 main function 


Main function is one of the essential part in a C program. In every C program there is one main function. Main function contains two parts. Those are declaration part and executable part. These two parts appears between the opening and closing braces. Opening brace is used before the execution and the closing brace is used when the program execution completes. This is the logical end of a program.

main function ()

{
    Declaration
    Execution
}


All the statements in both of the parts ends with a semicolon sign (;).
In the execution there should be at least one statement.

06 Subprogram section


In the main function user defines all the functions and those functions are contained in the subprogram section.
Remember that, without main function all other functions may be absent if those are not required.

Function 1

Function 2
Function 3                  [ User defined functions ]
-------------
-------------
Function n


Printing a message in C program

Printing a line is simple. Suppose we want to print a line given below-

Online shopping is becoming famous day by day. This is easier and helps us a lot.
For this we have to write -

main()

{
/*Begin of printing*/

printf(" Online shopping is becoming famous day by day. This is easier and helps us a lot.");

/*End of printing*/
}

After running this program output will be-

Online shopping is becoming famous day by day. This is easier and helps us a lot.

Here in the main function we have written code. Every program must have one and only main 
function.If we use multiple then compiler will fail to understand the beginning of the program.
main function has no parameter.

We have to use semi colon after every statement in C. After printing the line we used semi colon.

 Opening brace " { " marks the beginning part of the main function and 
closing brace " } " marks the ending part of  the main function.

Online shopping is becoming famous day by day. 
This is easier and helps us a lot.

If we want to get output like this we need to use another printf function.

main()
{
/*Begin of printing*/


printf(" Online shopping is becoming famous day by day.");
printf("This is easier and helps us a lot.");


/*End of printing*/
}


But running this code you will see that both are in same line. For starting new line we have to use \n 
just before starting the new line or just before finishing first line..

main()
{
/*Begin of printing*/


printf(" Online shopping is becoming famous day by day.\n");
printf("This is easier and helps us a lot.");


/*End of printing*/
}


Then you will see the output like this-

Online shopping is becoming famous day by day. 
This is easier and helps us a lot.

Main function 


Main is an essential part of a C program. It has some specific supported forms-

  • main()
  • void main()
  • int main()
  • main(void)
  • void main (void)
  • int main (void)


Here the word void represents that, the function will not return any information to the operating system.

Defining

Defining is needed for pre-processing the compiler derivative and it is not a statement.

  • Not ends with semi colon
  • For easily distinguishing from lower case variable names symbolic constants are written in uppercase.
  • Before beginning the main function #define instructions are placed usually. Symbolic constants are not declared in the declaration section.
If we want to set a =105 before the main function we have to write,

#define a = 105


No comments