C Program to print Hello World

C programming is consistently in 2nd position after Java for its popularity as per TIOBE index (a software quality company that publishes popularity index for programming languages every year).

Note: Are you a freelancer? Check out this amazing portal for latest C Programming Jobs.

It is so amazing that this programming language is been in top 2 position right from 1986. It was first released on 1972 by Dennis Ritchie. Now let’s learn to code and compile a C program.

  1. C “Hello World” program
  2. Compiling Steps in C
    1. Preprocessing
    2. Compiler
    3. Assembler
    4. Linker & Loader
  3. Execute “Hello World” C program.

C ‘Hello World’ Program

A hello world c program has only few lines and it is not tough to code. Here is an example for one of the simplest version.

/*
Simplest version of a Hello World C program from officetricks.com
*/
# include <stdio.h>
Main()
{
   Printf(“Hello World to C programming”);
}

Each component in the above program is explained below:

Comments: Programmer can give comments within these symbols ‘/*’ – ‘*/’. Comments are useful to explain the purpose of the program or logic inside a loop. This helps in easy maintenance of the C programs.

If we don’t give any comments in a complex C program, then in future even the person who coded might not remember how logic works. So, entering proper comments is also one of the important steps & a good practice to be followed.

Preprocessor directive: This sections starts with ‘#’. The preprocessor will directly substitute the content of the Header file (ends with a .h extension) in its place in C program.

Program start: Keyword ‘Main’ marks the beginning for program logic & this is where the execution of program will start. All the sub routines & user defined functions will be invoked within a Main function. The different variants of this are ‘int main()’ , ‘void main’. Int or void defines the data type that is returned by the main program to its calling program or system.

Print statement: ‘Printf’ is the keyword that is used to throw any message to the console. Using this command we can display information message or any result of a calculation.

Compiling Steps in C programming

There are different versions of C compiler available in Internet. The simplest and famous one is the Turbo C compiler for Windows OS. If it is Unix or Linux system, then you can directly use the command ‘cc’ to compile a C program.

Preprocessor

This step just processes the lines that start with ‘#’ & this can include preprocessor directives, conditional compilation commands or macros.

A preprocessor’s function is to replace the file content directly in place of a “# include”. In the above ‘Hello World’ c program, the preprocessor will just read the contents in ‘Stdio.h’ file and replace at the starting of the program.

Compiler

Once the .c program has been processed for its preprocessor directives, the compiler converts the C program into an assembly language program. All the commands are converted to its corresponding assembly language equivalent instructions.

Assembler

Assembler takes the output file from compiler with assembly language codes and converts it into machine language code. The output of the assembler is an object file (.obj or .o file)

Link & Load

This step takes the .obj file of source code & other supporting .obj files plus library files, if there are any external references from the program. Thus it resolves all external references & symbols and creates the executable file (in Windows it is .exe file, in Unix/Linux there is no extension).

Execute C Program

Now you are familiar with the steps involved in compiling. To actually compile c program & generate executable file use the below commands depending on the operating system & compiler used.

Windows Turbo C:

  • Compile: Alt + F9 – If this step gives any error, correct them and proceed to next step.
  • Make Exe File: F9
  • Execute: Ctrl + F9 or Programname.exe from command prompt

Unix/Linux:

  • cc programname.c –o programname or
  • Gcc programname.c –o programname

Once the executable file is created after the compile & linking steps, go to command prompt and just type the program name. It will get executed. Sometimes the exe name along with its complete path has to be entered to get it executed.

Leave a Reply