A ‘C’ program is divided into six sections: Documentation, Link, Definition, Global Declaration, Main() Function, Subprograms. While the main section is compulsory, the rest are optional in the structure of the C program.
Three Major things which Should Consider
- Firstly, think about what should be the output?
- Then, think about what should be the inputs that will be required to produce this output.
- And Last, think about the steps of instructions which use these inputs to produce required output.
Structure of C program
The structural parts of C program
- Documentation section
- Preprocessor section
- Definition section
- Global declaration
- Main function
- User defined functions
Explanation of Structure of C Programming by an Example.
A simple C Program
Explanation of Above C Program
#include<stdio.h>
- This is a reference to a special file which is also known as the header file called stdio.h.
- stdio.h contains information that must be included in the program when it is compiled.
- The #include statement tells the compiler to include the input/output library or Header file (stdio.h) in the program.
- It is present at the beginning of almost every C program.
Basically, all the statements starting with # in a C program are called preprocessor directives. This statement allows you to use some predefined functions such as, printf (), in this case.
int main()
- Every C program contains a function called main().
- This is the starting point of the program. This is the point from where the execution begins.
- It will usually call other functions to help to performs its job, some that we write and others from the standard libraries provided.
- main () declares the start of the function/while the curly brackets() shows the sent and finish of the function. Curly brackets in Care used to group statement together as a function, on in the body of a loop. Such a grouping is known as a compound statement on a block.
- Every statement within a function ends with a terminator semicolon (;).
- int is the return value of the main function and instead of int we can use void which means having no return type.
printf(“www.PrabhakarGuru.com \n”);
- The printf is a function which is defined in the stdio.h file and is used to prints the words on the screen.
- The text to be printed is enclosed in double quotes and put inside brackets.
- The message is quoted because in C a text (also known as String or a sequence of charaters) is always put between inverted commas.
- The \n is an escape sequence at the end of the text tells the program to print a newline as part of the output. That means now if we give a second printf statement, it will be printed in the next line.
/* Comments in C Program */
- Comments may appear anywhere within a program, as long as they are placed within the delimitres /* and */.
- Such comments are helpful in identifying the program’s principal features or in explaining the underlying logic of vanous program features.
- These are non executable statement as they are not executed by the compiler.
- // is used to comment a single statement. This is known as line comment whereas /* is ended with */is used to comment multiple statement. The comment inside it is known as block comment.
return 0;
- After all the statement in the program have been written, the last statement of the program will be return statement which will return an integer value 0 to the operating system indicating that there were no errors during the execution of the program.
- In case of void main we don’t need to write return 0.