Table of Contents
< All Topics
Print

5. Compile and Execute C Program

C is a powerful and widely used programming language that can run on different platforms. To compile and execute a C program, you need a C compiler and an editor. A compiler is a software that converts the source code written in C into an executable file that can be run by the computer. An editor is a software that allows you to write and edit the source code. There are many compilers and editors available for C, such as GCC, Visual Studio, Dev C++, CodeBlocks, etc.

Compile C Program

Source File

This file contains the source code of the program. The file extension of any c file is .c. The file contains C source code that defines the main function & maybe other functions.

Header File

A header file is a file with extension .h which contains the C function declarations and macro definitions and to be shared between several source files.

Assembly File

Assembly code is made by the compiler with an extension *.s, this is further being used to create a *.obj or *.o object file.

Object File

An object file is a file containing object code, with an extension .obj or .o, meaning relocatable format machine code that is usually not directly executable. Object files are produced by an assembler, compiler, or other language translator, and used as input to the linker, which in turn typically generates an executable or library by combining parts of object files.

Executable File

The binary executable file is generated by the linker. The linker links the various object files to produce a binary file that can be directly executed.

Compilation and Execution steps

Source code is the C program that you write in your editor and save with a ‘ .C ‘ extension. Which is un-compiled (when written for the first time or whenever a change is made in it and saved).

Object code is the output of a compiler after it processes the source code. The object code is usually a machine code, also called a machine language, which can be understood directly by a specific type of CPU (central processing unit), such as x86 (i.e., Intel-compatible) or PowerPC. However, some compilers are designed to convert source code into an assembly language or some other another programming language.

An assembly language is a human-readable notation using the mnemonics (mnemonics a symbolic name for a single executable machine language instruction called an opcode) in the ISA (Instruction Set Architecture) of that particular CPU.

Executable (also called the Binary) is the output of a linker after it processes the object code. A machine code file can be immediately executable (i.e., runnable as a program), or it might require linking with other object code files (e.g. libraries) to produce a complete executable program.

Compile and Execute the C Program (in Dev C++ IDE)

Let us see how to save the source code in a file, and how to compile and run it using the IDE Dev-C++. Following are the simple steps −


#include<stdio.h> // header file for input/output functions

int main() 
{
    printf("\n Hello, Bharat!"); // print the message using the printf function
    return 0; /* return 0 to indicate successful execution */
}
  • Open a text editor and add the above-mentioned code.
  • Save the file as hello.c
  • Go to execute menu then select compile (F9) option.
  • Once there is no error in program, go to execute menu then select Execute (F10) option.
  • On output window of DevC++ you will find the output as “hello Bharat”.
  1. C Program Structure
  2. Control Flow-based C Programs
  3. Errors in C Programs

Congratulations! You have successfully compiled and executed a C program!

Happy Learning!!

Leave a comment