Hello World Program In C
A C program consists of one or more functions or code modules. These are essentially groups of instructions that are to be executed as a unit in a given order and that can be referenced by a unique name. Each C program must contain a main() function. This is the first function called when the program starts to run. Note that while “main” is not a C keyword and hence not reserved it should be used only in this context.
A C program is traditionally arranged in the following order but not strictly as a rule.
Function prototypes and global data declarations |
The main() function |
Function definitions |
Consider first a simple C program which simply prints a line of text to the computer screen. This is traditionally the first C program you will see and is commonly called the “Hello World” program in c language for obvious reasons.
CODE/PROGRAM/EXAMPLE
#include <stdio.h>
void main()
{
/* This is how comments are implemented in C
to comment out a block of text */
// or like this for a single line comment
printf( "Hello World\n" ) ;
}
As you can see this program consists of just one function the mandatory main function. The parentheses, ( ), after the word main indicate a function while the curly braces, { }, are used to denote a block of code -- in this case the sequence of instructions that make up the function.
Comments are contained within a /* ... */ pair in the case of a block comment or a double forward slash, //, may be used to comment out the remains of a single line of test.
The line
printf("Hello World\n " ) ;
Is the only C statement in the program and must be terminated by a semi-colon. The statement calls a function called printf which causes its argument, the string of text within the quotation marks, to be printed to the screen. The characters
are not printed as these characters are interpreted as special characters by the printf function in this case printing out a newline on the screen. These characters are called escape sequences in C and cause special actions to occur and are preceded always by the backslash character, \ .
All C compiler include a library of standard C functions such as printf which allow the programmer to carry out routine tasks such as I/O, maths operations, etc. but which are not part of the C language, the compiled C code merely being provided with the compiler in a standard form.
Header files must be included which contain prototypes for the standard library functions and declarations for the various variables or constants needed. These are normally denoted by a .h extension and are processed automatically by a program called the Preprocessor prior to the actual compilation of the C program.
The line
#include <stdio.h>
instructs the preprocessor to include the file stdio.h into the program before compilation so that the definitions for the standard input/output functions including printf will be present for the compiler. The angle braces denote that the compiler should look in the default “INCLUDE” directory for this file. A pair of double quotes indicate that the compiler should search in the specified path e.g.
Command
#include “d:\myfile.h”
NOTE : C is case sensitive language i.e. printf() and Printf() would be regarded as two different functions.