First Program
A C program, whatever its size, consists of functions and variables. A function contains statements that specify the computing operations to be done, and variables store values used during the computation.
The following program is the traditional first program presented in introductory C courses and textbooks.
/* First C program: Hello World */
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
}
- Comments in C 1 start with /* and are terminated with */. They can span multiple lines and are not nestable.
- Inclusion of a standard library header-file. Most of C’s functionality comes from libraries. Headerfiles contain the information necessary to use these libraries, such as function declarations and macros.
- All C programs have main() as the entry-point function. This function comes in two forms:
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. The function returns a value of type int(i.e., an integer).
- 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.
- This program contains just one statement: a function call to 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 white space in most circumstances. Thus, statements are terminated by ; not by a new line.
No comments:
Post a Comment