Hello World in C

Hello World in C Featured Image

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 WikipediaOpens in a new tab..

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 compilerOpens in a new tab.. In addition, we’ll probably want to get a copy of Hello World in COpens in a new tab.. 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 compilerOpens in a new tab.. Alternatively, we can leverage a tool like MinGWOpens in a new tab..

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.

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts