Structure of C language

Before reading this blog, I recommend you to read about the introduction of programming in c so you will have a brief idea about programming in C.

C program is a collection of one or more functions. Every function is a collection of statements and performs specific tasks. The general structure of the C program is;
Preprocessor directives
Global variables
main () function
local variable
funcl ()
local variables
funcl ()
local variables
  1. A c program consists of header files and library functions.
  2. It needs at least one function and the name must be the main().
  3. Before using any function and variable that must be declared first.

Compiling Process

There are different phases through which our program passes before being transformed into an executable form. The following figure shows the phases.
  • Preprocessor: The source code is the code that we write using any text editor and the source code file is given an extension ‘.c’. This source code is first passed through the preprocessor. The preprocessor expands this code and passes it to the compiler.
  • Compiler: The extended code is passed to the compiler. The compiler converts this code into the machine’s assembly language code.
  • Assembler: The assembly language is converted to object code by the systems assembler. The name of the object file is the same as that of the source file. In DOS the object file has the extension ‘.obj’ and in UNIX the extension is ‘.o’.
  • Linker: Generally all programs written in C use library function. Library functions are precompiled and their object code is passed in library files with ‘.lib’ (or ‘.a’) extensions. The linker combines the object code of the library function with the object code of our program.

Development Stages of C Program

Typically, there are three development stages in C programming. They are:
  1. Creating or writing program code.
  2. Compiling
  3. Executing or Running

i. Creating or Writing program code

Creating is the process of writing the C source code. You can write C source code using any plain text editor program. There are many editor programs available for C. On the Windows platform, you can use Notepad and on the Linux platform, you can use Vi and Emacs programs. But now most people use compiler software to write C code. This software provides a complete programming environment for writing, managing, compiling, debugging, and testing C source code. This is also known as Integrated Development Environment (IDE).

ii. Compiling

AC source has to be translated into machine language code. The process of code translation is called compiling. During the compilation process, the compiler can detect any syntax error due to unrecognized program code as well as structural errors. The output of the compilation process is an object code that is saved in a file called an object file. In C, before a source code is compiled, there is a stage called pre-processing. During this stage, the C pre-processor performs directives which are usually including header or other C files to be compiled and text substitution.

iii. Execution or Running

The execution stage is where you run the program to check whether it produces the desired output or not. This stage can be a testing stage where you check the output of your program. If it does not produce the desired output, then you have made a logic (semantic) error in your source code. Unlike syntax error, the logic error won’t be detected during the compilation process. Therefore you have to find out the error’s location yourself.

C Compiler

Initially, the C code is created as plain text files and that is saved with a’.c’ extension. The C code can be written in any plain text editor program such as Notepad, Wordpad, etc. Or you can also directly type the program code on the C IDE (Integrated Development Environment). C IDE environment has its own text editor feature.
After typing C code it is a kind of software that translates the program code into byte code. There are many companies providing C compilers such as Borland, Microsoft, Mingw, etc. Borland has produced many C compilers such as Turbo C, Turbo C++, Borland C++, C++ Builder, etc. Microsoft has produced Quick C, Visual C++, etc. and Mingw has produced GNU C compiler (GCC for DOS).
Some of the C compilers are listed below:
  • GNU C Compiler (GCC)
  • Borland C Compiler (BCC)
  • Turbo C compiler (TCC)
  • Sun C Compiler (SCC)
  • Turbo C++ Compiler (TCC)
Among them GNU C Compiler (GCC, Turbo C, and Turbo C++ compiler are the most popular C compilers. They are available free under the terms of the general public license (GPL).

Wiring a C Program

In any programming language, there are some rules for writing code. C also has its own rules for writing code. Some general rules for C are as follows:
  • Generally, C statements are written in small letters (lower case letters).
  • There must be a semicolon (;) at the end of each C statement. (any line of code that is terminated by a semicolon is called a statement).
  • The execution of the C program must start from the main () function so there must be at least one main function.
  • In the program, a set of statements must be in between the start ‘{‘ and end ‘}’ brackets.

C program layout

Basically, a C program consists of two sections:
  1. Pre-processor directives
  2. Main function
The pre-processor section is the place where we specify what the compiler should do before compiling the source code. Usually, we should specify the included header file which will be used in our program. A header file keeps all the information about functions available in the standard C library. All per-processor directives begin with the # symbol. The second section begins with the function main() definition. The function main() is where the program starts its execution. Thus function main() must be present in any C program.
The general format of the C program is as follows:
#include <header file>
int main ()
set of statements;

A Sample C program

Let’s write a simple C program and discuss it.
int main()
printf(“Hello, Worldn”);
return 0;

Sample output:

Hello, World


When you run (execute) this program, it displays the output “Hello, World” in the output screen. It is a very simple C program but here is a lot of elements that are very new for you. Let’s discuss these elements one by one.

1. #include

At the first line of the program you can see the  $include
The #include

2. int main()

Below the preprocessor instruction, there is int main(). It is a function or sub-program. There may be a number of functions in a C program but there must be included at least one main() function. Execution of any C program starts from the main () function. It is not possible to execute any C program without a main() function.
The int before main () specifies the function declaration is the integer data type and its return type is an integer value. That means it can return an integer value.
The () parenthesis that follows the function name may contain a list of values to be used by that function is called parameters or arguments. If main (void) is given as a parameter which refers that it does not take any arguments.
The {} curly braces contain the program statements (function body) to be executed whenever the function is called. Each statement must be terminated by a semicolon (;). The ‘{‘ open curly brace represents the beginning of the function body and the ‘}’ close curly brace represents its termination.

3. printf()

printf() is a C library function that instructs the computer to display characters or a string enclosed by the quotation marks on the monitor screen.
To use any library function in the program that must be declared from the corresponding header file before its use. In this program, the printf function is declared in
Items enclosed by the parenthesis are called arguments.
Here the argument to printf() function is “Hello, World” and the n. “newline”. The n causes that text message to be displayed in a new line. It moves the print head to the left margin of the next line.

4. Return 0;

At the end of function main(), we have to include the return 0 statement because function main is defined to return a value of integer type. The return 0 statement returns a value from the main to the operating system. This statement also indicates that the program has terminated successfully.
Let’s type this program in Notepad. And ave it with hello.c filename.
hello world program in C
This is a sample program in text format or it is the source code of the program. Now it is ready to compile. It should be compiled into machine-readable byte format to make an executable file.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *