A First Program
C program, whatever its size, contains functions and variables.
A function contains statements that specify the computing operations to be
done, and variables store values used
during the computation [KR88, page 6].
The following program is the traditional first program presented in introductory C courses and textbooks.
1 /* First C program: Hello World */
2 #include <stdio.h>
3
4 int main(void)
5 {
6 printf("Hello
World!\n");
7 }
1 Comments
in C start with /* and are terminated with */. They can span multiple lines and
are not
nestable. For example,
/* this attempt to nest two comments /* results in just
one comment, ending here: */ and the remaining text is a syntax error. */
2 Inclusion
of a standard library header-file. Most of C’s functionality comes from
libraries. Header-
files contain the information necessary to use
these libraries, such as function declarations and
macros.
macros.
4 All C
programs have main() as the entry-point function. This function
comes in two forms:
int main(void)
int main(int argc, char *argv[])
The first takes no arguments, and the second
receives command-line arguments from the environment in which the program was executed—typically a
command-shell. (More on command-line arguments in Section 13.4.) The function
returns a value of type int (i.e., an
integer ).2
5 and 7 The
braces { and } delineate the extent of the function
block. When a function completes, the
program returns to the calling function. In
the case of main(), the program terminates and control
returns to the environment in which the program was executed. The integer return value of main()
indicates the program’s exit status to the environment, with 0 meaning normal termination.
returns to the environment in which the program was executed. The integer return value of main()
indicates the program’s exit status to the environment, with 0 meaning normal termination.
6 There is only one statement in this program: Function calls for the standard library function printf ()
which prints a
character string to standard output
(usually the screen). Note, printf() is not a part of the C language, but a function provided by the standard
library (declared in header stdio.h). The
standard library is a set of functions mandated to exist on all systems
conforming to the ISO C standard.
In this case, the printf() function takes one
argument (or input parameter): the string constant "Hello World!\n". The \n at the end of the string is an escape character to start a new line. Escape characters provide a mechanism for
representing hard-to-type or invisible characters (e.g., \t for tab, \b for
backspace, \" for double quotes). Finally, the statement is terminated
with a semicolon (;). C is
a free-form language, with program meaning unaffected by whitespace in most circumstances. Thus, statements are terminated
by ; not by a new line.
2You can see in the example program above, the main () indicate that it is rounded up in the interface declaration, but actually does nothing back; the function body (lines
5-7) contains no return statement. The reason is that for main(), and main() only,
an explicit return statement is optional (see Chapter 4 for more details).
No comments:
Post a Comment