|
Getting Started |
|
The main( ) function |
A C\C++ program consists of one or more modules called functions .
Each C\C++ program must have one function called main( ).
The reserved word main tells the compiler where program execution is to begin .
The main ( ) function is sometimes referred to as a driver function , because it tells the other functions the sequence in which they are to operate .
|
|
type of returned value |
function name |
argument list |
|
function body |
int |
main |
( ) |
|
{ } |
|||
The braces { and } determine the beginning and end of the function .
Small C program
Small C++ program
#include<stdio.h>
int main ( )
{
printf ("Hello World ");
return 0;
}
#include<iostream.h>
int main ( )
{
cout<<"Hello World ";
return 0;
}
#include<stdio.h> (#include<iostream.h>) is a preprocessor command (directive).
Preprocessor commands begin with a pound sign # .
The #include preprocessor command causes the contents of the named file, in this case stdio.h (iostream.h) , to be inserted before the program is compiled . The file stdio.h is referred as header file because it is placed at the top or head of a C program .
The stdio.h (iostream.h) file allows standard input/output operations. provide interface to the printf ( )(cout<<) function and should be included in all programs using printf( ) (cout<<) ( You can't print to the screen without using the #include<stdio.h>)
Preprocessor Commands do not end with a semicolon(;) .
If no #include<stdio.h> then error " Call to undefined function printf ( )"
return 0 , return control to the Operating System witch means that the program terminated normally ( A way to exit a function).
Since printf ( ) is prewritten function ,we do not have to write it (code it) ; it is available for use just by calling it correctly .
Statements in C ends with a semicolon (;).
Linker .When the compiler compiles printf statement , it provides space in the object program for a "call" to the library function .But the compiler does not know where the library functions are. The linker does. So when the linker runs , it locates the library functions and inserts the proper calls to these library functions in the object code. Now the object is complete and ready to be executed . The linked program is often called the executable. If function name is misspelled, the linker will produce an error because it will not be able to find function in the library.
Examples
(1) Return 6;
No errors
(2) Return 0;
printf(“hello”);
No print is there
|
Programming style |
Note that more than one statement can be put on a line ,or one statement can be written across lines.
C ignores all white space (white space refers to any combination of one or more blank spaces, tabs or new line) except for statements contained with double quotes, function names and reserved words.
_____ Indentation ( Leading white space ) is a good programming practice.
Escape character (\)
Indicates that printf should do something out of the ordinary.
\n is the newline character.