0% found this document useful (0 votes)
4 views

Unit I

The document provides an overview of imperative programming, specifically focusing on the C programming language, its history, types, and features. It discusses the differences between low-level and high-level languages, the role of translators, and the program development cycle, including requirements, analysis, design, coding, testing, and maintenance. Additionally, it covers the use of pseudocode and flowcharts as tools for algorithm representation and program planning.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit I

The document provides an overview of imperative programming, specifically focusing on the C programming language, its history, types, and features. It discusses the differences between low-level and high-level languages, the role of translators, and the program development cycle, including requirements, analysis, design, coding, testing, and maintenance. Additionally, it covers the use of pseudocode and flowcharts as tools for algorithm representation and program planning.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

FY BSc IT Imperative Programming SEM-I

UNIT – I
Introduction, Fundamentals
Chapter No.1: Introduction

❖ History and Types of Programming Languages

 C is a programming language developed at A T & T’s Bell Laboratories of USA in 1972.


 It was designed and written by a man named Dennis Ritchie.
 It was initially designed for programming UNIX operating system.
 C seems so popular is because it is reliable, simple and easy to use.
 There is a close analogy between learning English language and learning C language.
 The classical method of learning English is to first learn the alphabets used in the
language, then learn to combine these alphabets to form words, which in turn are
combined to form sentences and sentences are combined to form paragraphs.
 Learning C is similar and easier. Instead of straight-away learning how to write
programs, we must first know what alphabets, numbers and special symbols are used in
C, then how using them constants, variables and keywords are constructed, and finally
how are these combined to form an Instruction.
 A group of instructions would be combined later to form a Program. So, a computer
Program is just a collection of the instructions necessary to solve a specific problem.
 The basic operations of a computer system form what is known as the computer’s
Instruction set.
 And the approach or method that is used to solve the problem is known as an Algorithm.
FY BSc IT Imperative Programming SEM-I

 Programming Language concern these are of two types: -


1) Low level language
2) High level language

1) Low level language: -


 Low level languages are machine level and assembly level language.
 In machine level language computer only understands digital numbers i.e. in the form of
0 and 1. So, instruction given to the computer is in the form binary digit, which is
difficult to implement instruction in binary code.
 This type of program is not portable, difficult to maintain and error.
 An assembly language is on other hand modified version of machine level language.
Where instructions are given in English like word as ADD, SUM, MOV etc.
 It is easy to write and understand but not understand by the machine. So, the translator
used here is assembler to translate into machine level.
 In the assembly level language, the data are stored in the computer register, which varies
for different computer. Hence it is not portable.

2) High level language: -


 These languages are machine independent, means it is portable.
 The language in this category is Pascal, Cobol, Fortran etc.
 High level languages are understood by the machine. So, it needs to translate by the
translator into machine level.
 A translator is software which is used to translate high level language as well as low
level language into machine level language.

 Three types of translators are as follows:


i) Compiler
ii) Interpreter
iii) Assembler

 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

 If it checks error, statement by statement and hence of take more time.

 Integrated Development Environments (IDE): -


 The process of editing, compiling, running, and debugging programs is often managed by
a single integrated application known as an Integrated Development Environment,
(IDE).
An IDE is a windows-based program that allows us to easily manage large software
programs, edit files in windows, and compile, link, run, and debug programs.

❖ Features and Application

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.

2. Fast and Efficient


 Newer languages like Java, python offer more features than c programming
language but due to additional processing in these languages, their performance rate
gets down effectively.
FY BSc IT Imperative Programming SEM-I

 C programming language as the middle-level language provides programmers access to


direct manipulation with the computer hardware but higher-level languages do not
allow this.
 That’s one of the reasons C language is considered the first choice to start learning
programming languages.
 It’s fast because statically typed languages are faster than dynamically typed languages.
3. Modularity
 The concept of storing C programming language code in the form of libraries for
further future uses is known as modularity.
 This programming language can do very little on its own most of its power is held by
its libraries.
 C language has its own library to solve common problems.

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.

6. Rich set of built-in Operators


 It is a diversified language with a rich set of built-in operators which are used in writing
complex or simplified C programs.

7. Libraries with Rich Functions


 Robust libraries and functions in C help even a beginner coder to code with ease.

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

10. Easy to Extend


 Programs written in C language can be extended means when a program is already
written in it then some more features and operations can be added to it.

 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

7. Development of New languages: -


 Due to the fast execution and simplicity, many languages like Java, C++, Python, PHP,
PERL, JavaScript, etc were influenced by the development of C.
 In Python, C is used for building standard libraries.
 The syntax and control structures of PERL, PHP and C++ are based upon the C
programming language.

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.

10. Compiler Design: -


 C also helped in designing several popular compilers like Clang C, MINGW, Apple C
etc. This is one of the most popular uses of C language.

❖ Program Development Cycle


The program development cycle contains 6 phases, they are as follows:

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

 The program is tested on a number of suitable test cases.


 A test plan of the program has to be done at the stage of the program design itself.
 This ensures a thorough understanding of the specifications.
 Running the program several times using different sets of data verifies whether a program
works correctly for every situation provided in the algorithm.
 It is always useful to include the maximum and minimum values of all variables as test
data.
FY BSc IT Imperative Programming SEM-I

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 statements and Flowchart symbols

 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.

 How to write a Pseudo-code?

1. Arrange the sequence of tasks and write the pseudocode accordingly.


2. Start with the statement of a pseudo code which establishes the main goal or aim.
FY BSc IT Imperative Programming SEM-I

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”.

 Basic Symbols used in Flowchart Designs


1. Terminal: The oval symbol indicates Start and Stop in a program’s logic flow.
Terminal is the first and last symbols in the flowchart.

2. Input/Output: A parallelogram denotes any function of input/output type. Program


instructions that take input from input devices and display output on output devices are
indicated with parallelogram in a flowchart.
FY BSc IT Imperative Programming SEM-I

3. Processing: A rectangular box represents arithmetic instructions. All arithmetic


processes such as adding, subtracting, multiplication and division are indicated by
action or process symbol.

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.

 Rules For Creating Flowchart:


 A flowchart is a graphical representation of an algorithm.
 It should follow some rules while creating a flowchart.
 Rule 1: Flowchart opening statement must be ‘start’ keyword.
 Rule 2: Flowchart ending statement must be ‘end’ keyword.
 Rule 3: All symbols in the flowchart must be connected with an arrow line.
 Rule 4: The decision symbol in the flowchart is associated with the arrow line.

 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

Chapter No.2: Fundamentals

 Structure of C Language Program: -

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.

3) Global Variable Declaration: -


 This is the section where variable is declared globally so that it can be access by all the
functions used in the program.
 And it is generally declared outside the function.

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: -

/*First c program with return statement*/


#include <stdio.h>
int main (void)
{
printf ("welcome to c Programming language.\n");
return 0;
}
Output: welcome to c programming language.

 Steps for Compiling and executing the Programs: -


1. We use Editor to write program i.e.
2. Turbo C (DOS Based)
3. Borland's C (Windows Based)
4. ANSI C (UNIX OS)
5. We will use Turbo C
6. Start ->run->c: \ tc\bin\ tc.exe.
7. Start new - >Write Program
8. Compile- alt+f9
9. Run – ctrl+f9
FY BSc IT Imperative Programming SEM-I

❖ 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

 There are two types of type qualifier in C:


1. Size qualifier: short, long
2. Sign qualifier: signed, unsigned

 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:

Basic Datatype with type Size Range


Datatypes qualifier (byte)
char char or signed char 1 -128 to 127
Unsigned char 1 0 to 255
int int or signed int 2 -32768 to 32767
unsigned int 2 0 to 65535
short int (signed) 1 -128 to 127
unsigned short int 0 to 255
1
long int (signed) -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
4
float float 4 -3.4E-38 to 3.4E+38
double double 8 1.7E-308 to 1.7E+308
long double 10 3.4E-4932 to 1.1E+4932
FY BSc IT Imperative Programming SEM-I

 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).

ii) Real constant: -


 Real constant is also called floating point constant.
 To construct real constant, we must follow the rule of,
 Real constant must have at least one digit.
 It must have a decimal point.
 It could be either positive or negative.
 Default sign is positive.
 No commas or blanks are allowed within a real constant.
 Ex.: +325.34, 426.0, -32.76

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:

Data type variable name = constant;


Example: int a=20;
OR
int a;
a=20;

❖ 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 character may represent a numeric constant, a character constant, or a string.

 When the program is compiled, each occurrence of a symbolic constant is replaced by


its corresponding character sequence.

 They are usually defined at the beginning of the program.

 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

 A C program consists of the following symbolic constant definitions.


#define PI 3.141593
#define TRUE 1
#define FALSE 0

 #define PI 3.141593 defines a symbolic constant PI whose value is 3.141593.


 When the program is pre-processed, all occurrences of the symbolic constant PI are
replaced with the replacement text 3.141593.

You might also like