Getting Started With C Using NetBeans
Written by Harry Fairhead   
Monday, 13 June 2016
Article Index
Getting Started With C Using NetBeans
Hello C World

C is still, after all these years, a good language to learn. It is simple and close to the machine hardware and this makes it a good first language. The only problem is getting started, which this article makes easy.

 

Cbookcover

Fundamental C For The IoT & Embedded Programming

Available as a print book very soon - only two chapters left to write! I expect it to be published at the end of 2018.

Part I  - The Core Language

  1. Getting Started With C Using NetBeans
  2. Program Structure
  3. Low Down Data
  4. Expressions
  5. Functions 
  6. Arrays
  7. Strings
  8. Pointers
    Extract 1Pointers, Cast & Type Punning
  9. Structs
  10. Bit Operators & Manipulation
  11. Small Machine Arithmetic
  12. File I/O Streams and File Descriptors
  13. Sockets
  14. Memory mapped files
  15. sysFS
  16. Threading
    Extract 1 Condition Variables**NEW

 

 

C has a long history which I'm not going to bore you with, but it was invented in 1973 with the specific purpose of implementing Unix. Even today it is the language that most operating systems are implemented in and it is experiencing a resurgence because of its use in small Single Board Computers (SBCs) such as the Raspberry Pi and other IoT devices.

The reason that it is popular in these areas is that the C language is modelled on what happens under the covers. Its facilities are close to what the machine actually does and it is fairly easy to follow how C is converted into assembly language for a particular machine. Another way of saying this is that C is just one step of abstraction away from assembler, whereas other languages are trying to take as many steps of abstraction from the underlying machine as possible. 

As a result it is reasonable, but not entirely accurate, to call C a machine independent high level assembly language - but you can argue about such things to no real result.

The key point is that C is a lowish-level language and this makes it possible to write programs that are sensitive to the hardware available. C programs can be compact, i.e. use little memory, and they can be fast. For operating systems and embedded hardware these are important considerations. C also makes it possible to deal directly with the hardware, something that is often impossible in other languages that explicitly set out to stop you doing this. 

This brings us to the disadvantages of C. 

If it can be done then it can be done in C. There are few safeguards in the C language and if you aren't careful, or if you want to, then bringing the whole machine down is possible. Not only can you crash the machine, but you can "brick" it - mainly by destroying the firmware or by disabling essential hardware. Don't get too worried, you have to try hard and know a lot before this is even a possibility.

What is more likely is that you can create a program that has inherent security problems. If this is an important consideration then you need to exercise caution when using C and one of the best ways of doing this is to use a good IDE, like NetBean,s which will warn you when you use things that are inherently dangerous.

OK so if C is such a good language, why not move to C++ which must be better still?

This is only partially true. C++ is a more sophisticated language. It is object-oriented and as such has to be a little slower than C and its programs use slightly more memory. To be clear C++ is still fast and it is still compact. Sometimes a C++ program can be as fast and as compact as the equivalent C program, but if you value efficiency above all else then C is the best choice. 

It is also be best choice for the complete beginner. The reason is that it doesn't have the abstract sophistication of C++ and other object-oriented languages. It is a simple modular language that, as already mentioned, brings you into contact with underlying machine ideas. For a beginner wanting to master computing and computer science this is no bad thing.  For the more advanced ideas that are certainly needed you can move on to C++ and discover object-oriented programming when you are ready.

So C is a great first language for educational reasons and it is a practical and still important language. 

What more could you want.

Using NetBeans

You can program C using nothing more than a text editor and the command line, but this is not as productive as using an IDE. This is an arguable point if you are a C expert, but for the beginner and the causal user there is no doubt that an IDE is the best way to go. 

But which IDE?

If you are working with Windows then there is tendency to choose Visual Studio Community edition. It is free to use, but it supports so many options that it can be confusing. There is also the small matter that really Microsoft is more interested in C++ than C. Currently its C compiler only supports as much C89 as needed to make C++ work - but this is getting better.

A bigger problem with using Visual Studio is that it doesn't support any cross compilers. That is, you can only compile code that runs under Windows on an Intel processor. This will not be a problem if you don't want to make use of any embedded devices based on other CPUs. If you do then you really need to make use of a compiler that can target a range of different CPUs, such as the well known GCC GNU Compiler Collection. 

GCC is standard on Linux and many other operating systems, but it isn't standard on Windows. Another problem with using it is that it doesn't come with an IDE. Fine if you want to use the command line and a text editor, but as already discussed not so good if you want to learn C and write C only occasionally. 

There are a number of different IDEs that can make use of the GCC - Geany, C Lion and even Microsoft's own Visual Studio Code. However a good all-rounder, and one that can be used with multiple languages on multiple platforms, is NetBeans - it also happens to be free to use and open source. You can use NetBeans to program in Java, JavaScript with HTML/CSS, C/C++, Fortran, Assembler and PHP.

In short it is worth getting to know. 

NetBeans C/C++ Under Linux And Windows

Using NetBeans under Linux or Windows  is so trivial that it almost doesn't need any additional explanation. 

All you have to do is down load a version of NetBeans from  

 https://netbeans.org/downloads/

If you only plan to work with C/C++ I recommend only downloading the C/C++ version because it contains fewer irrelevant modules that often serve to confuse the beginner. 

Once you have the downloaded the installer under Windows simply run it. 

Under Linux the only difference is that you have to change permissions

chmod +x <installer-file-name>

and the run it

 ./<installer-file-name> 

In the case of Windows an icon will be added that you can use to run it. In the case of Linux you will find the executable  in the bin directory.

Installing GCC

Under Linux there should be nothing to do as GCC is usually installed by default - it is certainly standard on Ubuntu, Rasbpian and more.  If not you have to use whatever packet manager your distribution uses to install it. 

Under Windows we have a very different story. GCC is a Linux compiler and you need some Linux utilities such as make to get it working. There are many different ways to get GCC running under Linux and everyone has their own favourite. Mine is  MinGW - it really is minimal and doesn't make a mess of your Windows installation. However, it is important that you get the right version.

Go to:

http://www.mingw.org/

and download a file called mingw-get-setup.exe or similar.

This is the graphical installer and when it is downloaded you should run it.

 

mingw

 

Select Basic Setup to install mingw32-base which contains the GCC compilers and msys-base which contains make.

When the installer completes the installation you can move on to trying out NetBeans.

A First Program

Now that you we have a machine with GCC installed plus the necessary tools that NetBeans uses we can try to create a first program to make sure everything is working. 

Load NetBeans and select File, New Project. Select a C/C++ Application in the dialog box that appears:

dialog1

 

Move on to the next step by clicking Next. The dialog box that appears allows you to name the project - HelloWorld is a good choice:

 

dialog2

 

Notice that we have selected C99 as the language we are going to use. More information on which version of C to select in the next part. 

 

The important entry in this dialog is the Tool Collection. This should show that a Tool Collection is in use - in this case MinGW under Windows. If there are multiple Tool Collections select the appropriate one - GCC for the machine you are targeting. 

Finally if you now click Finish a project will be created. 



Last Updated ( Tuesday, 11 September 2018 )