Hello, World! Program - C

Tradition dictates that we begin with a very simple program, which simply displays the characters "Hello, World!" on the screen and immediately exits. Type the following source code in your preferred text editor/IDE and save this in a file named hello.c.

            #include <stdio.h>

int main(void)
{
    printf("Hello, World!\n");
    return 0;
}
            

Source code analysis

Now we will analyse the program line by line. The various details will be introduced and explained in later chapters.

#include <stdio.h>

This is a preprocessor directive.All preprocessor commands begin with a hash symbol (#).  Preprocessor directives are the instructions to the compiler that specifies how a compiler should process its input. Actually these Directives tell the compiler to modify the code we have written before the compilation. These are not part of the programming language, and may vary from compiler to compiler.

In this case, the #include directive tells the compiler to include information about the standard input/output from the stdio.h file found in the standard library. Files used in this way are called header files and are saved with the .h extension. The stdio.h file contains many functions defined according to the C standard. For this program, the only function we need from stdio.h is the printf function.

int main(void)

The function named main is the starting point of all C programs. A function contains the statements that specify the  work/computation to be done. The statements of a function are enclosed in braces {}.

  printf("Hello World!\n");

Printf is a library function that prints output,in this case the word” Hello World“,between the double quotes. ‘\n’ is a newline character which advances the output to the nextline.

  return 0;

When terminating our program, it is useful to be able to let the operating system know whether or not the program succeeded. We do this with an exit status, which is sent to the operating system with a return statement in the main function. In this case, we provide an exit status of 0 to indicate that execution succeeded without error.observe the output of the following two programs,one with exit status 0 and the other with error.

                #include <stdio.h>

int main(void)
{
    printf("Hello, World!\n");
    return 0;
}
                

Output of the below mentioned program will be :

‘)’ missing in the printf statement.

            #include <stdio.h>

int main(void)
{
    printf("Hello, World!\n";
    return 0;
}
            

Example:

Write the code to print the strings “Welcome” and “Home” in two separate  lines.Try to execute the code without the new line (‘\n’) character and see the output.

            #include <stdio.h>

int main(void)
{
    printf("Welcome!\n");
    printf("Home!\n");

    return 0;
}
            

Below mentione a program without newline character.

            #include <stdio.h>
int main(void)
{
    printf("Welcome!");
    printf("Home!");

    return 0;
}