Structure of a 'C' Program

Structure of a  'C' Program  : Before moving to a programming part let's learn about structure of a 'C' program including what sections are necessary in a program or how to write C program. 

To write a C program we need a text editor. C program contains preprocessor instructions, comments, functions, variables, statements and expressions. For example consider simple C program to display message "Good Morning" to user.

#include<studio.h>
int main()
{
  //to print data
   printf("Good Morning");
   return 0;
}

Here first statement of a program is preprocessor instruction it is used to link our program with standard input output header file (stdio.h is a standard  input output header file). Second line defines main function from where execution of C program begins. Every C program must have main function.

Syntax of main() is :
int main([int argc, char * argv[]])
{
    ______
    ______
}

Note : [ ] is used to show optional part.

Function definition always enclosed between {}. Statement printf is used for printing data at standard output console, return statement is used to return a value to function. Every statement in C language ends with ;(semicolon).

Note: To run this program on windows operating system one has to make some changes as conio.h instead of stdio.h , void main instead of int main and getch(); instead of return 0.

 Program must be saved with an extension c for example demo.c will be name of file if demo contains C program. Before executing program it should be compiled in order to check for errors if any and convert it into target code. There are many compilers available for C language out of that cc ang gcc are two most widely used. To compile program command is cc prog_name.c or gcc prog_name.c. After compiling program successfully next step is to run  that program. To run a C program command used is ./a.out

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.