Unit I
Unit I
UNIT – I
Introduction, Fundamentals
Chapter No.1: Introduction
Compiler and interpreter are used to convert the high-level language into machine level
language.
The program written in high level language is known as source program and the
corresponding machine level language program is called as object program.
Both compiler and interpreter perform the same task but there working is different.
Compiler read the program at-a-time and searches the error and lists them.
If the program is error free, then it is converted into object program.
When program size is large then compiler is preferred. Whereas interpreter read only
one line of the source code and convert it to object code.
FY BSc IT Imperative Programming SEM-I
1. Procedural Language
In a procedural language like C step by step, predefined instructions are carried out.
C program may contain more than one function to perform a particular task.
There are other programming paradigms as well in the programming world.
Most of the commonly used paradigm is an object-oriented programming language.
4. Statically Type
C programming language is a statically typed language.
Meaning the type of variable is checked at the time of compilation but not at run time.
This means each time a programmer types a program they have to mention the type of
variables used.
5. General-Purpose Language
The C programming language is used in various applications. Some of the common
applications where it’s used are as follows:
Operating systems: Windows, Linux, iOS, Android etc.
Databases: PostgreSQL, Oracle, MySQL, MS SQL Server, etc.
8. Middle-Level Language
As it is a middle-level language so it has the combined form of both capabilities of
assembly language and features of the high-level language.
9. Portability
C language is portable as programs that are written in C language can run and compile
on any system with either no or small changes.
FY BSc IT Imperative Programming SEM-I
Applications of C
Use of the C programming language is not limited to the development of operating
systems and applications.
It is also used in GUI development, IDE development, etc.
Some uses of C programming language: -
1. Operating Systems: -
With the help of the C programming language, you can write your own operating system.
Windows Kernel, Linux Kernel and Apple’s OS X kernel are mostly written in C.
2. GUI: -
It stands for Graphical User Interface. The C programming language also helps in
developing popular adobe software like Photoshop, Premier Pro, Illustrator etc.
3. Embedded Systems: -
In daily life, we use different embedded systems like coffee machines, microwaves,
climate control systems etc. These all are mostly programmed in C.
4. Database: -
The C programming language helps in developing the popular database management
system, MySQL.
5. Ease of Computation: -
C provides faster computation in programs.
The implementation of algorithms and data structures is swift in C.
With the help of C, you can perform high degree calculations such as MATLAB,
Mathematica etc.
6. Gaming: -
C programming is relatively faster than Java or Python.
It has been used in various gaming applications and graphics.
C programming language also helps in creating many popular childhood games like Tic-
Tac-Toe, The Snake game etc.
FY BSc IT Imperative Programming SEM-I
8. Assemblers: -
Mainly used to translate Assembly language to Machine language.
C also helped in developing GNU assembler.
9. Interpreters: -
With the help of C programming language, you can create language interpreters.
C helped in developing different programming language interpreters like Python and
MATLAB interpreters etc.
1. Requirements
2. Analysis
3. Design
4. Coding
5. Testing
6. Maintenance
1. Requirements
we define the problem statement and we decide the boundaries of the problem.
Information about the problem must be stated clearly and unambiguously.
we need to understand the problem statement and gather the preliminary requirements.
Main objective of this phase is to eliminate unimportant aspects and identify the root
problem
FY BSc IT Imperative Programming SEM-I
2. Analysis
All the factors like Input/output, processing requirement, memory requirements, error
handling, interfacing with other programs have to be taken into consideration in this stage.
It also determines the required format in which results should be displayed
3. Design
The software developer makes use of tools like algorithms and flowcharts to develop the
design of the program.
o Algorithm
o Flowchart
Algorithm is the list of instructions in a particular order to solve the problem.
Flowcharts are used to get the pictorial representation of the algorithm.
4. Coding
Once the design process is complete, converting algorithm to a program by selecting any
one of the high – level languages that is suitable for the problem.
Coding is generally a very small part of the entire program development process and also
a less time-consuming activity in reality.
In this process we identify and eliminate all the syntax errors related to spelling, missing
commas, undefined labels etc.
For effective coding some of the guide lines are to be follow:
o Use of meaningful names and labels of variables
o Simple and clear expressions
o Making use of comments and indenting the code properly
o Avoiding jumps in the program to transfer control
5. Testing
6. Maintenance
Updating and correction of the program for changed conditions and field experience is
accounted for in maintenance.
Maintenance becomes essential in following situations:
o Change in specification
o Change in equipment
o Errors which are found during the actual execution of the program
Pseudo Code: -
Pseudo code is a term which is often used in programming and algorithm-based fields.
It is a methodology that allows the programmer to represent the implementation of an
algorithm.
Algorithms are represented with the help of pseudo codes as they can be interpreted by
programmers.
Pseudo code, as the name suggests, is a false code or a representation of code which can
be understood by even a layman with some school level programming knowledge.
Algorithm: It’s an organized logical sequence of the actions or the approach towards a
particular problem.
A programmer implements an algorithm to solve a problem. Algorithms are expressed
using natural verbal but somewhat technical annotations.
Pseudo code: It’s simply an implementation of an algorithm in the form of annotations
and informative text written in plain English.
It has no syntax like any of the programming language and thus can’t be compiled or
interpreted by the computer.
Advantages of Pseudocode
Improves the readability of any approach. It’s one of the best approaches to start
implementation of an algorithm.
Acts as a bridge between the program and the algorithm or flowchart.
Also works as a rough documentation, so the program of one developer can be
understood easily when a pseudo code is written out.
The main goal of a pseudo code is to explain what exactly each line of a program
should do, hence making the code construction phase easier for the programmer.
3. Example:
This program will allow the user to check the number whether it's even or odd.
Pseudo-code: -
if "1"
print response
"I am case 1"
if "2"
print response
"I am case 2"
Flowchart: -
Flowchart is a graphical representation of an algorithm.
Programmers often use it as a program-planning tool to solve a problem.
It makes use of symbols which are connected among them to indicate the flow of
information and processing.
The process of drawing a flowchart for an algorithm is known as “flowcharting”.
4. Decision: Diamond symbol represents a decision point. Decision based operations such
as yes/no question or true/false are indicated by diamond in flowchart.
5. Connectors: Whenever flowchart becomes complex or it spreads over more than one
page, it is useful to use connectors. It is represented by a circle.
FY BSc IT Imperative Programming SEM-I
6. Flow lines: Flow lines indicate the exact sequence in which instructions are executed.
Arrows represent the direction of flow of control and relationship among different
symbols of flowchart.
Advantages of Flowchart:
Flowcharts are a better way of communicating the logic of the system.
Flowcharts act as a guide for blueprint during program designed.
Flowcharts help in debugging process.
With the help of flowcharts programs can be easily analysed.
It provides better documentation.
Flowcharts serve as a good proper documentation.
Easy to trace errors in the software.
Easy to understand.
FY BSc IT Imperative Programming SEM-I
Disadvantages of Flowchart:
It is difficult to draw flowcharts for large and complex programs.
There is no standard to determine the amount of detail.
Difficult to reproduce the flowcharts.
It is very difficult to modify the Flowchart.
Making a flowchart is costly.
Some developer thinks that it is waste of time.
It makes software processes low.
If changes are done in software, then the flowchart must be redrawn
Example: Draw a flowchart to input two numbers from the user and display the largest
of two numbers
FY BSc IT Imperative Programming SEM-I
1) Comment line
2) Preprocessor directive
3) Global variable declaration
4) Main function()
1) Comment line: -
It indicates the purpose of the program.
A comment should always be enclosed with /*………. */ or //.
Two Types of comments:
i) /* -------------------*/ (Multiline Comment)
ii) // (Single line comment).
Comment line is used for increasing the readability of the program.
It is useful in explaining the program and generally used for documentation.
It is enclosed within the decimeters.
Comment line can be single or multiple line but should not be nested.
It can be anywhere in the program except inside string constant & character constant.
2) Preprocessor Directive: -
#include<stdio.h> tells the compiler to include information about the standard
input/output library.
It is also used in symbolic constant such as #define PI 3.14(value).
The stdio.h (standard input output header file) contains definition and declaration of
system defined function such as printf( ), scanf( ), pow( ) etc.
Generally printf() function is used to display and scanf() function is used to read value.
4) main function(): -
It is the user defined function and every function has one main() function from where
actually program is started and it is encloses within the pair of curly braces.
The main( ) function can be anywhere in the program but in general practice it is placed
in the first position.
FY BSc IT Imperative Programming SEM-I
Syntax:
main()
{
……..
……..
}
The main( ) function return value when it declared by data type as:
int main( )
{
return 0;
}
The program execution starts with opening braces and end with closing brace.
In between the two braces declaration part as well as executable part is mentioned.
At the end of each line, the semi-colon is given which indicates statement termination.
Example: -
❖ Character Set
character denotes any alphabet, digit or special symbol used to represent information.
Valid alphabets, numbers and special symbols allowed in C are as follows:
The alphabets, numbers and special symbols when properly combined form
constants, variables and keywords.
Identifiers: -
Identifiers are user defined word used to name of entities like variables, arrays,
functions, structures etc.
Rules for naming identifiers are:
1) name should only consist of alphabets (both upper and lower case), digits and
underscore (_) sign.
2) first characters should be alphabet or underscore.
3) name should not be a keyword.
4) since C is a case sensitive, the upper case and lower case considered differently, for
example code, Code, CODE etc. are different identifiers.
5) Identifiers are generally given in some meaningful name such as value, net_salary,
age, data etc. Some invalid identifiers are 5cb, int, res#, avg no etc.
Keyword: -
There are certain words reserved for doing specific task, these words are known as
Reserved word or Keywords.
These words are predefined and always written in lower case or small letter.
These keywords cannot be used as a variable name as it assigned with fixed meaning.
There are 32 keywords available in C.
Some examples are auto, break, case, char, const, continue, default, do, double,
enum, extern, else, float, for, far, goto, if, int, long, near, register, return, short,
signed, static, struct, switch, typedef, union, unsigned, void, while etc.
FY BSc IT Imperative Programming SEM-I
Datatypes: -
Data types refer to an extensive system used for declaring variables or functions of
different types before it’s use.
The type of a variable determines how much space it occupies in storage and how the
bit pattern stored is interpreted.
The value of a variable can be changed any time.
C has the following 3 types of datatypes:
i) Basic built-in (Primary) data type: int, float, double, char, void
ii) User-defined data type: class, structure, union, enum, Typedef
iii) Derived data type: pointer, array, function, reference
A variable declared to be of type int can be used to contain integral values. (Only that
values which do not contain decimal places.)
A variable declared to be of type float can be used for storing floating- point numbers
(values containing decimal places).
The double type is the same as type float, only with roughly twice the precision.
The char data type can be used to store a single character, such as the letter a, the
digit character 6, or a semicolon. similarly, A variable declared char can only store
character type value.
FY BSc IT Imperative Programming SEM-I
When the qualifier unsigned is used the number is always positive, and when signed is
used number may be positive or negative.
The range of values for signed data types is less than that of unsigned data type.
Because in signed type, the left most bit is used to represent sign, while in unsigned type
this bit is also used to represent the value.
The size and range of the different data types on a 16-bit machine is given below:
Constants: -
Constant is an any value that cannot be changed during program execution. In C,
any number, single character, or character string is known as a Constant.
For example, i) The number 50 represents a constant integer value. ii) The character
string "Programming in C is fun.\n" is an example of a constant character string.
Constants can be divided into two major categories:
1) Primary Constants
2) Secondary Constants
These constants are further categorized as:
Classification of Constants: -
1) Numeric constant
2) Character constant
3) String constant
1) Numeric constant: -
Numeric constant consists of digits.
It required minimum size of 2 bytes and max 4 bytes.
It may be positive or negative but by default sign is always positive.
No comma or space is allowed within the numeric constant and it must have at least 1
digit.
FY BSc IT Imperative Programming SEM-I
It is categorized as:
i) Integer constant
ii) Real constant
i) Integer constant: -
Integer constants are whole number which have no decimal point.
Types of integer constants are:
Decimal constant: 0-------9 (base 10)
Octal constant: 0-------7 (base 8)
Hexa decimal constant: 0----9, A------F (base 16)
In decimal constant first digit should not be zero unlike octal constant first digit must be
zero (as 076, 0127).
In hexadecimal constant first two digit should be 0x/ 0X (such as 0x24, 0x87A).
2) Character constant: -
Character constant represented as a single character enclosed within a single quote.
These can be single digit, single special symbol or white spaces such as ‘9’,’c’,’$’, ‘ ’
etc.
Every character constant has a unique integer like value in machine’s character code as if
machine using ASCII (American standard code for information interchange).
A------------ Z ASCII value (65-90)
a-------------z ASCII value (97-122)
0-------------9 ASCII value (48-57)
; ASCII value (59)
3)String constant: -
Set of characters are called string.
When sequence of characters are enclosed within a double quote is a string constant.
String constant has zero, one or more than one character and at the end of the string null
character (\0) is automatically placed by compiler.
Some examples are “sarathina” , “908”, “3”,” ”, “A” etc.
FY BSc IT Imperative Programming SEM-I
In C although same characters are enclosed within single and double quotes it represents
different meaning such as “A” and ‘A’ are different because first one is string attached
with null character at the end but second one is character constant with its corresponding
ASCII value is 65.
Variables: -
Variable is a data name which is used to store some data value or symbolic names for
storing program computations and results.
The value of the variable can be change during the execution.
The rule for naming the variables is same as the naming identifier.
Before used in the program it must be declared.
Declaration of variables specify its name, data types and range of the value that variables
can store depends upon its data types.
Syntax:
int a;
char c;
float f;
Variable initialization: -
When we assign any initial value to variable during the declaration, is called initialization
of variables.
When variable is declared but contain undefined value then it is called garbage value.
The variable is initialized with the assignment operator such as:
❖ Symbolic Constants: -
FY BSc IT Imperative Programming SEM-I
symbolic constant is name that substitute for a sequence of character that cannot be
changed.
The symbolic constants may then appear later in the program in place of the numeric
constants, character constants, etc., that the symbolic constants represent.
For example