Printing A Message:
main(
)
1" .pri
nting beg i ns.
pri ntf
("I see, I remember") ; /* ....printing ends.....................*/
|
This program when executed will produce the following output:
I see, I remember
Let us have a close look at the program. The first line informs the system that the name of the program is main and the execution begins at this line. The main( ) is a special function used by the C system to tell the computer where the program starts. Every program must have exactly one main function. If we use more than one main function, the compiler cannot tell which one marks the begin¬ning of the program.
The empty pair of parentheses immediately following main indicates that the function main has no arguments (or parameters).
The opening brace "{ " in the second line marks the beginning of the function main and the closing brace "}" in the last line indicates the end of the function. In this case, the closing brace also marks the end of the program. All the statements between these two braces form the ftinction body. The function body contains a set of instructions to perform the given task.
In this case, the function body contains three statements out of which only the printf line is an executable statement. The lines beginning with /* and ending with */ are known as comment lines. These are used in a program to enhance its readability and understanding. Comment lines are not executable statements and therefore anything between /* and */ is ignored by the compiler. In gen¬eral, a comment can be inserted wherever blank spaces can occur--at the beginning, middle or end of a line—"but never in the middle of a word ".
Although comments can appear anywhere, they cannot be nested in C. That means, we cannot have comments inside comments. Once the compiler finds an opening token, it ignores everything until it finds a closing token. The comment line
is not valid and therefore results in an error.
Since comments do not affect the execution speed and the size of a compiled program, we should use them liberally in our programs. They help the programmers and other users in understanding the various functions and operations of a program and serve as an aid to debugging and testing. We shall see the use of comment lines more in the examples that follow.
Let us now look at the printf( function, the only executable statement of the program.
printf ("I see, I remember");
printf is a predefined standard C function for printing output. Predefined means that it is a function that has already been written and compiled. and linked together with our program at the time of linking. The concepts of compilation and linking are explained later in this chapter. The printf func¬tion causes everything between the starting and the ending quotation marks to be printed out In this case, the output will be:
I see, I remember
Note that the print line ends with a semicolon. Ever), statement in C .thould end with a semicolon ( mark.
Suppose we want to print the above quotation in two lines as
I see,
I remember !
This can be achieved by adding another printf function as shown below:
printf(I see, \n");
Iprintf( .I remember !
The information contained between the parentheses is called the argument of the function. This argument of the first printf function is" I see, \n" and the second is "I remetnber!". These arguments are simply strings of characters to be printed out
Notice that the argument of the first printf contains a combination of two characters \ and n at the end of the string. This combination is collectively catled the newline character. A new line character instructs the computer logo to the next (new) line. It is 'similar in concept to the carriage return key on a typewriter. After printing the character comma t.) the presence of the newline character VI causes the string "I remember!" to be printed on the next line. No space is allowed between I and n.
It we omit the newline character from the first printf statement, then the output vsi I again be a single line as shown below.
see J remember !
This is similar to the output of the progrimi in Fig I 2 However. note that there is no space between . and I
It is also possible to produce two ot nut/ e lines ot output by one printf statement with the use newline character at appropriate pla,s lttr eydmple, the statement printf ("I see, \n I remember I");
will output
I see,
I remember!
while the statement
Printf( "I\n.. see,\n_ _ _ I\n_ _ ... remember !");
will print out
I
.. see
... remember !
NOTE: Sonic authors i,commend the inclusion of the statement
#include <stdio.h>
at the beginning of all programs that use any input/output library functions. However, this is not necessary for the functions priori and scal#'which have been defined as a part of the C language. See Chapter 4 for more on input and output functions.
Before we proceed to discuss further examples, we must note one important point. C does make a distinction between uppercase and lowercase letters. For example, printf and PRINTF are not the same. In C. everything is written in lowercase letters. However, uppercase letters are used for sym¬bolic names representing constants. We may also use uppercase letters in output strings like "I SEE" and -I REMEMBER"
The above example that printed I see, I remember is one of the simplest programs. Figure highlights the general format of such simple programs. All C programs need a main function.
The main Function
The main is a part of every C program. C permits different forms of main state¬ment. Following forms are allowed.
• main()
• int main()
• void main()
• main(void)
• void main(void)
• int main(void)
The empty pair of parentheses indicates that the function has no arguments. This may be explicitly indicated by using the keyword void inside the parentheses. We may also specify the keyword int or void before the word main. The key¬word void means that the function does not return any information to the operat¬ing system and int means that the function returns an integer value to the operating system. When int is specified, the last statement in the program must be "return 0". For the sake of simplicity, we use the first form in our programs.