Structure of a C program

Image result for Structure of a C program


Basically a C program involves the following sections:
  • Documentations
  • Preprocessor Statements
  • Global Declarations
  • The main() function
    • Local Declarations
    • Program Statements & Expressions
  • User Defined Functions

Sample code of C “Hello World” program:

Let’s begin with a simple C program code.
Example:
/*Writes the words "Hello World" on the screen */
  
#include<stdio.h>

int main()
{
    /* first C program */
    printf("Hello, World!\n");
    return 0;
}Run 
or in different way
/*Writes the words "Hello World" on the screen */

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

void main()
{
    /* first C program */
    printf("Hello World\n");
    return;
}
Program Output:
C program
The above example has been used to print “Hello World” on the screen.

Let’s look into various parts of the above C program.


/* Comments */Comments are a way of explaining what makes a program. Comments are ignored by the compiler and used by others to understand the code.
#include<stdio.h>stdio is standard for input / output, this allows us to use some commands which includes a file called stdio.h.
int/void main()int/void is a return value, which will be explained in a while.
main()The main() is the main function where program execution begins. Every C program must contain only one main function.
BracesTwo curly brackets “{…}” are used to group all statements together.
printf()It is a function in C, which prints text on the screen.
return 0At the end of the main function returns value 0.

No comments:

Post a Comment