C – Hello World!

InPursuitThe first program most people learn to write in any computer language is a program called “Hello World!” An online programmer friend recently mentioned that many of the basics you encounter in the programming language are built into “Hello World!” It got me thinking and I decided to post a short example of this classic program with explanations.

Yes, this program can be found many places and I’m reinventing the wheel by writing it. However, every person explains differently and, where someone else may have confused you, I may be the person to explain something in a way you will understand. Also, I find that communicating concepts helps me understand them better.

Here’s the program in C.

#include <stdio.h>

int main() {

   printf("\n\nHello World!\n\n");
   return 0;

}

The first line we encounter is:

#include <stdio.h>

This instruction is called a pre-processor directive. It loads the standard input/output library into the program. Part of the raw power of C is the low level control it gives you over the tasks your computer accomplishes and a good portion of this power is derived from the functionality that libraries provide. The most basic (and most often used) input and output are the keyboard and monitor, used to input and output data (interact with the program).

Libraries contain pre-programmed functions that expand what you can do with a computer language and “stdio.h” is one of the standard libraries included in the C language. You can also write functions of your own to complete repetitive tasks. In fact, there is one function that every C program must have.

Here’s the function that every C program must have to compile and run.

int main() {

}

Programmers refer to this function as “main.” The “int” refers to the type of data that the function returns when it exits, an integer. Functions are capable of returning different types of data or no data at all. A function that returns no data starts with “void” instead and there are other types. I have seen programs that start “main” with “void” instead of “int” but not all compilers will compile them. It is standard to start “main” with “int” and return an integer when the program exits, using a “return” statement.

The actual function is the word “main,” followed by parentheses and a pair of curly braces. The braces can contain data and commands. You can place data inside the parentheses that you want passed to the function. Our example contains a couple of lines of code inside the braces but the parentheses are empty. That is because we are not passing any outside data to “main.”

Here is an example.

return 0;

The “return” statement is used to return data when the function completes. It’s standard for the “main” function to return a zero when it completes, indicating that the program completed and exited normally. The “;” at the end of the line is like the period at the end of a sentence in English. It notifies the compiler that this is the end of the statement.

The one line of the program we haven’t discussed is:

printf("\n\nHello World!\n\n");

This line calls the “printf” function and tells it to print the “Hello World!” message to stdout, which is usually the computer screen. The printf() is the complete function (you only add the braces to functions in which you are writing the commands), everything between the parentheses is the data you are passing to the function and the semicolon at the end of the line tells the compiler this is the end of the statement.

Everything between the double quotes is a string of characters sent to the “printf” function. You can pass data in other ways but, when you are typing a string of characters to send, you must enclose them in double quotes. The “\n” is a newline character and printf() will respond by moving down a line on your screen before writing the message. In this case, the message is printed two lines down from the command used to call the program and is followed by two more new lines, ensuring that there is space before and after the program’s output. This makes the output more readable because it is not surrounded by clutter.

I hope you enjoyed the article. I’m looking forward to any comments, questions or corrections you may have.

Advertisements

2 Replies to “C – Hello World!”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s