It is time to create your C hello world program. By tradition, a "Hello, world!" program just prints that greeting on the screen. This is usually the first program that any developer creates. It serves two purposes:
In this lesson, I will show you how to create your first program for each of the environments from the previous lesson. You will pick and create the program for the IDE that you chose. To create a program you will:
No matter which environment you use, the code for this example is the same:
1 2 3 4 5 6 7 | #include <stdio.h> int main() { printf("Hello world!\n"); return 0; } |
I will show you how to write compile and run the code in the IDEs that we looked at in the last lesson:
I am using Visual Studio Community 2015, but the process is very similar to previous versions(both Express and Professional editions). Every program in Visual Studio is created within a project. To create your C hello world program, create a new project:
1. Open Visual Studio and go to File->New->Project
If this is a new installation, it is possible that the template component is not installed yet. If that is the case, double click on the “Install Visual C++” text to add the component.
You have just created a new empty project. You can see its contents in the “Solution Explorer”. The solution explorer is located to the right, by default. To make it your C hello world project:
Now you are ready to run your C Hello World program!
The result looks like this:
To compile and run code you need a compiler. Make sure you have the Code::Blocks distribution that includes the GCC, otherwise you need to install a compiler manually.
Code::Blocks allows you to create a program in several ways.
The first two methods are convenient only if your program consists of just 1-2-3 files. If your program has more files it is preferred to put them in a project, because it is easier to navigate.
With the C hello world we want to know that everything is working as expected, so let's create your first project! Start Code::Blocks and from nemu File create a new porject:
Now in the left side of the screen go to Projects in the "Management" sub-window. Expand your new project and its folder "Sources". You will see that Code::Blocks automatically added a new source file "main.c". Double click it, to open it.
You see? The "hello-world" code is already there ;)
It is time to compile and run your first C program. Go to menu Build->"Build and Run" or just press F9 and then OK, when you are asked if you want to build the project.
If you downloaded a distribution without a compiler, you need to install one manually.
There are two ways to create our “C hello world” program in Dev-C++:
For a this short program it is easier to choose the first option. However, later for programs with more files it is better to use a project. Here is how to create a C project in Dev Cpp:
In the next window just click "Save" to save the project files in the default location.
Now your project is created and it contains one source file:
Before you can compile and run your C hello world, you need to install a C compiler. For instance to install the GCC in any Debian based distribution, for instance Ubuntu, do the following:
In Geany you can:
I will show you the second option:
This will generate a new C source file. It will start with a 20 row comment(the red text starting with /* and ending with */) that you can delete right away.
Add the line
printf("Hello, world!\n");
between the opening curly bracket "{" and the line "return 0;".
Now you just need to build and execute the file:
When the build is done, you will see the message "Compilation finished successfully." in the Compiler section below the code.
Now press F5 or the "Execute" button to start the program. You will see a console that displays the "Hello, world!" string.
You have just created your C Hello World program! Your development environment is working and tested, so you are ready to continue with the actual part of the basic C tutorial.
In the next lesson you will see what is the typical structure of a C program. To explain it, I will use the example from this lesson.
Previous lesson: C programming software |
Next lesson: C Language Program |