Sunday, 3 January 2016

C/C++ Installation

After reading the previous article, many people are curious to learn C and wants to know how to use it.

Without any other introduction, I am jumping off to set-up your system. There are multiple steps, you have to perform,

  1. Download and install C and verify the installation
  2. Write a short program
  3. Compile and run the program
  4. Check the output
So, these are the steps you need to follow.

Detailing each steps,

Step 1 - Download and install C and verify the installation

This step is system dependent. That is, it depends on the OS you are using. I will be using Windows 7 and readers who are using Windows 7, can follow the steps below for step 1. For others, I have tried to provide some link, you can check the following links,

For Mac OS

The same installation should work for Windows Vista/Windows 8 as well. If you are still using Windows XP, please upgrade your system to some latest OS like Windows 7 or Windows 8.

Installation

We'll use MinGW for our case. So, let's start with the installation.
  • Download the installer from Sourceforge.
  • Double click on the installer.
  • Installer opens up a window. Click Install.
  • It will ask for installation directory. By default this is set to C:\MinGW
  • You can change the directory. I recommend not to change the directory.
  • After the directory selection, click on Continue.
  • It will download required files.
  • Once this is completed, click Continue.
  • It will ask you for installing required packages.
  • Choose mingw32-base


  • If you want to install the compiler for C++ as well, you have to select the mingw32-gcc-g++ too
  • Then click on Installation panel and click Apply Changes
  • Click on Apply.
  • It will download the required files and installation gets completed.
  • Once the installation is complete, click Close.
  • Close any installation window open.
  • Now change the PATH Variable in Windows Advanced Settings to C:\MinGW\bin (Or the directory you have chosen at the time of installation). If you don't know how to change the PATH variable, check this article, Java Installation.
Phew !!!
A lot of stuffs to do. The good news is, you have completed the installation, let's verify it. 

Verification

Now open the command prompt from Start > All Programs > Accessories > Command Prompt

Issue the command
gcc

For C++ compiler, issue the command
g++

Which should be similar to the following,
D:\Palash\c\trunk\src>gcc
gcc: fatal error: no input files
compilation terminated.

D:\Palash\c\trunk\src>g++
g++: fatal error: no input files
compilation terminated.

D:\Palash\c\trunk\src>

Its OK, if you see an error. Do not panic. We'll get into that very soon.

With this our Installation is complete. Now we'll move to the next section.

Step 2 - Write a short program

At this point, you may have completed the installation. We will use a simple text editor like Notepad, Notepad++, Vi etc. We will not use any high level IDE for learning purpose, a simple text editor is good.

Now, open the editor and write the following program.
#include <stdio.h>
int main(){
 printf("Hello World");
 return 0;
}

Save the program in any location with the name first.c.

Step 3 - Compile and run the program

Compile the program

Open command prompt and move to the directory you have saved the file in Step 2.
In my case, I have saved the program in D:\Palash\c\trunk\src\blog

So, I have issued the following commands,
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Palash>D:

D:\>cd D:\Palash\c\trunk\src\blog

D:\Palash\c\trunk\src\blog>

Now, we'll compile the program using C compiler,
To do that, use the command,
gcc first.c -o first

If you have installed C++ compiler as well, you might want to verify the installation as well. To do that, use the command,
g++ first.c -o first

This command will compile the source code and will provide us with an executable output file with the name first. You may wonder how a C program is compiled using C++ compiler. You might read this article for details.

Now let's take a closer look on gcc command. We have issued, gcc command followed by the file name and -o and another String.
gcc is the command to compile a C program. The -o indicates the output file. If you don't provide this option, compiler will provide a default name.

  • In case of Unix and Mac OS, the default name is a.out
  • In case of Windows, this default is the a.exe
So, if you do not provide -o to the compiler, you will loose your previous output file each time compiler compiles a C Program. So, the best practice is to provide output file name to the compiler. Try following this from day 1.
For advanced readers, to check the options to the compiler you can check this list. If you are a beginner, you can safely skip this. We'll look deeper on this later.

Run the program

Your program is compiled, you must run the program to check the output. Simply issue the following command,
first

Step 4 - Check the output

You should see the following output in the console,

Hello World

With this, the installation and verification process completes.

Review

In this article, we have covered the following areas,
  1. We have downloaded and installed C compiler..
  2. We have written a C Program.
  3. We have checked how to compile a C Program.
  4. We have run the Program.
  5. We have verified the output in the console.
That's it for the day, we'll be checking more in our next articles.

Source Code Distribution

Although it is not a recommended practice, you can download the Source Code of this article. I have my repository hosted in github and published the source code which you can find on Palash90/C.

Prev   Next

1 comment:

  1. Thanks Palash - That was neat and helpful.

    Cheers, Kamal.

    ReplyDelete