Nothing Special   »   [go: up one dir, main page]

Unit 2 - Introduction To C Language Processing System

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

UNIT -2 : INTRODUCTION to C Language

Computers are a balanced mix of software and hardware. Hardware is just a piece of
mechanical device and its functions are being controlled by a compatible software. Hardware
understands instructions in the form of electronic charge, which is the counterpart of binary
language in software programming. Binary language has only two alphabets, 0 and 1. To
instruct, the hardware codes must be written in binary format, which is simply a series of 1s
and 0s. It would be a difficult and cumbersome task for computer programmers to write such
codes, which is why we have compilers to write such codes.

COMPONENTS OF C Language Processing Systems

We have learnt that any computer system is made of hardware and software. The hardware
understands a language, which humans cannot understand. So we write programs in high-
level language, which is easier for us to understand and remember. These programs are
then fed into a series of tools and OS components to get the desired code that can be used
by the machine. This is known as Language Processing System.

The high-level language is converted into binary language in various phases. A compiler is
a program that converts high-level language to assembly language. Similarly,
an assembler is a program that converts the assembly language to machine-level
language.
Let us first understand how a program, using C compiler, is executed on a host machine.
 User writes a program in C language (high-level language).
 The C compiler, compiles the program and translates it to assembly program (low-
level language).
 An assembler then translates the assembly program into machine code (object).
 A linker tool is used to link all the parts of the program together for execution
(executable machine code).
 A loader loads all of them into memory and then the program is executed.
Before diving straight into the concepts of compilers, we should understand a few other
tools that work closely with compilers.
Preprocessor

A preprocessor, generally considered as a part of compiler, is a tool that produces input for
compilers. It deals with macro-processing, augmentation, file inclusion, language extension,
etc.

Interpreter

An interpreter, like a compiler, translates high-level language into low-level machine


language. The difference lies in the way they read the source code or input. A compiler
reads the whole source code at once, creates tokens, checks semantics, generates
intermediate code, executes the whole program and may involve many passes. In contrast,
an interpreter reads a statement from the input, converts it to an intermediate code,
executes it, then takes the next statement in sequence. If an error occurs, an interpreter
stops execution and reports it. whereas a compiler reads the whole program even if it
encounters several errors.

Assembler

An assembler translates assembly language programs into machine code.The output of an


assembler is called an object file, which contains a combination of machine instructions as
well as the data required to place these instructions in memory.

Linker

Linker is a computer program that links and merges various object files together in order to
make an executable file. All these files might have been compiled by separate assemblers.
The major task of a linker is to search and locate referenced module/routines in a program
and to determine the memory location where these codes will be loaded, making the
program instruction to have absolute references.

Loader

Loader is a part of operating system and is responsible for loading executable files into
memory and execute them. It calculates the size of a program (instructions and data) and
creates memory space for it. It initializes various registers to initiate execution.

INTRODUCTION to C Programming

The C programming language is a general purpose high level language that was originally
developed by Dennis M. Ritchie to develop the Unix operating system at Bell Labs. C was
originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan
and Dennis Ritchie produced the first publicly available description of C, now known as the
K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX
applications programs have been written in C.

The C has now become a widely used professional language for various reasons.

 Easy to learn
 Structured language
 It produces efficient programs.
 It can handle low-level activities.
 It can be compiled on a variety of computer platforms.

Facts about C

 C was invented to write an operating system called UNIX.


 C is a successor of B language which was introduced around 1970
 The language was formalized in 1988 by the American National Standard Institute.
(ANSI).
 The UNIX OS was totally written in C By 1973.
 Today C is the most widely used and popular System Programming Language.
 Most of the state of the art software’s have been implemented using C.
 Today's most popular Linux OS and RBDMS MySQL have been written in C.

Why to use C ?

C was initially used for system development work, in particular the programs that
make-up the operating system. C was adopted as a system development language
because it produces code that runs nearly as fast as code written in assembly
language. Some examples of the use of C might be:

 Operating Systems
 Language Compilers
 Assemblers
 Text Editors
 Print Spoolers
 Network Drivers
 Modern Programs
 Data Bases
 Language Interpreters
 Utilities

C Programs

A C program can vary from 3 lines to millions of lines and it should be written
into one or more text files with extension ".c" for example hello.c. You can
use "Dev++", "Visual Studio", "Turbo C", "vi", "vim" or any other text editor to
write your C program into a file.

C Program Structure

Before we study basic building blocks of the C programming language, let us look a bare
minimum C program structure so that we can take it as a reference later.

A C program basically consists of the following parts:

 Preprocessor Commands
 Functions
 Variables
 Statements & Expressions
 Comments

Let us look at a simple code that would print the words "Hello World":
#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}

Let us look various parts of the above program:

1. The first line of the program #include <stdio.h> is a preprocessor command which tells a
C compiler to include stdio.h file before going to actual compilation.

2. The next line int main() is the main function where program execution begins.

3. The next line /*...*/ will be ignored by the compiler and it has been put to add additional
comments in the program. So such lines are called comments in the program.

4. The next line printf(...) is another function available in C which causes the message
"Hello, World!" to be displayed on the screen.

5. The next line return 0; terminates main() function and returns the value 0.

You might also like