2.5. A simple C program¶
2.5.1. General form of a C program¶
Preprocessor directives int main(void) { declarations statements }
We will see examples of each of type of statement shortly.
2.5.2. Hello World Example¶
/* * The classic 'Hello World' first program. */ #include <stdio.h> int main(void) { printf("Hello, World!\n"); return 0; }
2.5.3. Analysis of Hello World¶
Comments
The /* */ pair defines a comment block.
#include <stdio.h>
Definitions and prototypes are pulled from a system header file. #include "my.h" is for user defined header files. In Unix, system header files are in /usr/include with the libraries in /usr/lib or /lib. User defined headers should be in the same directory as the source code.
int main(void)
main() is where the program begins. It is in every program. Notice that main() is a function like other functions.
int means that it will return an integer value to the operating system. Generally main() will either be of type int or void.
(void) means that no arguments are passed to main().
{ }
Indicates the start and end of each function or block of code. We define a block of code as a set of statements that are to be executed sequentially as a group.
printf( );
A function to print to the screen. C was one of the first languages to use a function for printing. The argument is a formated character string. \n means to start a new line.
return 0;
Return to the OS with an int value. 0 = success, 1 = failure.