Unit 1 FPL
Unit 1 FPL
Syllabus:
The algorithm and flowchart are two types of tools to explain the process of a program.
1.1.1 Algorithm:
Definition of algorithm:
Characteristics of algorithms:
iii) Effectiveness : Each step must be effective, in the sense that it should be easily
convertible into program statement and can be performed exactly in a finite amount of time.
iv) Generality : The algorithm should be complete in itself so that it can be used to solve all
problems of a given type for any input data.
v) Input/Output : Each algorithm must take zero, one or more quantities as input data and
yield one or more output values.
Step 1: Start
Step 4: Add num1 and num2 and assign the result to sum.
Sum = num1+num2
Step 6: Stop
Step 1:BEGIN
A = R*R
2
FPL:Unit-1 DIT,Pimpri
Step6: END
Step 1 : Start
Step 7 : Stop
Step 1:BEGIN
Step3: if NUM1>NUM2
Step4: END
1.1.2 Flowchart:
A Flowchart is a graphical representation of a Algorithm.
The sequence of steps in algorithm is maintained and represented in the flowchart by
using some standard symbols such as rectangles and directed lines.
3
FPL:Unit-1 DIT,Pimpri
Start/stop/begin/end
Oval Terminal
symbol.
Making data available for
processing (input) or
Parallelogram Input/Output recording of the processed
information(output).
Any processing to be
performed. An assignment
Rectangle Process operation normally
represented by
this symbol.
Decision or switching type of
operations that determines
4
FPL:Unit-1 DIT,Pimpri
2. Overview of C:
2.1.1 History:
5
FPL:Unit-1 DIT,Pimpri
ALGOL 60 Never Became Popular Because It Was Too Abstract And Too General.
CPL Turned Out To Be So Big, Having So Many Features, This It Was Hard To
Learn And Difficult To Implement.
Around same time a language called “B” was written by Ken Thompson.
Finally Dennis Ritchie inherited the features of B and BCPL, added some of his own
stuff and developed “C”.
6
FPL:Unit-1 DIT,Pimpri
2.1.2 Importance of C:
1) It is a robust language, whose rich set of built-in functions and operators can
be used to write any complex program.
2) Programs written in C are efficient and fast. This is due to its variety of data
types and powerful operators.
3) C’s code is very portable, in the sense that it is easy to adapt software
written for one type of computer or operating system to another type.
4) C has very small key words (only 32). Its strength lies in its built-in
functions. These built-in functions can be used for developing programs.
5) C language is well suited for structured programming, thus requiring the
user to think of a problem in terms of functions (or) blocks.
6) A proper collection of these functions would make a complete program.
This modular structure makes program debugging, testing and maintenance
easier.
7) Another important feature of C is its ability to extend itself.
Before discussing any features of C, we shall look at some sample C program and analyze
and understand how they work.
#include <stdio.h>
int main() {
printf("Welcome to DIT\n");
return 0;
Explanation:
i) #include <stdio.h>: This line includes the Standard Input Output header file,
7
FPL:Unit-1 DIT,Pimpri
Welcome to DIT
iv) return 0 : This statement terminates the main function and returns the value 0 to
the operating system, indicating that the program ended successfully.
v) Every statement in C should end with a semicolon(;) mark.
-------
8
FPL:Unit-1 DIT,Pimpri
The following steps are used in sequence for developing an efficient program:
Designing an algorithm
Coding
Debugging
Linking the program with functions that are needed from the C library.
2. Portability: C programs can be compiled and run on various platforms with little or no
modification, making it a highly portable language. The standard library functions ensure
consistency across different environments.
3. Speed: C is known for its high execution speed. Because it is a compiled language and
offers low-level access to memory, it is often used in performance-critical applications.
4. Control: C provides a high level of control over system resources, such as memory
management, file handling, and I/O operations. This allows programmers to write highly
efficient code and directly manage the hardware.
9
FPL:Unit-1 DIT,Pimpri
1. Operating Systems:
C is the backbone of many operating systems like UNIX and Linux. Its ability to work
closely with hardware makes it ideal for building and maintaining operating systems.
2. Embedded Systems:
C is commonly used in embedded systems, which are found in everyday devices like
microwaves, TVs, and car engines. It’s chosen for its efficiency and direct hardware
control.
3. Game Development:
C is used to develop game engines and performance-critical parts of video games. Its
speed allows for smooth gameplay and complex graphics handling.
Many compilers and interpreters, including those for the C language itself, are written in
C. This is because C allows precise control over memory and processing.
5. Databases:
C is used to create database management systems like MySQL. Its efficiency helps
manage large amounts of data effectively.
6. Network Programming:
C is used to develop network drivers, protocols, and tools. It’s well-suited for network
programming because it allows fine control over data and memory management.
Character set refers to a set of all the valid characters that we can use in the source
10
FPL:Unit-1 DIT,Pimpri
program for forming words, exepressions and numbers. The characters in C are grouped
into four categories.
1. Letters a, b, c, ……………z
2. Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
4. white spaces
With these characters are combined to form data types, constants, variables and key words.
4)White Spaces
11
FPL:Unit-1 DIT,Pimpri
Blank space
Horizontal tab
Carriage return
New line
Form feed
2.3 C Tokens:
Types of tokens:
12
FPL:Unit-1 DIT,Pimpri
In ‘C’ every word is classified into either a key word or an identifier. All key
word have fixed meaning cannot be changed. Keywords serve as a basic building block
for program statements. All the keywords must be written in lower case.
2.4.1 Keywords:
Keywords are predefined tokens in C. These are also called reserved words.
Key words have special meaning to the C compiler. These key words can be used
only for their intended action; they cannot be used for any other purpose.
C has 32 keywords.
Standard key words:
2.4.2 Identifiers:
Identifiers refer to the names of the variable, function and arrays. These are user
defined names and consists of sequence of letters and digits.
13
FPL:Unit-1 DIT,Pimpri
Keywords Identifires
An Identifier is a unique name
1. Keywords are related words with a
created by the programmer to
definite, predetermined meaning .
define a variable, structure, class,
Any programme statement may be
or function.
defined with the aid of keywords.
The initial character in the
2. A keyword always starts in
Identifier can be capitalized,
lowercase.
lowercase or starts with
underscore.
It can have numbers, alphabetical
3. It can only have alphabetical
characters and underscores.
characters.
5. Examples of IDs are test, count1,
4. Examples of keywords are double,
high speed etc.
int, auto, char, break and many
others.
2.5 Constants:
Constants are fixed values that do not change during the execution of program.
For example in the equations 5x+2y=45 since 5, 2 and 45 cannot change , these are called
constants, where as the quantity X&Y can vary or change hence these are called variables .
14
FPL:Unit-1 DIT,Pimpri
Numeric constants:
Integer constants: It refers to a sequence of digits, it has to fallow the below rules:
6. The allowable range for integer constants is -32768 to +32767 (16-bit machine).
i) decimal
ii) octal
iii) hexadecimal
15
FPL:Unit-1 DIT,Pimpri
Note: Embedded spaces, commas, and non-digit characters are not permitted between
digits.
Ex: 12 727 23,879 $1772 are illegal numbers.
ii) An Octal Integer constant: It consists of any combination of digits from the set 0
through 7,with a leading 0.
Ex: 012 07134 07777
Real Constants:
• A real constants are sequence of digits with a decimal point(fractional part) like
45.382.Such numbers are called real(or floating point) constants.
• Integer numbers are inadequate to represent quantities that vary continuously
,such as distances, heights, temperatures, prices etc.
16
FPL:Unit-1 DIT,Pimpri
mantissa e exponent
The letter e separating the mantissa and the exponent can be written
in either lowercase or uppercase
The scientific notation is often used to express numbers that are either very small or
very large.
Ex: 7500000000 may be written as 7.5e9 or 75e8.
Character constant:
17
FPL:Unit-1 DIT,Pimpri
printf(“%d”, ‘a’);
would print the number 97, the ASCII value of the letter a.
Similarly,
printf(“%c”, ‘97’)
would output the letter ‘a’.
String constant:
A string constant is a sequence of characters enclosed in double quotes.
The characters may be letters, numbers, blank space or special characters.
Note that “ ” is null string or empty string. And the single string constant “A” is
not equivalent to the single character constant ‘A’.
Each string constant must end with a special character ‘\o’. This character is called
null character and used to terminate the string. The compiler automatically places a
null character ‘\o’ at the end of every string constant.
Ex: ”hello” “1999” “5+4+6” “good bye”
C supports some special backslash character constants that are used in output
functions. Each one of them represents one character, although they consist of two
characters. These character combinations are known as escape sequences.
18
FPL:Unit-1 DIT,Pimpri
2.6 Variables:
A variable is a data name which can be used to store a data value.
A variable may take different values at different times, during execution.
Syntax:
20
FPL:Unit-1 DIT,Pimpri
• Data type is a data storage format that can contain a specific type or range of
values.
• Data types in C refer to an extensive system used for declaring variable for
function of different types.
• Each data type has predetermined memory requirement and an associated range of
legal values.
ANSI C supports three classes of data types.
21
FPL:Unit-1 DIT,Pimpri
22
FPL:Unit-1 DIT,Pimpri
Integer Types
Integers are the whole numbers with a range of values supported by a particular
machine.
Integers occupy one word of storage, but as the word sizes of machine vary(16 bits
or 32 bits) the size of an integer depends on the computer.
For 16 bit word length, the size of integer value range between -32768 to +32767
(-215 to +215 -1) and for a 32 bit word length range is -2,147,468 to 2,147,468,647.
A signed integer uses one bit for sign and 15 bits for magnitude of a number.
C provides three different types of integers they are int, short int and long int in
both signed and unsigned forms. The difference between these three integers is
23
FPL:Unit-1 DIT,Pimpri
short int
int
long int
Fig. 2.5 Integer Types
short int represents fairly small integer values and requires half the amount of
storage than regular int number uses.
To increase the range of values we have to declare long and unsigned integers.
24
FPL:Unit-1 DIT,Pimpri
Like integers floats are divided into three types. They are float, double and long
double.
The difference between these three floats are the number of bytes, the variable of
these types occupy and subsequently the range of values.
A floating point (or real ) numbers are stored in 32 bits(on both 16bit and 32 bit
machine) with 6 digits of precision.
short
floatint
double
long double
Character Types:
A char is a data type which can store an element of machine character set.
These are two types, they are signed and unsigned characters. The
25
FPL:Unit-1 DIT,Pimpri
differences between these two types are the range of values. Both will occupy
one byte.
Signed chars have values between -128 to 127 and unsigned chars have
values 0 to 255.
Void Types:
The users can define an identifier that represent an existing data type by a feature
known as “type definition”.
The user defined data type identifier can later be used to declare variables.
General form:
where type refers to an existing data type and identifier refers to the new name given
to the data type.
Ex:
typedef int Sno;
typedef float salary;
Here Sno symbolizes int and salary symbolizes float. These can be used to
declare variables as follows:
Sno c1,c2;
salary e1,e2;
Note: The main advantage of typedef is that we can create meaningful data type names for
increasing the readability of the program.
26
FPL:Unit-1 DIT,Pimpri
Another user defined data type is enumerated data type provided by ANSI .
General form:
the enumerated variables v1, v2, …..vn can only have one of the value 1, value 2, ……
value n.
Ex 1:
enumday{Monday,Tuesday……Sunda};
enumday week_st, week_end;
week_st=Monday;
week_end=Friday;
if(week_st==Tuesday)
week_end=Saturday;
The compiler automatically assigns integer digits beginning with 0 to all the
enumeration constants. That is, the enumeration constant Monday is assigned with 0,
Tuesday is assigned with 1 and so on. However, the automatic assignments can be
overridden by assigning values explicitly to the enumeration constants.
For example,
27
FPL:Unit-1 DIT,Pimpri
here, the constant Monday is assigned the value 1.The remaining constants
are assigned values that increases successively by 1.
The declaration of variables must be done before they are used in the program.
data-type v1,v2,…….,vn;
int count;
double ratio;
28
FPL:Unit-1 DIT,Pimpri
Here, int and double are the keywords to represent integer type and real type data
values respectively.
The program statement given infollowing fig 2.7 illustrates declaration of variables.
Every variable that we declare in a C program has its scope and lifetime.
Storage class in C provides information about the location and visibility of variables.
It determines the portion of the program within which the variables are recognized.
29
FPL:Unit-1 DIT,Pimpri
30
FPL:Unit-1 DIT,Pimpri
2. The scope remains throughout The scope is limited and remains within
the program. the function only in which they are
declared.
3. Any change in global variable Any change in the local variable does not
affects the whole program, affect other functions of the program.
wherever it is being used.
7. Global variables are stored in the Local variables are stored in a stack in
data segment of memory. memory.
31
FPL:Unit-1 DIT,Pimpri
Storage Scope,
Name Memory Lifetime
Class Default Value
Basic Assignment
Values can be assigned to variables using the assignment operator ‘=’ as follows:
Syntax:
variable_name = constant;
32
FPL:Unit-1 DIT,Pimpri
For example:
int final_value = 100;
char x = 'x';
double balance = 75.84;
The process of giving initial values to variables is called initialization.
C permits the initialization of more than one variable in one statement using
multiple assignment operators.
For example:
p=q=s=0
x = y =z = MAX
33
FPL:Unit-1 DIT,Pimpri
Another way of giving values to variables is to input data through keyboard using
function “scanf”.
The scanf function is used to input data from the keyboard in C.
The control string specifies the format of the data being received.
The & symbol before each variable name specifies the variable's address.
For example: scanf("%d", &number);
When the scanf statement is executed, it waits for input.
main()
int number;
else
Output
54
34
FPL:Unit-1 DIT,Pimpri
Unique constants refer to specific, unchanging values that are consistently used
throughout a program, such as the value of pi (3.142) or the number of students (50).
These constants ensure that important values remain consistent and accurate wherever
they appear in the code.
Using unique constants helps in maintaining clarity and simplifying the process of
updating values, thereby improving code reliability and maintenance.
1. Modifiability:
2. Understandability:
The #define pre-processor directive is used to create symbolic constants. This makes the code
more readable and easier to maintain. Here are the rules and conventions for using #define:
1. Naming Conventions:
Symbolic names should follow the same rules as variable names but are
typically written in uppercase letters to distinguish them from regular variables.
2. Syntax Rules:
35
FPL:Unit-1 DIT,Pimpri
Here are examples of invalid #define statements and explanations for their invalidity:
Valid Examples
Here are some valid examples of defining symbolic constants using #define:
36
FPL:Unit-1 DIT,Pimpri
We may like the value of certain variables to remain constant during the execution of
a program. We can achieve this by declaring the variable with the qualifier `const` at
the time of initialization.
Syntax for `const` declaration:
Advantages
The `volatile` qualifier informs the compiler that a variable's value may be
changed at any time by external sources (outside the program).
Syntax :
This ensures that the variable's value cannot be modified by the program but
may be altered by some other process.
Here,
• volatile Keyword: Ensures the compiler checks the variable's value each time it is
accessed, accounting for possible external modifications.
• Combination with const: Prevents the program from modifying the variable, while
still allowing external changes.
38