C Program Structure
Posted on December 23, 2009
Structure Of A C Language Program
The best way to start understanding the structure of a C program is to explain it with the help of a small and a simple program. So let us take a simple program example to begin :
/* simple program in C */
#include
int main()
{
printf(“ \n welcome to the world of C ”);
return 0;
}
Now we can see that there are several components or forms in the above example and each of the above component has it’s own significance and importance.
These components or forms can be named as :
- Comments
- Preprocessor directives or preprocessor commands
- Type definitions
- Function and it’s prototypes
- Variables
- Statements, conditions and expressions
Now we start discussing all these components one by one.
Comments:
Comments are used only to allow the programmer or user to write additional information within the program. Comments are parts of the program code disregarded by the compiler. They are optional and does not have much relevance with the code.
From the very first line of the above code we can see that a comment is written within /*….*/ . It will simply print the message.
Preprocessor commands:
#include
Lines beginning with a hash sign (#) are directives for the preprocessor. With the help of these commands, the compiler is given an instruction to preprocess the code before doing the necessary compilation of the code. The statement #include is used to direct the compiler of C to include stdio.h file before executing the actual code.
Function:
int main()
The above line shows the beginning of the definition of the main function.
The main function is the important and a must required function as it is the point by where all programs begin their execution. The instructions given within this function’s definition will be executed first, it does not matter whether there are some other functions also present in the code.
It is necessary for every code to have a main function in it.
The pair of parentheses after the word main may contain one or more parameters within them.
Type-definition:
The word int used before the word main indicates the data-type. Therefore, in the above code example it means that the function will be returning an integer value if it exists.
In actual, there are two types of functions:
- Built-in function
- User-defined function
Printf():
The function printf() used in the above code is an built-in function which is used to print any message on the output screen. The message to be print is given inside the parentheses after the word printf. We can also use a word print in place of printf.
Variables:
A variable is just a name used to store a single value that can either be a numeric or character or string.
To declare a variable in C we follow the following syntax :
Data-type list variables
int a, b, c;
float i, j, k;
char choice;
Statements, conditions and expressions:
These comprise of all the function calls, assignments, and statements used in the C program.
For e.g. In the above code the word return is used as a statement or an expression which is used to return the integer value.
Important Characteristics of a C Program Structure:
- Each statement of a C program is ended with a semi-colon or a semi-colon is used after each statement to terminate the statement.
- C language is case sensitive. Therefore, we must pay attention while writing the code. e.g. we cannot write Int in place of int.
- We can write multiple statements on the same line.
- Usually white spaces are ignored in C language.
Looking for a Fresher Job? Post Your Resume Free !!!
More Articles From "C Programming" Category
- Variable Types in C Programming Language
- Derived Data Types in C Programming Language
- C Language Operator Types: Operators in C
- C Programming Primary Constants: Integer, Real & Character
- Constants in C Programming Language: Primary & Secondary
- Storage Classes in C Programming Language: Auto, Register, Extern & Static Storage Types
- C Data Types: Range & Size
- Type Modifiers in C Language: signed, unsigned, long & short
- C Language Fundamental Data Types: Int, Float, Char, Double
- C Language Data Types