C Program Structure
A C program basically has the following form:
• Preprocessor Commands
• Type definitions
• Function prototypes -- declare function types and variables passed to function.
• Variables
• Functions
We must have one and only one main() function in a C program.
A function has the form:
type function_name (parameters)
{
local variables
C Statements
}
If the type definition is omitted C assumes that function returns an integer type.
/* Sample program */
main()
{
printf(“Welcome to the world of Computers\n”);
exit(0);
}
NOTE:
• C requires a semicolon at the end of every statement.
• printf is a standard C function -- called from main.
• \n signifies newline. Formatted output -- more later.
• exit() is also a standard function that causes the program to terminate. Strictly speaking it is not needed here as it is the last line of main() and the program will terminate anyway.
Let us look at another printing statement:
printf(“.\n.1\n..2\n...3\n”);
The output of this would be:
.
.1
..2
...3