Get up to 80 % extra points for free! More info:

Lesson 2 - Installing NetBeans and the C compiler

In the previous lesson, Introduction to the C language, we talked about the language itself. In today's tutorial, we're going to install the NetBeans IDE and the C language compiler, so we can start programming. We'll show you how to use it and program a simple console application.

Installing the C compiler

Note: If you use Linux, you don't have to install a compiler.

First of all, we'll download a compiler. It's a computer program (or a set of computer programs) which transforms a source code written using a programming language (the C language in our case) to a lower computer language which the computer processor is able to run.

There are various C language compilers, we'll use Cygwin GCC for this course (if you use another one, we cannot guarantee that all of the examples will work for you). Now, go to http://cygwin.com/install.html and click on "setup-x86.exe" or "setup-x86_64.exe" if you have a 64-bit version of Windows.

Installing Cygwin GCC - The C Language Basic Constructs

Once downloaded, the "Cygwin Setup" will execute and you'll just keep pressing the "Next" button until you see this window:

Cygwin installation - The C Language Basic Constructs

Here we choose the location we want to download the program from. We can choose any address we want, however, it's the best to choose an area which is close to you. I'm going to use http://cygiwn.mirrorcatalogs.com. Once you pick an address, click the "Next" button.

Note: Some of the addresses could potentially not work.

Another window will then appear, this time to choose the packages that are to be installed. We'll collapse the "Devel" package and click on the little "plus" next to it.

Cygwin installation packages - The C Language Basic Constructs

Next, find the "gcc-core" package and click on "Skip" next to it.

Note: The numbers which have popped up indicate the compiler version, so choose the latest one (the greatest number).

GCC devel installation - The C Language Basic Constructs

Then look for the "make" package and click on it's "Skip" as well so it can be installed.

Cygwin installation - The C Language Basic Constructs

Now, just click the "Next" button and then the "Finish" button once the installation is done.

Installing the IDE

First, we'll need to download JDK (Java Development Kit) which is a set of tools needed for Java development. You'll find it at http://www.oracle.com/…s/index.html. Select that you agree with the license and select the right JDK for your operating system (which will likely be Windows 64 or 32, depending on your PC). The installation is trivial, just keep clicking on the Next button. The installation might also run the installation for some other components, if this is the case, just install them as well.

IDE stands for Integrated Development Environment. In a nutshell, it's an application in which we write source code, and then use it to run, test and debug the application.

For this course (and all the other C courses at ICT.social) we'll use NetBeans. Let's open https://netbeans.org/downloads/ and click on the download button under the "C/C++" section.

Note: If you plan on programming in other languages, e.g. Java, download the "all" NetBeans bundle.

Install it (just keep clicking "Next") and run it.

Downloading NetBeans for the C language development - The C Language Basic Constructs

Backup and version control

Programmers usually need a tool that will provide version control and backup of their work. We can't rely on the fact that we could simply save the program because we're humans and humans make mistakes. When you lose a few days' or even a few weeks' work, it can be really demotivating. It's good to think ahead from the get-go. I highly recommend using Dropbox, which is extremely simple, and automatically stores multiple versions of your files, which makes it possible to revert to previous versions of the project. On top of that, it also synchronizes with a web repository. Even if you've accidentally deleted your project, overwrote it, somebody stole your laptop or your hard drive accidentally collapsed, your data will remain safe. Dropbox also allows you to share projects with other developers.

You can also use a tool called GIT for the same purposes, but its configuration would require the while article. Dropbox is perfect for our current intents and purposes.

Hello world

Let's test that our compiler has been successfully installed. As tradition instructs, we'll make the first program most people make when they learn a new language - Hello World. This is a program that displays "Hello world" or some similar text.

Let's open NetBeans (the icon should be on your desktop). Once it starts, click on the brown cube icon at the top left corner (Create a new project).

New project in the NetBeans IDE - The C Language Basic Constructs

A new window will show up. We'll click on the "C/C++ Application" and then on "Next >".

A new C language project in NetBeans - The C Language Basic Constructs

A new window will show up. We'll change the project name to "HelloWorld". There is a drop-down menu next to the main. Open it and choose C. Your form should look like this:

New project in the NetBeans IDE - The C Language Basic Constructs

Once you're sure it's been properly filled, click "Finish".

We're interested mainly in the middle window in which NetBeans will generate some source code. It may be a surprise to some of you that we are not starting out with an empty window, but then again that is exactly what a template is (easily modifiable preset code)! We'll keep it simple for now, everything will be explained throughout the courses.

The gray text between slashes and asterisks are comments. The compiler doesn't mind them, they serve mainly as notes for programmers. Here, they carry the information about who created the program and when.

Our focus should be mainly on the lines which start with #include. There, we declare which libraries we'll be using. A library can be understood as a package of functions. stdio.h contains functions for working with the console and files (it stands for Standard input/output). stdlib.h provides basic functions for working with the C language (it stands for the Standard library).

C programs always start in the main() function. Its body is closed by curly brackets. We can see the return command inside the main() function, which returns the standard output code when the program terminates. This is the way we tell the operating system that the program has finished successfully.

The C language in NetBeans - The C Language Basic Constructs

Let's add code into the main() function just below the line containing:

int main(int argc, char** argv) {

Add the following line:

printf("Hello World!");

It'll write some text to the console once the program starts. Once you're done, you may run your program using the green play button or using the F6 key:

Hello World C NetBeans - The C Language Basic Constructs

The message "Hello World!" will then be written to the console through our program.

Console application
Hello World!

If this isn't the case, make sure you set everything up correctly. If the panel with the program's output didn't show up, open it from the application menu using Window -> Output.

Congratulations, you have just become a programmer :) That will be all for today, in the next lesson, Variables and the type system in the C language, we'll look at the basic data types and create a simple calculator.

Today's project is attached as a file below the article. You will always be able to download the result below each lesson. I highly recommend that you create your own project using the tutorial. If something in your project doesn't work, then, download it to find the mistake. If you download the lesson code before even trying to make it yourself, you won't learn anything :)


 

Previous article
Introduction to the C language
All articles in this section
The C Language Basic Constructs
Skip article
(not recommended)
Variables and the type system in the C language
Article has been written for you by David Capka Hartinger
Avatar
User rating:
2 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities