information about computer technology and about computer languages.

Tuesday, 19 January 2021

Basic Structure of C Program

 The format of writing C program is called its structure. The basic structure of C is very flexible. 

For Example:

#include<stdio.h>
int main ()
 {
      /* OUR FIRST SIMPLE PROGRAM */
       printf("HELLO WORLD");
       return 0; 
}

In the above example,
The first line is preprocessor which is used to include header files. The preprocessor directives start with hash symbol (#). 
The second line is main function where execution of C program starts.
The statement of program are written in curly braces, also known as delimiters. Every statement in C program is terminated with a semicolon (;).  

Parts of C program

  • Preprocessor directive
  • Header file
  • Main Function
  • Statements
  • Comment




Preprocessor Directives

Preprocessor directives is an instruction given to the compiler before execution of program. it must start with the hash symbol and used to include the header files. it is not terminated with semicolon. The define directives is used to define a constant. The include preprocessor directives is used to include header files in program.

#include<stdio.h>

In above statement include the the file stdio.h in source program before compiling it.

Header File

Header files are collection of standard library functions to perform different tasks. There are many header files each contains different types of predefined functions. .h is the extension of a header file. The name of header file is written in angle brackets < >. 
Syntax 
                    #include <header_file_name>
Example 
                   #include<stdio.h> 

Main Function

The main () function is the function where execution of C program starts. Each program must contain main function. The body is enclosed in braces {}. Main function starts with keyword. Keyword indicates the type of value that is returned by function.
Syntax 
                  
            int main()
{
body of main function

Statement

A statement is an instruction for the computer to perform a task. Computer perform these instructions one by one. Each statement is terminated with semicolon. 
For Example:
   Int main()
  {
    printf("HELLO WORD");
  }             


Comments

We can add comments in programs to describe what we are going to do. These comments are not executed by compiler. /*.......*/ comments written between these slashes. 


3 comments: