First C program, Hello World

After talking about the history and compilers it is time to make our first program. We like to make C programs under Linux, so we will use a text editor and the gnu compiler. But all programs we make in these tutorials will also work under Windows. Remember, the examples included in the C and C++ tutorials are all console programs. That means they use text to communicate. All compilers support the compilation of console programs. Check the user’s manual of your compiler for more info on how to compile them. (It is not doable for us to write this down for every compiler).

Open a text editor (vi, emacs, notepad) or make a console project in Visual Studio Express and type the following lines of code (Don’t use cut/paste, type them. This is better for learning purposes):


#include<stdio.h>

int main()
{
printf(“Hello World\n”);
return 0;
}

Save the program with the name: hello.c

#include<stdio.h>

With this line of code we include a file called stdio.h. (Standard Input/Output header file). This file lets us use certain commands for input or output which we can use in our program. (Look at it as lines of code commands) that have been written for us by someone else). For instance it has commands for input like reading from the keyboard and output commands like printing things on the screen.

int main()

The int is what is called the return value (in this case of the type integer). Where it used for will be explained further down. Every program must have a main(). It is the starting point of every program. The round brackets are there for a
reason, in a later tutorial it will be explained, but for now it is enough to know that they have to be there.

{}

The two curly brackets (one in the beginning and one at the end) are used to group all commands together. In this case all the commands between the two curly brackets belong to main(). The curly brackets are often used in the C language to group commands together. (To mark the beginning and end of a
group or function.).

printf(“Hello World\n”);

The printf is used for printing things on the screen, in this case the words: Hello World. As you can see the data that is to be printed is put inside round brackets. The words Hello World are inside inverted ommas, because they are what is called a string. (A single letter is called a character and a series of characters is called a string). Strings must always be put between inverted commas. The \n is called an escape sequence. In this case it represents a newline character. After printing something to the screen you usually want to print something on the next line. If there is no \n then a next printf command will print the string on the same line.

Commonly used escape sequences are:

  • \n (newline)
  • \t (tab)
  • \v (vertical tab)
  • \f (new page)
  • \b (backspace)
  • \r (carriage return)

After the last round bracket there must be a semi-colon. The semi-colon shows that it is the end of the command. (So in the future, don’t forget to put a semi-colon if a command ended).

return 0;

When we wrote the first line “int main()”, we declared that main must return an integer int main(). (int is short for integer which is another word for number). With the command return 0; we can return the value null to the operating system. When you return with a zero you tell the operating system that there were no errors while running the program. (Take a look at the C tutorial – variables and constants for more information on integer variables).

Compile

If you have typed everything correct there should be no problem to compile your program. After compilation you now should have a hello.exe (Windows) or hello binary (UNIX/Linux). You can now run this program by typing hello.exe (Windows) or ./hello (UNIX/Linux).

If everything was done correct, you should now see the words “Hello World” printed on the screen.

Congratulations!!!!!
You have just made your first program in C.

Comments in your program

The Hello World program is a small program that is easy to understand. But a program can contain thousands of lines of code and can be so complex that it is hard for us to understand. To make our lives easier it is possible to write an explanation or comment in a program. This makes it much easier to understand the code. (Even if you did not look at the code for years).These comments will be ignored by the compiler at compilation time.

Comments have to be put after // or be placed between /* */ .
Here is an example of how to comment the Hello World source code :


/* Description : Print Hello World on the screen
Author : Your name
Date : 01/01/2007 */

#include<stdio.h>

int main()
{
//Print something and then newline
printf(“Hello World\n”);
return 0;
}

Indentation

As you can see the printf and return statements have been indented or moved to the right side. This is
done to make the code more readable. In a program as Hello World, it seems a stupid thing to do. But as the programs become more complex, you will see that it makes the code more readable.

So, always use indentations and comments to make the code more readable. It will make your life much easier if the code becomes more complex.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There are currently 52 responses to “First C program, Hello World”

Why not let us know what you think by adding your own comment!

  1. saurabh dubey on January 29th, 2010:

    hi

    this article is very helpful for beginners…

  2. Aman on May 24th, 2010:

    thanx…

  3. gladys.c on July 15th, 2010:

    tnx

  4. prabhath on January 9th, 2011:

    this article good who has not teory prts in c language..tnx…

  5. rahul on February 22nd, 2011:

    i like it. thanks

  6. Ben on April 26th, 2011:

    very nice, thanks

  7. muditha on May 6th, 2011:

    Interesting & encouraging the beginners of C programming.thks a lot.

  8. Sina on May 17th, 2011:

    thanks very much!!!

  9. tej on May 30th, 2011:

    hi,
    i found a starting point of c in this articles from where the c language begins
    i feel good while i go through this one
    thank you.

  10. sandeep reddy on August 2nd, 2011:

    its a good path to learn C for the beginners…. great work…

  11. anonymous on September 28th, 2011:

    awesome, after one year i found a website where i understand the c language so easily.

  12. Roshan kashyap on January 30th, 2012:

    Thanks
    It is a very good for beginners

  13. ali on February 9th, 2012:

    thank you
    go a head

  14. bharat on March 13th, 2012:

    good thanks i read in web site program .i am happy.

  15. Khalid on April 17th, 2012:

    i like this coding for hello program and inshallah we read further in future.

  16. vikrant on June 5th, 2012:

    this is very good for beginners

  17. sravya on August 10th, 2012:

    very useful for the beginers

  18. Cynthia on August 24th, 2012:

    good one… Very useful…

  19. fadhil ally on September 3rd, 2012:

    this concept is some how good but it not explained well about return and int

  20. admin on September 6th, 2012:

    @fadhil ally: we’ve added some additional information to this tutorial, so return and int will be easier to understand. But we don’t want to go into to much detail in this tutorial. We want to keep the first one as easy as possible. When you read the next tutorials, everything will be explained in more detail. So I urge you to read on! Good luck.

  21. manish singla on October 1st, 2012:

    thanks
    it is very easy to understand for beginners

  22. sagar singh on October 15th, 2012:

    very good information of all user
    sagar[DIST]

  23. vishnu on November 16th, 2012:

    excellent tutorial :)

  24. pooja on November 20th, 2012:

    its really easy n very helpful
    thanx :)

  25. salman on December 12th, 2012:

    thnx’s!!!!

  26. ajay rathore on December 13th, 2012:

    thank u

  27. fahad on December 19th, 2012:

    i am the beginner in c language,,,,,,i found this website more than just a website… i think this this is ELECTRONIC TEACHER

  28. sem shemzigwa on January 5th, 2013:

    Its helpfull bcoz i learn a lot
    Thanks.

  29. segzy on January 14th, 2013:

    it is very good and explanatory enough to me. Nice 1

  30. newbie01 on February 2nd, 2013:

    Nice article especially for those wanting to go back to C programming … like me :(- … would be nice if there is an option to copy the the code to clipboard though? When I copy and paste from IE, some are getting pasted as special characters :(-

  31. surton on February 23rd, 2013:

    goodto have

  32. raj swami on March 6th, 2013:

    this is helpful..
    bt #include
    header file is also used….

  33. GaneshYadav on March 30th, 2013:

    this is helpful

  34. RAJKUMAR on June 21st, 2013:

    thankyou………

  35. RAJKUMAR YADAV on June 21st, 2013:

    THANKU ………………..

  36. harishma on june 24th,2013 on June 24th, 2013:

    nice to learn……..
    v got the basics easily…….
    tnx for sharing………….

  37. miraul islam on July 14th, 2013:

    Thanks&very good

  38. Binay on July 29th, 2013:

    Article is presented in a very nice and easy to understand manner . Thanks

  39. Akash on August 1st, 2013:

    Very easy to understand

  40. person on August 2nd, 2013:

    I finally understand it. Best site for learning c

  41. shreyansh on August 2nd, 2013:

    thanx!!! this was very helpful for beginners

  42. ashwini on September 13th, 2013:

    thank you….

  43. rocky on October 17th, 2013:

    Good Program

  44. davy on October 25th, 2013:

    indeed it is worthy for begginers

  45. Ajay on October 30th, 2013:

    I am including stdio.h header file 2times in same file am not getting any problem, if am including own file like “add.h” 2times its giving an error. what is the problem can anybody explain?

  46. Paul Koenen on January 29th, 2014:

    Ajay, the reason you can include stdio.h multiple times is due to a condition that is usually included at the top of an INCLUDE file:
    #ifndef _INC_STDIO << at the beginning ….
    #define _INC_STDIO
    ….
    #endif << placed on the last line.

    This effectively keeps the include instructions in stdio.h from only being executed once. It defines the _INC_STDIO at the start of the first include and turns all future includes into a No-Op. So your 2nd include of stdio.h is safe, but is not doing anything.

  47. prashantsingh on February 25th, 2014:

    it very nice for me.

  48. Pooja on March 4th, 2014:

    nice article ….good job..like it..

  49. Chandu on March 6th, 2014:

    Its really cool… I am sure it will be a kick-start for beginners.

  50. Chandu on March 6th, 2014:

    #include
    int main()
    {
    printf(” Hello,Coding Unit!!! Thanks for helping us\n”);
    }

  51. RIFAYATHALI on March 6th, 2014:

    very nice article. it really useful to us in basic c programming thanks!!!!!!

  52. Manasa on March 13th, 2014:

    very nice.thank you so much………

Leave a Reply: