Introduction to C Programming
UNIT 2
Overview and History of C
● C language was developed by Dennis Ritchie in 1972 at Bell Laboratories.
● C was invented to write an operating system called UNIX.
● C is a successor of B language which was introduced around the early 1970s.
● The language was formalized in 1989 by the American National Standard Institute (ANSI).
● The UNIX OS was totally written in C.
● Today C is the most widely used and popular System Programming Language.
● Most of the state-of-the-art software have been implemented using C.
● Today's most popular Linux OS and RDBMS MySQL have been written in C.
● In 1990, a version of the C language was approved by the International Standard Organisation(ISO),
and that version of C is also referred to as C89.
Features of the C language
C is one of the most popular programming languages. It has many features:
● Modularity: It allows the reusability of modules. It has header files with readymade functions that
you can use.
● Middle-Level language: It combines both the advantages of low-level and high-level languages. (C
arrays, C Pointers, etc.)
● General-purpose programming language: It can be used to implement any kind of application, just
like Python.
● Portability: C program can be compiled or executed in any operating system (Unix, DOS, Windows).
You can compile the program in Windows and then run it on a Unix machine, it will run without any
issue.
● Powerful programming language: It is a very efficient and powerful programming language. It is
best used for data structures and designing system software.
● C is a case-sensitive language.
Structure of a C Program
The structure of a C program can be mainly divided into six parts, each having its purpose. It makes
the program easy to read, easy to modify, easy to document, and makes it consistent in format.
Section Description
Consists of the description of the program, programmer's name, and creation date.
Documentation
These are generally written in the form of comments.
All header files are included in this section which contains different functions from
Link the libraries. A copy of these header files is inserted into your code before
compilation.
Includes preprocessor directive, which contains symbolic constants. E.g.: #define
Definition allows us to use constants in our code. It replaces all the constants with its value
in the code.
Structure of a C Program
Section Description
Includes declaration of global variables, function declarations, static global
Global Declaration
variables, and functions.
For every C program, the execution starts from the main() function. It is
Main() Function
mandatory to include a main() function in every C program.
Includes all user-defined functions (functions the user provides). They can
Subprograms contain the inbuilt functions and the function definitions declared in the Global
Declaration section. These are called in the main() function.
Example
/** //Documentation int main(void) //Main() Function
* file: age.c {
int current = 2021;
* author: you
printf("Age: %d", age(current));
* description: program to find our age. return 0;
*/ }
#include <stdio.h> //Link
int age(int current) { //Subprograms
#define BORN 2000 //Definition return current - BORN;
}
int age(int current); //Global Declaration
Creating and Executing a C Program
To create and execute C programs in the Windows Operating System, we need to install Turbo C
software. We use the following steps to create and execute C programs in Windows OS…
Step 1: Creating a Source Code
Source code is a file with C programming instructions in a high-level language. To create source code, we
use any text editor to write the program instructions. The instructions written in the source code must
follow the C programming language rules.
The following steps are used to create a source code file in Windows OS…
● Click on the Start button
● Select Run
● Type cmd and press Enter
● Type cd c:\TC\bin in the command prompt and press Enter
● Type TC press Enter
● Click on File -> New in C Editor window
● Type the program
● Save it as FileName.c (Use shortcut key F2 to save)
Step 2: Compile Source Code (Alt + F9)
● The compilation is the process of converting high-level language instructions into low-level
language instructions. We use the shortcut key Alt + F9 to compile a C program in Turbo C.
● Whenever we press Alt + F9, the source file is going to be submitted to the Compiler.
● On receiving a source file, the compiler first checks for the Errors.
● If there are any Errors then compiler returns List of Errors, if there are no errors then the source
code is converted into object code and stores it as a file with .obj extension.
● Then the object code is given to the Linker. The Linker combines both the object code and
specified header file code and generates an Executable file with a .exe extension.
Step 3: Executing / Running Executable File (Ctrl + F9)
● After completing compilation successfully, an executable file is created with a .exe extension.
● The processor can understand this .exe file content so that it can perform the task specified in the
source file.
● We use a shortcut key Ctrl + F9 to run a C program.
● Whenever we press Ctrl + F9, the .exe file is submitted to the CPU.
● On receiving .exe file, CPU performs the task according to the instruction written in the file.
● The result generated from the execution is placed in a window called User Screen.
Step 4: Check Result (Alt + F5)
● After running the program, the result is placed into User Screen.
● Just we need to open the User Screen to check the result of the program execution.
● We use the shortcut key Alt + F5 to open the User Screen and check the result.
Execution Process of a C Program
● When we execute a C program it undergoes with the following process…
Compilation process in c
What is a compilation?
● The compilation is a process of converting the source code into object code.
● It is done with the help of the compiler.
● The compiler checks the source code for the syntactical or structural errors, and if the source code
is error-free, then it generates the object code.
Compilation process in c
● The c compilation process converts the source code taken as input into the object code or
machine code.
● The compilation process can be divided into four steps, i.e., Pre-processing, Compiling,
Assembling, and Linking.
● The preprocessor takes the source code as an input, and it removes all the comments from the
source code. The preprocessor takes the preprocessor directive and interprets it. For example, if
<stdio.h>, the directive is available in the program, then the preprocessor interprets the directive
and replace this directive with the content of the 'stdio.h' file.
Compilation process in c
The following are the phases through which our program passes before being transformed into an
executable form:
● Preprocessor
● Compiler
● Assembler
● Linker
Preprocessor
The source code is the code which is written in a text editor and the source code file is given an
extension ".c".
This source code is first passed to the preprocessor, and then the preprocessor expands this code. After
expanding the code, the expanded code is passed to the compiler.
Compiler
The code which is expanded by the preprocessor is passed to the compiler.
The compiler converts this code into assembly code. Or we can say that the C compiler converts the pre-
processed code into assembly code.
Assembler
The assembly code is converted into object code by using an assembler.
The name of the object file generated by the assembler is the same as the source file.
The extension of the object file in DOS is '.obj,' and in UNIX, the extension is 'o'.
If the name of the source file is 'hello.c', then the name of the object file would be 'hello.obj'.
Linker
Mainly, all the programs written in C use library functions. These library functions are pre-compiled, and
the object code of these library files is stored with '.lib' (or '.a') extension. The main working of the linker
is to combine the object code of library files with the object code of our program.
Example
hello.c
#include <stdio.h>
int main()
{
printf("Hello world!");
return 0;
}