A ‘C Program‘ can be written and compiled using many IDE applications, but we are going to use Code-blocks, which is a free, cross platform and beginner friendly IDE. Here I am using Code-blocks 20.03 with GCC compiler to demonstrate how to write and compile C program using Code-blocks IDE.
Get started to write your first C program using Code-blocks IDE
Start Code-blocks by using a short cut on desktop or searching in applications list.
SECTION 1 – Creating a folder and C Program file
Step 1.
Go to File > New > click on File , see in below screenshot.
Step 2.
A new window will pop up, select ‘C/C++ source’ in Category section and then click on Go button.
Step 3.
A new window will pop up, check the tick box and then click on Next button.
Step 4.
A new window will pop up, click on ‘C’ in selection window, and then click on Next button.
Step 5.
In this pop window,click on the three dots button to create a folder in any of your Windows system's drive to save your C Program files.
Step 6.
Now here in this pop window, first select the drive where you want to make a new folder to save your C Programs, after that create a folder in the selected drive and give a name to it. And then select the newly created folder and click on open button.
Give a name to your folder as per your choice. Use this folder to save your C Programs. D drive is preferred location, because many times if the system OS gets corrupt,then you might need to format C drive and reinstall your OS.
In that case your data on D drive remains intact. Also some antivirus programs also don’t allow to run the executable program output files from C Drive
Step 7.
Now you are in the folder that you have just created, give a filename to your C Program file in the ‘File name’ field with ‘.c’ extension e.g. ‘helloworld.c’ and then click on Save button.
Step 8.
First section of creating folder and C Program file is completed here. Now you can use this folder to save as many program files as you want. Just need to select this folder and create another program file. Click on Finish button.
SECTION 2 – Write, Compile and Run a C Program
Step 1.
Now we are inside the Code-blocks IDE with an empty file where we can write a C Program. Just write any C Program here. I have wrote a C Program to display ‘Hello World’ string on the screen. After you finish writing your C Program, first you have to save the program and then click on the Green triangled button to compile and run the C Program. See in below screenshot.
After writing the program you can save the program file in three ways
1) by pressing ‘Ctrl+S’ keys from keyboard, or
2) by clicking on the third icon on ribbon (floppy drive icon), or
3) Go to File > then click on Save file.
Step 2.
Now you will see a new pop up window, click on yes button to build the C Program.
In this process first the C Program will be compiled, if it is having any syntax errors, those will be shown in error log window at the bottom of Code-blocks IDE.
You have to rectify those errors and save the file and do this step again until you get zero errors. When your program will be with zero errors, it will be compiled and an executable C Program output file will be created. Code-blocks will run C Program executable file automatically in this process. Then you will see C Program output in command window.
This is the simple and fast method to compile and run a C Program. But you can check different options by going in the 'Build' menu to complile and run a C program step by step, exlplore it.
Step 3.
In below screenshot, you see the C Program output in command window. Here you see your C Program (source code), your C Program Output and the Error Log window.
Now you will see three files created in your C Program folder, first is your C Program Souce file (.c), second is your C Program Object file (.obj) and third is your C Program executable file (.exe), check them.
Step 4.
After you have done using Code-blocks, close the application by going to Files and then click on ‘Quit’ or you can use keyboard shortcut ‘Ctrl+Q’.
Conclusion
In this way we have seen in this tutorial all the steps of how to write C Program using Code-blocks IDE. First we created a folder on D drive to save C programs and then created an empty C program file.
After that we wrote a simple C Program to display "Hello World" string on display. Then we compiled that C Program using in Code-blocks and seen the program output.
Comments
Post a Comment