C programming language - Basic C program structure

<<Previous

Next >>





Executing C Program >>        C Data Types >>       

C Functions >>        C 2D Arrays >>       



Let us learn C program structure in this tutorial.

C Program Structure in C Programming

Every C program contains a number of building blocks which include functions, blocks, variable declarations, including header files etc. Each function in C program can perform specific task. A program written in C will have the following structure:

    Include Header File Section

    Global Declaration Section

    /* Comments */

    main() /*Function name * /
    {
        /* comments */

        Local Declaration Part

        Executable Part
     }

    User-defined Function
     {
        Statements
     }
   

Let us discuss about the various part of C program

Step 1: Include Header File Section in C Programming

  • Most C program depend upon 1 or more header files. This header file contain function definition that is used in our program.
  • The header files have a default extension with ".h ".
  • The header file is included in the program by using " #include " directive.

For example:

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

Some of the header files are:

  • stdio.h - standard i/o functions
  • stdlib.h - defines library function
  • math.h - mathematical functions
  • string.h - string functions
  • time.h - time holding functions
  • conio.h - console in/out, that means, the ways of getting data to and from the console devices (keyboard & screen). This is required if our program has clrscr() and getch( )

Step 2: Global Declaration Section in C Programming

  • Here declare some variables which are used in more than one function. These variables are called global variables.
  • This must be declared outside of all the functions.

Step 3: main() function in C Programming

  • Every C program must contain main() function because this is an entry point of every program.
  • Program execution is always begin with main() function and then it starts from open brace { and ends with close brace } .
  • Within ‘{ } ’, we will write an entire program.

Step 3.1: Declaration Part in C Programming

  • Here declare the entire variables which are used in executable part.
  • Variable initialization is done in this section, that means, providing initial value to the variables.
  • These variables are called local variables.

Step 3.2: Executable Part in C Programming

This contains statement which may be single statement or set of statements.

Step 4: User-defined Function in C - Not compulsory but required in all large or complex programs

  • User or Programer can define their own function in C.
  • In general, this function can be defined after the main() function.
  • Sometimes, this function may be defined before the main().
  • It is not compulsory to write functions in order to execute. However, when the number of lines in main() becomes more, programmers move the logic into multiple meaningful functions

Step 5: Comments in C

Comments are used to understand the flow of the program.

  • while execution, the compiler does not execute the comments.
  • The programmers wish to include the comments anywhere in their programs just for better understanding of the program.

Thus, Comments serve two purposes in C programming:

  • It is primarily used to maintain an explanation of why a particular statement or block of statements is written.
  • It is also very useful for documentation of software or set of programs written in C.

There are 2 types of comments in C

C Single line comment //

// is used at the start of the line

Example: // This line is to the calculate the area of a square

C Multi line comment /* and */

Usually, the comments are placed between the delimiters /* and */


C Program Writing - Rules and Conventions

Rules in C programming

  • All keywords should be written in lower case letters.
  • C program is case sensitive. For example, variable name COUNT is different from the variable name count.
  • Opening and closing brace should be balanced.
  • Each statement should be terminated with semicolon. For example:
    d = a + b;
  • Keywords like for cannot be used for variable or function names

Conventions in C programming

  • Variable names should be written in lower case letters as a convention though not a rule
  • Upper case letters used for symbolic constants.
  • Extra Blank space may be inserted between the words which improve the readability of the statement.
  • It is not necessary to fix the position of the statements in the program.
  • More than one statements can be written in one line that are separated by using semicolon. For example:
    printf(" the main program ");   d = a + b;

<< Previous

Next >>




C Input Output functions >>        C Variables >>       

C Operators >>        C If Statement >>

strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length




C programming language - Basic C program structure

<<Previous

Next >>





Executing C Program >>        C Data Types >>       

C Functions >>        C 2D Arrays >>       



Let us learn C program structure in this tutorial.

C Program Structure in C Programming

Every C program contains a number of building blocks which include functions, blocks, variable declarations, including header files etc. Each function in C program can perform specific task. A program written in C will have the following structure:

    Include Header File Section

    Global Declaration Section

    /* Comments */

    main() /*Function name * /
    {
        /* comments */

        Local Declaration Part

        Executable Part
     }

    User-defined Function
     {
        Statements
     }
   

Let us discuss about the various part of C program

Step 1: Include Header File Section in C Programming

  • Most C program depend upon 1 or more header files. This header file contain function definition that is used in our program.
  • The header files have a default extension with ".h ".
  • The header file is included in the program by using " #include " directive.

For example:

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

Some of the header files are:

  • stdio.h - standard i/o functions
  • stdlib.h - defines library function
  • math.h - mathematical functions
  • string.h - string functions
  • time.h - time holding functions
  • conio.h - console in/out, that means, the ways of getting data to and from the console devices (keyboard & screen). This is required if our program has clrscr() and getch( )

Step 2: Global Declaration Section in C Programming

  • Here declare some variables which are used in more than one function. These variables are called global variables.
  • This must be declared outside of all the functions.

Step 3: main() function in C Programming

  • Every C program must contain main() function because this is an entry point of every program.
  • Program execution is always begin with main() function and then it starts from open brace { and ends with close brace } .
  • Within ‘{ } ’, we will write an entire program.

Step 3.1: Declaration Part in C Programming

  • Here declare the entire variables which are used in executable part.
  • Variable initialization is done in this section, that means, providing initial value to the variables.
  • These variables are called local variables.

Step 3.2: Executable Part in C Programming

This contains statement which may be single statement or set of statements.

Step 4: User-defined Function in C - Not compulsory but required in all large or complex programs

  • User or Programer can define their own function in C.
  • In general, this function can be defined after the main() function.
  • Sometimes, this function may be defined before the main().
  • It is not compulsory to write functions in order to execute. However, when the number of lines in main() becomes more, programmers move the logic into multiple meaningful functions

Step 5: Comments in C

Comments are used to understand the flow of the program.

  • while execution, the compiler does not execute the comments.
  • The programmers wish to include the comments anywhere in their programs just for better understanding of the program.

Thus, Comments serve two purposes in C programming:

  • It is primarily used to maintain an explanation of why a particular statement or block of statements is written.
  • It is also very useful for documentation of software or set of programs written in C.

There are 2 types of comments in C

C Single line comment //

// is used at the start of the line

Example: // This line is to the calculate the area of a square

C Multi line comment /* and */

Usually, the comments are placed between the delimiters /* and */


C Program Writing - Rules and Conventions

Rules in C programming

  • All keywords should be written in lower case letters.
  • C program is case sensitive. For example, variable name COUNT is different from the variable name count.
  • Opening and closing brace should be balanced.
  • Each statement should be terminated with semicolon. For example:
    d = a + b;
  • Keywords like for cannot be used for variable or function names

Conventions in C programming

  • Variable names should be written in lower case letters as a convention though not a rule
  • Upper case letters used for symbolic constants.
  • Extra Blank space may be inserted between the words which improve the readability of the statement.
  • It is not necessary to fix the position of the statements in the program.
  • More than one statements can be written in one line that are separated by using semicolon. For example:
    printf(" the main program ");   d = a + b;

<< Previous

Next >>




C Input Output functions >>        C Variables >>       

C Operators >>        C If Statement >>



strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy