Hello World in C

Published on 05 December 2018 (Updated: 05 December 2018)

Hello World in C

In this article, we’ll demonstrate how to implement Hello World in C.

How to Implement the Solution

Since C predates both Java and Python, the syntax is naturally a bit archaic. That said, you’ll find that the syntax for Hello World in C is still easier to understand than Java:

#include <stdio.h>
int main()
{
   printf("Hello, World!");
   return 0;
}

At the top, we’ll notice an include statement. Basically, this statement copies in functionality from the standard IO library of C. This includes the printf functionality we’ll need to actually write our string to the command line.

Like Java, we’ll notice that we have a main function. In C, the main function is much simpler. In fact, we don’t even have classes in C, so we don’t have to bother with that extra layer of abstraction. Instead, we can define the main function directly. Again, we can only define one of these per program.

Inside the main function, we’ll find our usual call to print. However, in C, we use printf which allows us to format strings as well.

Finally, we’ll notice that we return zero. That’s because the main function is like any other function, so it has a return type. In this case, the return type is an integer, and that integer is used to indicate status codes. A status code of zero means no errors occurred.1

How to Run the Solution

Now, if we want to run the solution, we’ll need to get a hold of a C compiler. In addition, we’ll probably want to get a copy of Hello World in C. With both prerequisites out of the way, all we have to do is navigate to our file and run the following commands from the command line:

gcc -o hello-world hello-world.c
./hello-world

Of course, these are Unix/Linux instructions. If we’re on Windows, it may be easier to take advantage of an online C compiler. Alternatively, we can leverage a tool like MinGW.1


References

  1. J. Grifski, “Hello World in C,” The Renegade Coder, 18-March-2018. [Online]. Available: https://therenegadecoder.com/code/hello-world-in-c/. [Accessed: 05-Dev-2018].  2