Friday 16 May 2014

First Program “HELLO WORLD”



When we start reading any language like English then we have to knowledge of letter, word, sentence and at last grammar. Likewise in c language token, keyword, statement and syntax respectively. In previous post we seen all these things so let us see the first program of printing the hello world in the screen (Command prompt).


      1   #include<stdio.h>
      2
      3   int main()
      4   {
      5          printf("Hello World");
      6                        // this statement display the string Hello World
      7         getchar();
      8         return 0;         
      9    }
  

   A C program basically consists of the following parts:
  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

In this program; in first line '#' means a Pre-processor and "include" is used for including the header file and libraries.To know more about Pre-processor plz Click-Here.
 Here "stdio.h" is added which includes some pre defined and standard i/o function like getchar(),printf().

 In third line, we already know "int" which is data type but here it is also called as return type.Return type means which type must be return by the function. Here it is return as integer type therefore in the 8th line, it returns 0.

In fifth line, printf() is a pre-defined function which is defined in stdio.h  header file.And used for printing the "Hello World" in a console.

In the seventh line, getchar() holds the screen until the user press "enter" key only. Here we also used getch() instead of getchar() but difference is that getch() holds the screen until the user press any key.

Compile & Execute C Program:

Lets look at how to save the source code in a file, and how to compile and run it. Following are the simple steps:
  1. Open a text editor and add the above-mentioned code.
  2. Save the file as hello.c
  3. Open a command prompt and go to the directory where you saved the file.
  4. Type gcc hello.c and press enter to compile your code.
  5. If there are no errors in your code the command prompt will take you to the next line and would generate a.out executable file.
  6. Now, type a.out to execute your program.
  7. You will be able to see "Hello World" printed on the screen

    $ gcc hello.c
    $ ./a.out
    Hello, World!
Output of this program is shown as:


0 comments :

Post a Comment