C – Environment Setup using GCC

      Prev                                                                                              Next

      In this chapter, we are going to learn how to setup C development environment using GCC compiler in your machine and how to compile and execute a C program by your own.

 

Note:

  • GCC (GNU Compiler Collection) is a compiler used to compile both C and C++ programs.
  • GCC is just a compiler. It is not an IDE ( Integrated Development Environment ) such as Turbo C++, Borland C++ and Dev C++
  • We have to use window’s command prompt to compile and execute C and C++ programs.

Steps to install GCC compiler:

1. Go to http://sourceforge.net/projects/mingw/files/

2. Download the latest version (Check the line “Looking for the latest version? “Download mingw-get-setup.exe (86.5 kB)”) and click on this link.

3. Once download is completed, double click on exe file. Shortcut may be available in your desktop.

4. Click on “All Package”, and then choose the C compiler as shown below. Please choose all the GNU C Compiler listed in below window. Then, Click on Installation ->Apply Changes.

Step4.1

Step4.2

        • The below step is for C++ Compiler installation. If you want to install C++ compiler, you can follow this step. Else, you can move to step5.

Step4.4

Step4.5

5. Once installation is completed, go to your desktop and right click on My Computer -> properties -> advanced system settings -> advanced tab. Then, click on “Environment Variables” button and then “New”. You will get a popup window as shown below.

        • Change the System variable as given below.

Variable name: PATH
Variable value: C:MinGWbin;

Step5

6. Restart you machine.

7. Open command prompt (Type “cmd” in search box).

8. Type “gcc –version” in command prompt to check whether C compiler is installed in your machine. Type “g++ –version” in command prompt to check whether C++ compiler is installed in your machine. But, we are good if C compiler is installed successfully in our machine as of now.

Step8

9. Open notepad or text document and type any simple program and save this file as test.c (You can put any name for file. But, make sure that it is saved with .c extension).

Step9

Some basic commands you need to know before compiling and executing C program using command prompt:

        • To go to particular directory, use this command – cd {full_path} (for example : cd c:MinGWbin)
        • To list all the files in a directory, use this command – dir { full_path_of_directory} (for example : dir c:MinGWbin)
        • To rename a file, use this command – ren {old_file_name} {new_file_name} (for example : ren test.c.txt test.c)
        • To compile a C program, use “gcc file_name.c” (for example : gcc test.c)
        • To execute C program, use “a.exe” in command prompt
        • To clear the sceen, use “cls” command

10. Go to the respective direcory in command prompt where you have saved your C program file and compile your C program using “gcc file_name.c” command.

Note:

        • Sometimes, when you type C program in notepad or text editor and save the file as “file_name.c”, it would be saved as “file_name.c.txt”.
        • This file can’t be compiled by compiler.
        • When you see this file in windows folder, which will look good as file_name.c.
        • But, when you list all available files in the directory where you have placed your program file using “dir { full_path_of_directory}” command in command prompt, you can able to see file name will be as “file_name.c.txt”
        • So, you have to rename this file in command prompt using “ren {old_file_name} {new_file_name}” command, and then compile the program.

11. Run your C program using “a.exe” command in command prompt. You can see the output as below.

Step1011

      Prev                                                                                              Next

 .