With Python and Java in the books, let’s get a little closer to hardware. In this edition of Hello World in Every Language, we’re learning how to implement Hello World in C.
Table of Contents
C Background
As usual, I decided to dive into the history of C with Wikipedia.
According to Wikipedia, C is a general-purpose programming language that was developed as early as 1969 at Bell Labs by Dennis Richie. Today, the C language has influenced the design of many languages such as C++, C#, Go, Java, and many, many more.
Features of C include static typing, lexical variable scope, and recursion. In addition, function parameters are passed by value, not reference, but pass-by-reference can be accomplished by explicitly using pointers. Also, while C has static typing, variables are weakly typed and can be converted implicitly.
Hello World in C
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.
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
.
Sample Programs in Every Language
Since the hello world syntax is so different between C and C++, I decided to separate them into different articles. As a result, we’ll tackle C++ next. After that, we’re looking to tackle C#.
Once again, if you liked this article, give it a share. Also, feel free to leave your comments below.
Recent Posts
The Difference Between str() and repr() in Python: A Design by Contract Perspective
Recently, I was giving a lecture about Java's "common" methods (i.e., all of the methods of Object), and I had epiphany about how Java only has toString() while Python has str() and repr(). So, it...
Magic numbers are numerical constants that have no clear meaning in the code and therefore make code harder to read. Anything that makes code harder to read is something we can use to obfuscate our...