C is a compiled language that requires another program to convert the source code to object code which is understood by the computer. This program is known as compiler.
So to compile a C program, we need a compiler. A few popular compilers are: GCC, Turbo C/C++.
To set up a C language compiler, there are three ways:
- To download a full fledge IDE(Integrated Development kit) like Turbo C or Microsoft visual C++, which comes along with a C language compiler.
- Use any text editor to write a program and download the C compiler separately.
- Use some online IDEs like http://www.onlinegdb.com or http://www.codechef.com.
In this we will be looking at the second and third options to compile a C program.
Without an IDE:
- First, a C compiler called GCC needs to be downloaded from the gcc website https://gcc.gnu.org/install
- Once it is downloaded and installed, a program can be written on any text editor and saved with the extension .c like hello_world.com.
- Open a command prompt or a terminal(for Linux or MAC OS) and go to the directory where the program hello_world is saved.
- Type in the command gcc hello_world.c to compile the code. This will compile the code and result in an executable with the name a.out(default name), if there are no errors.
- Now, to run the program, type in ./a.out, and you will see Hello World displayed on the screen.
Using online IDE:
This is the easiest way to compile and run a C program.
Step 1: Visit the website
Visit the link www.onlinegdb.com and select the programming language as C.
Step 2: Write your program
By default, a hello world program is already written as a skeleton for all C programs. This can be modified or extended to add to your logic.
Step 3: Run the program
In this on-click run, it compiles and runs. If there are no errors a Hello World is written on the below output screen.
Step 4: Save the program
Create a login and save the program with the appropriate name for future reference
Difference between Compiling a program and running a program:
There are two steps first is to compile a program that is look for any syntactical errors in the code and if not produce the machine code which can be understood by the system. This involves use of another program called a compiler to do so.
The second steps is to run the executable (machine code) that is to execute the statement inside the main.
Diag-1: C program Execution steps
- What is Programming Language?
- Introduction to C language
- Features of C language
- The phase of C program Compilation
- Compiler, Interpreter and Assembler
Categories: C Language
Leave a Reply