Analyzes the C code for a simple 'Hello World' application to give you some familiarity with the language and the way it works.
C is not such a current language anymore, it was replaced by C++ long ago and recently C++ is being replaced by the .NET Framework. If I would stop the paragraph right here this tutorials is sure to get some hate comments, because there’s something wrong about the first sentence, and I don’t mean to misinform you: C is not fully replaced by C++, the same as C++ is not fully replaced by .NET. This means that for certain projects, C++ is far more appropriate than C# or VB.NET, and sometimes even C is more appropriate than C++. Most game developers – for example – use both C and C++ code in their applications.
So if you are not planning to work on projects that require close access to the computer’s hardware (such as graphic cards) you will probably never need to code something in C. However it’s important to know at least the very basics of the C language, because this gets you closer to understanding how programming works. C, being a low level language, most of the time requires more lines of code (and therefore time) to accomplish something that you could do in C# with only a few lines. On the other hand, C offers you more control over the computer.
How does the code of a C Hello World application look like?
include int main(void) { printf(“Hello World\n”); return 0; } |
On the first line a header file is included: stdio.h. This file contains code that you will need in most of your C applications, this means that in almost any file you will see the #include line. In our case, we need this header file because we use the printf() function which is declared there.
stdio.h, even though its extension is not c, it’s a C file also – it contains C code. Including this file in your code is just like pasting the contents of that file into your code.
Moving on, we have a function named main(). Before its name, we can see a return type void. It’s important to remember that functions can return values of different types (strings, numbers, etc.). You need to define the type the function returns, before its name. Here the the type is void, which is actually not a type, it rather specifies that the function has no return type whatsoever, meaning it doesn’t return any value.
In the parenthesis the arguments are declared. Arguments are values that the function can take inside it (again, same values that can also be returned: strings, numbers, etc.). Having void instead of an argument means we want the function to take no arguments.
A C file can contain multiple functions, but the compiler wouldn’t know with which one to start. That’s why there is a need for a function name main. main is always the starting point for the application.
The { and } braces define the start and the end of the function. What’s inside them belongs to the function.
And inside them we have a line that calls the printf() function. The printf() function is probably the most popular function among C learners. This function outputs the argument it takes to the console. In our case the argument is the string “Hello World“.
What is the result of running this compiled code?