Introduction To C Language
Introduction To C Language
Learning Objectives
1. To understand fundamentals of C language
2. To know structure of C program
3. To develop algorithm/flowcharts
4. To know applications of C language
C Programming Language
Electronic Science
1. Introduction to C language
1. Introduction
Programming in C is one of the most important papers for Electronic Science subject at
post graduate level. Every student planning to make career in Electronics Science
should have some kind of programming skills. One may ask a question like What are
the different computer languages an electronics student must learn for his
career? It is like asking a question How many cars a driver should practice to learn
driving?
1. Introduction to C language
Ada
Modula-2
Pascal
COBOL
FORTRAN
BASIC
Java
C++
C
FORTH
Macro-assembler
C Programming Language
Electronic Science
1. Introduction to C language
Assembler
The machine language and the assembly language is popularly known as Low level
language. The programmer must learn the instruction set of the particular computer
system to write a program in assembly language, and the program is not portable. The
program will not run on a different processor. This is because different processors have
different instruction sets. As these assembly language programs are written in terms of
seperate instruction sets, they are machine dependent.
For writing the program once independent of the processor, it was necessary to
standardize the syntax of a language. Higher-level language was introduced so that a
program could be written in the language to be machine independent. That is, a
program could run on any machine that supported the language with few or no changes.
To execute a higher-level language, a special computer program must be developed
that translates the statements of the higher-level language into a form that the computer
can understand. This program is known as a compiler. Table -1 indicates the position
of C in programming languages.
2.2 Operating System
Operating system is program that controls the entire operations of computer system.
Access to all the resource of the computer e.g. memory and input/output devices is
provided through the operating system. This program looks after the file management,
memory management and I/O management. It is a program that allows all other
program to use the computing capability of the computer and execute them. The most
popular operating systems are Windows, Linux, Unix, Mac OS X, MSDOS etc.
1. Introduction to C language
5
Start
Edit
Compiler and
assembler
Yes
Errors?
Source
Program
(file.c)
Object
Program
(file. obj)
No
Linker
Libraries and
other object
program
Execute
No
Executable
Program
(a.exe)
Result
OK?
Yes
Done
1. Introduction to C language
known as the source program. Every source program must have a valid filename e.g.
file.c. Once the source program is entered into a file then one can proceed to the
compilation,
To start the compilation process, the file containing the source C program must be
specified. Compiler examines each program statement present in the source programs
for correct syntax and semantics of the language. If there are any mistakes or errors
detected by the compiler then these are reported to the user and compila tion process is
terminated. The errors must be removed from the source program and then compilation
process may be reinitiated.
Once all the syntax error ( e.g. missing parentheses) or semantic error (e.g. variable not
defined) have been removed then compiler restarts it process. Compiler takes the
statements of the program and translates it into lower language. After the program has
been translated into lower level language like assembly language, the next stage is to
translate these assembly instructions into machine language. In many compilers, this
stage is automatically executed along with the compilation process. The result of this
compilation process is the generation of the object file from the given source file. This
object file has an extension .obj and contains the object code. The filename is just
same as source filename but with different extension name.
After the program is converted into object code, the linker automatically starts the
process. The purpose of linker phase is to link the present file with any previously
compiled file and systems library. At the end of linking the system generates a file with
the same source filename but with extension .exe. Once executable file is generated
then the program is ready to execute or run. Computer system execute each program
statement sequentially. If any external data is required then user must input it. Once the
processing is over the result is displayed as the o utput of the program.
If the desired results are obtained the complete process is over else go back to the
editor and check for the logical error. At this state, the built in debugger can help
programmer to remove the bugs from the program. In this case the entire processor of
editing, compiling, linking and executing the program. Usually, the process of editing,
C Programming Language
Electronic Science
1. Introduction to C language
3. Introduction to C language
Before learning how to write programs in C it would be important to know a brief history,
features, structure and applications of C language. In this section, the attempt is made
to present this information in short.
3.1 History of C Language
The origin of C is closed related with the development of UNIX operating system for
PDP-7 computers. UNIX operating system was originally written by Ritchie and
Thompson using assembly language. Even for PDP-11, the operating system was
developed using assembly language. Developers were planning to rewrite OS using the
B language using the Thompsons simplified version of BCPL (Basic Combined
Programming Language).
1. Introduction to C language
During the 1990s, the development of the C++ standard consumed most programmers'
attention. However, work on C continued quietly along, with a new standard for C being
developed. The end result was the 1999 standard for C, usually referred to as C99.
3.2 Features of C
C is a general-purpose high level language that was originally developed for the UNIX
operating system. The UNIX operating system and virtually all UNIX applications are
written in C language. C has now become a widely used professional language for
various reasons. Figure 2 indicates feature of c at a glance.
Modularity
Middle level
Case sensitive
Syntax based
Structure Oriented
Use of Pointer
C
features
Power full
Compiler Based
Portability
Simple
Platform
independent
It is a very simple and easy language. C language is mainly used for develop desktop
based application. All other programming languages were derived directly or indirectly
from C programming concepts. C language has following features;
C Programming Language
Electronic Science
1. Introduction to C language
1. Simple
Every c program can be written in simple English language so that it is very easy to
understand and developed by programmer.
2. Portability
C programs are portable. It is the concept of carrying the instruction from one system to
another system. In C language .C file contain source code, we can edit also this code. .exe file
contain application, only we can execute this file. When we write and compile any C program on
window operating system that program easily run on other window based system.
3. Powerful
C is a very powerful programming language, it have a wide verity of data types,
functions, control statements, decision making statements, etc.
4. Structure oriented
C is a Structure oriented programming language.Structure oriented programming
language aimed on clarity of program, reduce the complexity of code, using this
approach code is divided into sub-program/subroutines. These programming have rich
control structure
5. Case sensitive
It is a case sensitive programming language. In C programming 'break and BREAK'
both are different
6. Modularity
It is concept of designing an application in subprogram that is procedure oriented
approach. In c programming we can break our code in subprogram.
For example we can write a calculator programs in C language with divide our code in
subprograms.
7. Middle level language
C programming language can supports two level programming instructions with the
combination of low level and high level language that's why it is called middle level
programming language.
8. Syntax based language
C is a strongly tight syntax based programming language.
C Programming Language
Electronic Science
1. Introduction to C language
10
c a Middle-Level Language
C is often called a middle-level computer language. It does not mean that C is less
powerful, difficult to use, or less developed than a high-level language such as BASIC
or Pascal. Rather, C is thought of as a middle-level language because it combines the
best elements of high-level languages with the control and flexibility of assembly
language. As a middle-level language, C allows the manipulation of bits, bytes, and
addresses, the basic elements with which the computer functions. Despite this fact, C
code is also very portable. C language is very often known as a System Programming
Language, because it is used for writing assemblers, compilers, editors and even
operating systems,
C Programming Language
Electronic Science
1. Introduction to C language
11
3.3 Applications of C
C language is used in wide variety of applications. Practical applications of C language
are several, right form writing the operating systems like UNIX, Windows to creating
antivirus programs. Some application of C language is given below.
C Programming Language
Electronic Science
1. Introduction to C language
12
The C language program starts form main() function which is Function name. The
program function start with { (open brace) and ends with } (close brace ). Then valid c
statements are to be written between open brace and close brace .
The functions
written between open and close brace called as function body. Semicolon (;) is used
to end the valid statement.
Let us consider a very simple program; here programmer wants to display some text
message.
#include <stdio.h>
main ( )
//Execution begins
//Opening brace
printf(Fergusson College);
//Executable statement
//Closing brace
Output of program is
Fergusson College
3.5 Problem solving process
Any scientific and engineering problem can be solved using some Problem Solving
process. Here total six important steps are given to solve any general problem.
1. Understanding a problem
2. Identifying necessary inputs and expected output
3. Design an algorithm/flowchart
4. Writing or coding the program
5. Testing the code
6. Debugging
Everything begins with problem therefore it is necessary to understand the problem
clearly. With a given problem, the second step is to know necessary inputs and
C Programming Language
Electronic Science
1. Introduction to C language
13
expected outputs. By knowing the input and output parameter the next step is to design
an algorithm to solve the problem or one prefer to draw the design in a graphical way
using flowchart. Once the algorithm or flow chart is ready, the obvious step is to start Writing
or coding the program in an appropriate programming language which is suitable for given a
application Any program is not complete without testing the code with possible inputs.
This stage is also referred to as program e xecution or running the program. If there are
any errors in the program then debugging is an important step in problem solving i.e.
removing the bugs or error.
4. The algorithm
An algorithm is a finite sequence of instructions which can be carried out to solve a
particular problem in order to obtain the desired results.
C Programming Language
Electronic Science
1. Introduction to C language
14
C Programming Language
Electronic Science
1. Introduction to C language
15
5. Flow charts
A flowchart is a pictorial representation of an algorithm. Flow chart consists of set of
flow chart symbols connected by arrows. Each symbol has its own meaning, tells to
compiler what must be done at that point. The sequence of flow chart symbols can be
considered as a program.
Flow charts are better way of communicating logic of system. Problem can be analyzed
in effective way with the flowchart. If the algorithms are represented in a graphical or
pictorial form they are easy to understand. Efficient coding acts as Guide or Blue print
during system analysis or development phase.
5.1 Guidelines for flowchart
Usually flow charts are drawn using some standard symbols. Some special symbols can
also be developed when required. Standard symbols, which are frequently required for
flowcharting are shown as follows.
C Programming Language
Electronic Science
1. Introduction to C language
16
Let us now consider simple general flowcharting technique. Let us start drawing a
flowchart for a given problem.
C Programming Language
Electronic Science
1. Introduction to C language
17
Here different flowchart symbols are shown and are interconnected by the flow line.
Start, input, processing, decision, predefined process, output and terminator are
interconnected.
C Programming Language
Electronic Science
1. Introduction to C language
18
In this example we can draw simple flowchart for sum of first 50 natural numbers. So we
need flowchart symbols for input,output,process,decision,start and end. The first and
last symbols are starting and end of flowcharting. Initial sum of numbers and number
assumed to be zero which are inputted in input symbol. The sum is added to the
number which is initialized as zero. The decision block used in the flowchart takes the
decision when number is equal to 50, if not then natural number is incremented by one
and added in to the sum. This process continues till number N goes up to 50.If number
is 50 then final sum will be calculated and displayed using output symbol.
Example using Algorithm and Flow chart.
Write an algorithm and draw a flowchart for computing factorial n (n!) Where n! = 1 x 2
x 3 x x n
C Programming Language
Electronic Science
1. Introduction to C language
19
C Programming Language
Electronic Science
1. Introduction to C language
20
6. Summary
In the language learning it is most important is to have deep knowledge about what to
tell computer to do and use different implementation strategies to write algorithm. With
the algorithm in hand, it is possible to write the instructions necessary to implement the
algorithm. Programs are developed to solve some problem. Programming language
levels are: Machine language, Assembly language, Middle level language and High
level language.
C is a general-purpose high level language that was originally developed for the UNIX
operating system. C language is mainly used for develop desktop based application. All
other programming languages were derived directly or indirectly from C programming
concepts. C is often called a middle-level computer language. C language is very often
known as a System Programming Language, because it is used for writing
assemblers, compilers, editors and even operating systems. C language is used in wide
variety of applications. Practical applications of C language are several, right form
writing the operating systems like UNIX, Windows to creating antivirus programs.
C Programming Language
Electronic Science
1. Introduction to C language