Structure & Samples of C-Program

Structure & Samples of C-Program

4 | Structure & Samples of C-Program


4.0 | Contents :

     1.   General Structure of C Program
     2.   Samples of C Programs


4.1 | General Structure of C Program :

     include section
     main function
       {
          variable declaration;
          clrscr(;
          statements/expressions;
          getch(;
          return 0;
       }
     
  • Include Section : In this section, header files must be included. Which type of header files are included, this depends upon which type of funtions are used in the C program.
  • Main Function : This function is always present in all the C programs. You can call it like this is the starter of every C program.
  • Variable Declaration : Here, user declares the variables and these variables are only usable in this function i.e. variables declared are local variables.
  • clrscr () : This function will clear the output screen before the execution of the program.
  • Statements / Expressions : In this section, user can write valid C statements OR valid C expressions.
  • getch() : This function is used to hold the screen until C program is not terminated.
  • return 0 : This will terminate the execution of the program.


4.2 | Samples of C Programs :


Example 1 :

Input :

#include<stdio.h>

void main()
  {
     clrscr();
     printf("Hello World !");
     getch();
  }
  Output :

  Hello World !
Input :

#include<stdio.h>

int main()
  {
     clrscr();
     printf("Hello World !");
     getch();
     return 0;
  }



Example 2.1 :

Input :

#include<stdio.h>

int main()
  {
     char a='A';
     int b=34;
     float c=34.645;

     clrscr();

     printf("Character : %c\n",a);
     printf("Integer : %d\n",b);
     printf("Floating Point Number : %f",c);

     getch();
     return 0;
  }
Output :

     Character : A
     Integer : 34
     Floating Point Number : 34.645000


     If you can't use \n, then the output is come in the same line i.e. there is no line-break like "Character : A" and "Integer : 34" in above program's output. (See example given below)




Example 2.2 :

Input :

#include<stdio.h>

int main()
{
     char a='A';
     int b=34;
     float c=34.645;

     clrscr();

     printf("Character : %c",a);
     printf("Integer : %d\n",b);
     printf("Floating Point Number : %f",c);

     getch();
     return 0;
}
Output :

     Character : AInteger : 34
     Floating Point Number : 34.645000


     As you can see here, \n is used after %d to break the line between "Integer : 34" and "Floating Point Number : 34.645" ; but it can't use after %c in above program and hence there is no line-break between "Character : A" and "Integer : 34" in output of the above program.




Note :
          To know how to use printf() and scanf() funtions, click here.

          Many other examples/samples of C programs will come with there respective topics.

          There is no need to use clrscr(); and getch(); in other compilers (such as gcc) except TurboC/TurboC++.




Comments