Structure of a C program
C is a case sensitive programming language which means in C two variables named x and X will have different meanings. However, it is a free-form language which means any C statement can start in any column but end of each C statement must be marked with a semicolon (;). In addition, multiple statements can be on the same line, statements can continue over multiple lines and white spaces (i.e. TAB and SPACE BAR) are ignored. In general, a C program basically contains following elements:
- Preprocessor Commands
2. Functions
3. Variables
4. Statements & Expressions
5. Comments
Consider a simple program below:
Pre-Processor Commands
These commands tell the compiler to do some processing before doing actual compilation. Like #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h header file before going to actual compilation. stdio.h refers to a file supplied along with the C compiler which contains definitions of many functions that perform input-output with standard devices like keyword and screen. For example, one of the functions in the file stdio.h is printf() which accepts a string of character enclosed in double quotes to be displayed on the screen. This header file must be included in every program that reads some data from keyboard or displays some data on screen. stdio.h is not the only header file available in C language and to include other header files in a C program the same pre-processor command with different header file name can be used.
Functions
Functions are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is called main() function. The main() function is the most important function and must be present in every C program. The execution of a C program begins in the main() function. The opening brace { indicates the beginning of the function. The closing brace } indicates the end of the function. This function is prefixed with keyword void which means this function returns nothings when it exits. However, it can also be prefixed with int in which case it means it will return some value. And to return that value return keyword is used.
The C Programming language provides a set of built-in functions. In the above example printf() is a C built-in function which is used to print anything on the screen.
Variables
Variables are used to hold numbers, strings and complex data for manipulation.
Statements & Expressions
Expressions combine operators, variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs.
Comments
Comments are used to give additional useful information inside a C Program. Comments are optional and are just used to increase the readability of the program. All the comments are put inside /*…*/ as shown in the previous example. However, this comment can span through multiple lines and may appear anywhere in a C program. Single line comments can also entered by just prefixing them with //. The program 4.1. shows the general template of a C program that will used to create and test our programs.