To create and run programs you need two things:
- Text editor
A text editor is where you write your programs. A compiler translates your program into a binary stream of 1s and 0s which computer understands. So whenever you compile a program, the compiler creates a new binary file called executable (having .exe extension in Windows) which computer can execute.
Steps you need to follow while writing C programs.
- Create a program
- Compile program
- Run program
You can create your programs using your default text editor like Notepad in Windows or vim in Linux, but to make things easy we will install a cross-platform IDE(Integrated Development Environment) called Code Blocks.
Why use an IDE?
IDE allows you to create, compile and run programs from a single environment. Code Blocks comes with a compiler called GCC to compile C and C++ programs. If you are not using IDE then you need to create the program using your text editor. Then to compile the program you need to open command prompt or terminal and type some commands. We will first learn how to write a simple program using an IDE, then I will show you how you can create programs using your text editor and compile it using GCC compiler.
Installing Code Blocks in Windows
Step 1: Go to http://www.codeblocks.org/downloads/26 and scroll down a little under Section “Windows XP / Vista / 7 / 8.x / 10:” select “codeblocks-16.01mingw-nosetup.zip”.
It is crucial for you to select MinGW setup because this will install the GCC compiler on your system.
Step 2: After downloading double click on the setup to run it and you will be presented with the following window. Click next to continue.
Step 3: Accept the license agreement and click next.
Step 4: In the next window, Code Blocks will ask you to select components you want to install. Select Full Installation, it will look something like this:
Make sure “MinGW Compiler Suite” is selected because this option will install GCC on your system.
Step 5: Select the destination folder (keep it to default) and click next.
Step 6: The installer will proceed to complete the installation.
Step 7: After completing the installation, Code Blocks will prompt you to run it.
Click no and then click on the Next button.
Click Finish to exit the installer.This completes the installation of Code Blocks on Windows.
Installing Code Blocks in Linux
Code Blocks can run or various Linux distributions. For example Ubuntu, Fedora, CentOS etc. In this section, we will learn how to install Code Blocks on Ubuntu 16.04.1.
Open terminal and type the following command.
$: sudo add-apt-repository ppa:damien-moore/codeblocks-stable
After entering this command, you will be asked to press enter. Hit enter and enter the next command:
$: sudo apt-get update
This command will update all the packages on the system. Finally, enter the following command:
$: sudo apt-get install codeblocks codeblocks-contrib
This command will download and install code blocks on your system.
Note: The whole process may take some time if you are on a slow connection. So, please be patient.
The First Run
On Windows go to Start > All Programs > Code Blocks to launch Code Blocks. Alternatively, double-click on the shortcut icon of Code Blocks on the desktop.
On Ubuntu, you can open Code Blocks from the terminal by issuing the following command.
Alternatively, click on the top of the launcher and type some keyword as shown in the screenshot below:
Code Block will now ask you to select default Compiler. Code Block supports several compilers from different vendors. If your computer has some other compiler in addition to GCC, Code Blocks will detect all of them and will show you a list in the window. This computer does not have any compiler except GCC, that’s why only GCC is detected. Select The GCC compiler and click on “Set as default” as shown in the screenshot below:
Click OK to continue. Next, you will be shown a Code Blocks splash screen. After loading completes you will be presented with the following window.
Note: If this is the first time you are running Code Blocks, you may get a window asking you to select Code Blocks as a default application to handle C/C++ source files.
Select “No, leave everything as it is” and click OK.
Menu bar and Toolbar: Like many other GUI programs Code Blocks has the menu bar. The toolbar provides all commonly used commands like Build, Run, Save, Find etc.
Start Page and Editor: Start Page is the default page when Code blocks is launched and editor is where we will write code.
Management Window: This window shows all source files available in your project.
Log Window: This is where you will get all sorts of logs like build messages, compilation errors, warning etc.