You read why C is so important and now you want to make your first C program, but you would like a simple and complete explanation of the whole process.

Let’s get started!

First of all, we will see:

Install a development environment

What is a development environment?

Here’s the short answer: it’s the set of tools used to make a program. We will use an IDE (Integrated Development Environment) that have all these tools in one software.

Here’s the long answer: What is Development Environment? – Definition from Techopedia

To make things even simpler, an IDE has a text editor, a compiler, a list of all our programs and other functions that will help us a lot to write our codes.

Of course each operative system has different IDEs and installation processes, so let’s see the three main ones: Windows, Linuxand MacOS.

Windows

On Windows we are going to install CodeBlocks.

First, download it here. Then open it and on the installation pop-up click Next until the button Finish appears. Once you’ve clicked on it, CodeBlocks will be successfully installed.

Now open CodeBlocks (there should be a shortcut on the desktop otherwise search it in the Windows menu) and choose the first compiler (GNU GCC compiler) from the dialog box that will appear.

If, for some reason, you can’t find any compiler, you can download one here.

Select a template

 

To create a new file, click on File -> New -> File… and choose C/C++ source as template.

Select C

 

Click Go and a new pop-up will appear (so many pop-ups). Click on Next, select C, Next, “” near filename with full path (choose where you want to save the file and give it a name), Next and you’ve finished.

Now you’re ready to program!

Linux

We will install CodeBlocks on Linux too.

As a Linux user you should know that to install basically everything you can either use the command line or .rpm and .deb packages.

So, if you have Debian, Fedora or Centos, you can download the installing package here.

If you have Ubuntu check this.

Otherwise here you can find a tutorial for any other distro.

MacOS

On Mac we are going to install Apple’s XCode.

You can download it from the Mac App Store at https://itunes.apple.com/it/app/xcode/id497799835?mt=12

 

 

Preview of your first C program

Make your first C program

 


This is the program we’re gonna analyse. Pretty simple, isn’t it?

I mean: for you now it’s just a bunch of words and punctuation without any sense, but it’ll become clear in a second.

First things first, why have I choose this code?

  1. I wanted to do some input/processing/output stuff, but than it would have become too difficult to explain: as I said in previous posts, never do too many things all together
  2. This is the usual and boring Hello World! program, where we just make the computer print (=write on the command prompt) a sentence.

Vocabulary

Click on the red words to see the vocabulary

Function: it is a named piece of code that performs a task. printf is a function.

Library: it is an external file that contains a collection of functions. stdio.h is a library.

To return: to give a result. It’s the output of a function.

To execute a program: to run a program.

int: it’s the type of a value. It means “this value/the output this function returns is an integer”

Parameter: a function’s input.

\n: a special character that stands for “new line“.

to declare a function: to create a function where you can write your own code.

to call a function: to use a function already created.

namespace: it’s a set of libraries and programs.

Let’s analyse the C code

Before we start, remember: the empty lines are not necessary, they are just used to make the code more readable.

The first line is #include  and it means “this program uses the library called stdio.h“.

stdio.h contains the main input/output functions of C.

Let’s have a look at int main() {:

  • main is the main function of the program. It tells the computer where to start executing (=running) the code.
  • int stands for integer (=a number without fractional component, like 1, 2, 3… but not 1.5, 2.5…) and it is the type of the value that the function main returns.
    When the computer finishes executing a function it is possible to see its result: for instance, a function that multiply two numbers will give use (we say it returns) the result of the multiplication.
    main returns an integer (int) that tell us if there were errors: you’ll see that value at the end of the execution on the command prompt.
  • Inside the two brackets () we put the parameters (=the inputs) of a function. In this case main doesn’t require any parameters, so we leave it empty.
  • The last bracket { means “the function’s code starts here“. Everything we will put inside the two {} of the function main will become the core code of our program.
    Remember that can be other curly brackets inside the function main, but for every opening bracket there must be a closing one.

Now, let’s examine printf("Next time we'll do something better!\n");

  • printf is the main C function for command line outputs. It works like this: printf(what you want to write between quotation marks “…”, optional things that we will see next time);
    Since we aren’t writing the code for printf, but we are using an already written one, we don’t need curly brackets. This process is called “to call a function
  • n is a special character (they are two, I know, but for the computer is just one) and it means new line.
  • The semicolon you see at the end of the line means “line finished” or “this is the end of this instruction“. You must put a semicolon a the end of each line/instruction/called function, apart form #include, curly brackets and others we’ll see.

If you read this explanation carefully, you already know what that last } means: for every opening bracket there must be a closing one.

Let’s translate the code

In human language, this code can be translated in:

 

Computer, this program uses functions from the library called stdio.h

This is the main part of the program: start from here

Write on the command prompt the sentence “Next time we’ll do something better!” and the go to a new line.

This is the end of the program.

 

Run the code

Before running the code, you must first compile (or Build) it to make it understandable by the computer (remember that it can read only binary code).

Here’s an image that shows how to run the program:

Press F9 to run and compile your code

Every time you change it, click on “Build and Run” instead.

C and C++ comparison

In the table at the top, you can see two columns: one for C and the other for C++.

What are the differences between the two?

  1. The main input/output library of C++ is iostream, not stdio.h
  2. There is a new line using namespace std; that tells the computer that the program is using the standard (std) namespace (set of libraries).
    You can also don’t use this line of code, but then you’ll have to explicitly tell the computer whenever you call a function from the Standard Library.
  3. cout (or std::cout if you decided to not use the std namespace) is the C++ alternative to printf.
    It has a different syntax because it isn’t a function, but you can add other sentences to the first. Each time you want to do it just type >, I usually imagine cout as the command prompt and the double less-than sign as an arrow that move the content from one side to another.
    In fact when we’ll learn to use the input cin, we will use the double greater-than sign >>.

Nevertheless, you can use the same C code in C++.

Conclusion

Now that I’ve explained how each line works, this program is quite simple, isn’t it?

Why don’t you try to create your own, make the computer write some joke or more sentences.

As I always say, if there’s something you haven’t understood write me a comment and I will be happy to help you.

Otherwise, if you have any friends who would like to start programming with you, share this guide (you can also use the sharing buttons below).

Anyway, the post is finished, have a good day and we will see the next week!

From Zephyro it’s all, Bye!


Leave a Reply

%d bloggers like this: