1
UNIT-I
Introduction to Computers: Computer Systems, Computing Environments. Computer
Languages, Creating and Running Programs, Software Development, Flow charts
Number Systems: Binary, Octal, Decimal, And Hexadecimal.
Introduction to C Language: Background, C Programs, Identifiers, Data Types, variables,
Constants, Input / Output Statements
Arithmetic Operators and Expressions: Evaluating Expressions, Precedence and
Associativity of Operators, Type Conversions.
INTRODUCTION TO COMPUTERS
COMPUTER SYSTEMS
A computer is a system made of two major
components: hardware and software. The
computer hardware is the physical equipment.
The software is the collection of
programs(instructions) that allow the
hardware to do its job.
Computer Hardware:
The hardware component of the computer
system consists of five parts:
• input devices
• central processing unit (CPU)
• primary storage
• output devices
• auxiliary storage devices
The input device is usually a keyboard where programs and data entered into the computer.
Examples of other input devices include a mouse, a pen or stylus, a touch screen, or an audio
input unit.
The central instructions such are processing unit (CPU) is responsible for executing as
arithmetic calculations, comparisons among data, and movement of data inside the system.
Today’s computers may have one, two, or more CPUs.
Primary storage, also known as main memory, is a place where the programs and data are
stored temporarily during processing. The data in primary storage are erased when we turn
off a personal computer or when we log off from a time-sharing computer.
PPS UNIT-I Kiran Kumar. B
2
The output device is usually a monitor or a printer to show output. If the output is shown on
the monitor, we say we have a soft copy. If it is printed the printer, we say we have a hard
copy.
Auxiliary storage, also known as secondary storage, is used for both input and output. It is the
place where the programs and data are stored permanently. When we turn off the computer,
our programs and data remain in the secondary storage, ready for the next time we need them.
Computer Software:
Computer software is divided into two broad categories:
✓ system software
✓ application software.
This is true regardless of the hardware system architecture.
System software manages the computer resources. It provides the interface between the
hardware and the users hut does nothing to directly serve the users’ needs.
Application software, on the other hand, is directly responsible for helping users solve their
problems.
System Software:
System software consists of programs that manage the hardware resources of a computer and
perform required information processing tasks.
These programs are divided into three classes:
✓ operating system
✓ system support
✓ system development.
PPS UNIT-I Kiran Kumar. B
3
The operating system provides services such as a user interface, file and database
access, and interfaces to communication systems such as Internet protocols. The primary
purpose of this software is to keep the system operating in an efficient manner while allowing
the users access to the system.
System support software provides system utilities and other operating services.
Examples of system utilities are sort programs and disk format programs. Operating services
consist of programs that provide performance statistics for the operational staff and security
monitors to protect the system and data.
Software category, system development software, includes the language translators
that convert programs into machine language for execution, debugging tools to ensure that
the programs are errorfree, and computer-assisted software engineering (CASE) systems that
are beyond the scope
Application Software
Application software is broken into two classes:
✓ general-purpose software
✓ application-specific software.
General-purpose software is purchased from a software developer and can be used for more
than one application.
Examples of general-purpose software include word processors, database management
systems, and computer-aided design systems.
Application-specific software can be used only for its intended purpose. A general ledger
system used by accountants and a material requirements planning system used by a
manufacturing organization are examples of application-specific software. They can be used
only for the task for which they were designed; they cannot he used for other generalized
tasks.
The relationship between system and application
software is seen in Figure 1 -4. In this figure, each
circle represents an interface point. The inner core is
the hardware. The user is represented by the outer
layer. To work with the system, the typical user uses
some form of application software. The application
software in turn interacts with the operating system,
which is a part of the system software layer. The
system software provides the direct interaction with
the hardware.
Relationship between System and
Application Software
PPS UNIT-I Kiran Kumar. B
4
Computing Environments
Personal Computing Environment
In 1971, Marcian E. Hoff, working for
Intel, combined the basic elements of the
central processing unit into the
microprocessor. This first computer on a
chip was the Intel 4004 and was the
grandparent many times removed of
Intel’s current system. If we are using a
personal computer, all of the computer
hardware components are tied together in
our personal computer (or PC1 for short).
Time-Sharing Environment
Employees in large companies often work
in what is known as a time-sharing
environment. In the time-sharing
environment, many users are connected to
computers. These computers may be
minicomputers or central are often
nonprogrammable, although one or more
mainframes. The terminals they use today
we see more and more microcomputers
being used to simulate terminals. Also, in
the time-sharing environment, the output
devices (such as printers) and auxiliary
storage devices (such as disks) are shared
by all the users.
Client/Server Environment
A client/server computing environment
splits the computing function between
a central computer and users’
computers. The users are given
personal computers or workstations so
that some of the computation
responsibility can be moved from the
central computer and assigned to the
workstations. In the client/server
environment, the user’s
microcomputers or workstations are
PPS UNIT-I Kiran Kumar. B
5
called the client. The central computer, which may be a powerful microcomputer,
minicomputer, or central mainframe system, is known as the server. Because the work is now
shared between the user’s computers and the central computer, response time and monitor
display are faster and the users are more productive
Distributed Computing
A distributed computing environment provides a seamless integration of computing functions
between different servers and clients. The Internet provides connectivity to different servers
throughout the world.
For example, eBay uses several computers to provide its auction service.
This environment provides a reliable, scalable, and highly available network
Computer Languages
To write a program for a computer, we must use a computer language. Over the years
computer languages have evolved from machine languages to natural languages.
PPS UNIT-I Kiran Kumar. B
6
Machine Languages
In the earliest days of computers, the only programming languages available were machine
languages. Each computer has its own machine language, which is made of streams of O’s
and 1’s. Program 1-1 shows an example of a machine language. This program multiplies two
numbers and prints the results
Symbolic Languages
It became obvious that few programs would be written if programmers continued to work in
machine language. In the early 1950s, Admiral Grace Hopper, a mathematician and naval
officer, developed the concept of a special computer program that would convert programs
into machine language. These early programming languages simply mirrored the machine
languages using symbols, or mnemonics, to represent the various machine language
instructions. Because they used symbols, these languages were known as symbolic languages.
PPS UNIT-I Kiran Kumar. B
7
Because a computer does not
understand symbolic language, it
must be translated to the machine
language. A special program called
an assembler translates symbolic
code into machine language. Because
symbolic languages had to he
assembled into machine language,
they soon became known as assembly
languages. This name is still used
today for symbolic languages that
closely represent the machine
language of their computer.
High-Level Languages
Although symbolic languages greatly improved programming efficiency, they still required
programmers to concentrate on the hardware that they were using. Working with symbolic
languages was also very tedious because each machine instruction had to be individually
coded. The desire to improve programmer efficiency and to change the focus from the
computer to the problem being solved led to the development of high-level languages.
High-level languages are portable to many different computers, allowing the programmer to
concentrate on the application problem at hand rather than the intricacies of the computer.
High-level languages are designed to relieve the programmer from the details of the assembly
language. High-level languages share one thing with symbolic languages, however: They
must be converted to machine language. The process of converting them is known as
compilation.
The first widely used high-level language, FORTRAN was created by John Backus and an
IBM team in 1957; it is still widely used today in scientific and engineering applications.
C, with its standard libraries, is considered a high-level language used for system software
and new application code
PPS UNIT-I Kiran Kumar. B
8
Creating and Running Programs
Computer hardware understands a program only if it is coded in its machine language. Here
is the procedure for turning a program written in C into machine language. There are four
steps in this process:
(1) writing and editing the program
(2) compiling the program
(3) linking the program with the required library modules
(4) executing the program
Writing and Editing Programs:
The software used to write programs is known as a text editor. A text editor helps us enter,
change, and store character data. After we complete a program, we save our file to disk. I his
file will be input to the compiler; it is known as a source file.
Compiling Programs:
The code in a source file stored on the disk must be translated into machine language. This is
the job of the compiler. The C compiler is actually two separate programs: the preprocessor
and the translator.
PPS UNIT-I Kiran Kumar. B
9
The preprocessor reads the source code and prepares it for the translator. While preparing the
code, it scans for special instructions known as preprocessor commands. These commands
tell the preprocessor to look for special code libraries, make substitutions in the code, and in
other ways prepare the code for translation into machine language. The result of
preprocessing is called the translation unit.
After the preprocessor has prepared the code for compilation, the translator does the actual
work of converting the program into machine language. The translator reads the translation
unit and writes the resulting object module to a file that can then be combined with other
precompiled units to form the final program. An object module is the code in machine
language. Even though the output of the compiler is machine language code, it is not yet
ready to run; that is, it is not yet executable because it does not have the required C and other
functions included.
Linking Programs:
C program is made up of many functions. The linker assembles all of these functions, into
final executable program.
Executing Programs:
Once the program has been linked, it is ready for execution. To execute a program, use an
operating system command, such as run, to load the program into primary memory and
execute it. Getting the program into memory is the function of an operating system program
known as the loader. It locates the executable program and reads it into memory. When
everything is loaded, the program takes control and it begins execution.
System Development
This critical process determines the overall quality and success of the program. Carefully
designing each program using good structured development techniques, then the programs
will be efficient, error-free, and easy to maintain.
System Development Life Cycle
Today’s large-scale, modern programming projects are built using a series of interrelated
phases commonly referred to as the system development life cycle. Although the exact
number and names of the phases differ depending on the environment, there is general
agreement as to the steps that must he followed. Whatever the methodology, however,
today’s software engineering concepts require a rigorous and systematic approach to software
development. One very popular development life cycle is the waterfall model. Depending on
the company and the type of software being developed, this model consists of between five
and seven phases.
PPS UNIT-I Kiran Kumar. B
10
The waterfall model starts with systems requirements. In this phase, the systems analyst
defines requirements that specify what the proposed system is to accomplish. The
requirements are usually stated in terms that the user understands.
The analysis phase looks at different alternatives from a system’s point of view.
The design phase determines how the system will be built. In the design phase, the functions
of the individual programs that will make up the system are determined and the design of the
files and/or the databases is completed.
Finally, in the fourth phase, code, we write the programs.
After the programs have been written and tested to the programmer’s satisfaction, the project
proceeds to system test. All of the programs are tested together to make sure the system
works as a whole.
The final phase, maintenance, keeps the system working once it has been put into
production.
Program Development
Program Development is a multistep process that requires to
✓ Understand the problem
✓ Develop a solution
✓ Write the program
✓ Test it.
When an assignment is given to develop a program, program requirements statement and the
design of any program interfaces will be provided and also receive an overview of the
complete project to understand how our part fits in to the whole. The job is to determine how
to take the inputs given and convert them into the outputs that have been specified. This is
known as program design.
PPS UNIT-I Kiran Kumar. B
11
Simple problem to help understand the idea of how the process works:
Ex: Calculate the square footage of a house
Understand the Problem
The first step in solving any problem is to understand it. Begin by reading the requirements
statement carefully. When they are fully understood, review the understanding with the user
and the systems analyst.
Example questionnaire on simple requirements
➢ What is the definition of square footage?
➢ How is the square footage going to be used?
o for insurance purposes?
o to paint the inside or outside of the house?
o to carpet the whole house?
➢ Is the garage included?
➢ Are closets and hallways included?
Develop the Solution
Once the problem is fully understood and have clarified all the questions, then develop the
solution.
Three tools will help in this task:
(1) structure charts
(2) pseudocode
(3) flowcharts.
Only two of them are used generally—a structure chart and either pseudocode or a flowchart.
A Structure chart, also known as a hierarchy chart, shows the functional flow through the
program. Large programs are complex structures consisting of many interrelated parts; thus,
they may be carefully laid out. This shows how the program is broken down into logical
steps; each step will be a separate module. The structure chart shows the interaction between
all the parts (modules) of the program.
Looking again at the problem to calculate the square footage of a house, assume the
following answers to the questions raised in the previous section.
1. The purpose of calculating the square footage is to install new floor covering.
2. Only the living space will be carpeted. The garage and closets will not he considered.
3. The kitchen and bathrooms will he covered with linoleum; the rest of the house is to be
carpeted.
PPS UNIT-I Kiran Kumar. B
12
Pseudocode is part English, part program logic. Its purpose is to describe, in precise
algorithmic detail, what the program being designed is to do. This requires defining the steps
to accomplish the task in sufficient detail so that they can he converted into a computer
program type of precise logic
PPS UNIT-I Kiran Kumar. B
13
A flowchart is a program design tool in which standard graphical symbols arc used to
represent the logical flow of data through a function.
Write the Program
Write a program, by starting with the top box on the structure chart and work on the way to
the bottom.
Test the Program
Program testing can be a very tedious and time-consuming part of program development.
There are two types of testing: blackbox and whitebox.
Blackbox testing is the concept of testing the program without knowing what is inside it,
without knowing how it works. This is done by System test engineer and the user.
Whitebox testing assumes that the tester knows everything about the program. This is the
responsibility of the programmer.
PPS UNIT-I Kiran Kumar. B
14
Number System
A number system relates quantities and symbols. In digital system how information is
represented is key and there are different radices, i.e. number bases, that a numbering system
can use. 1.1 Digital computer Any class of devices capable of solving problems by processing
information in discrete form. It operates on data, including letters and symbols, that are
expressed in binary form i.e using only two digits 0 and 1. The block diagram of digital
computer is given below:
The memory unit stores programs as well as input, output and intermediate data. The
processor unit performs arithmetic and other data processing tasks as specified by the
[Link] control unit supervises the flow of information between various units. The
program and data prepared by the user are transferred into the memory unit by means of an
input device such as punch card reader (or) tele typewriter. An output device, such as printer,
receives the result of the computations and the printed results are presented to the user.
1.2 Number Representation:
Control Unit Processor (or) Arithmetic unit Storage (or) Memory Unit Input Devices and
Control Output Devices and Control It can have different base values like: binary (base-2),
octal (base-8), decimal (base 10) and hexadecimal (base 16), here the base number represents
the number of digits used in that numbering system. As an example, in decimal numbering
system the digits used are: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. Therefore the digits for binary are: 0
and 1, the digits for octal are: 0, 1, 2, 3, 4, 5, 6 and 7. For the hexadecimal numbering system,
base 16, the digits are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
Binary numbers
Numbers that contain only two digit 0 and 1 are called Binary Numbers. Each 0 or 1 is called
a Bit, from binary digit. A binary number of 4 bits is called a Nibble. A binary number of 8
bits is called a Byte. A binary number of 16 bits is called a Word on some systems, on others
PPS UNIT-I Kiran Kumar. B
15
a 32-bit number is called a Word while a 16-bit number is called a Halfword. Using 2 bit 0
and 1 to form a binary number of 1 bit, numbers are 0 and 1 a binary number of 2 bit,
numbers are 00, 01, 10, 11 a binary number of 3 bit, such numbers are 000, 001, 010, 011,
100, 101, 110, 111 a binary number of 4 bit, such numbers are 0000, 0001, 0010, 0011, 0100,
0101, 0110, 0111, 1000, 1001, 1010, 1011,
1100,1101,1110,1111 Therefore , using n bits there
are 2n binary numbers of n bits Each digit in a binary
number has a value or weight. The LSB has a value of
1. The second from the right has a value of 2, the next
4, etc.,
The binary equivalent for some decimal numbers are given below.
Number Base Conversions
3.1 Conversion of decimal number to any number system
Step 1 convert the integer part by doing successive division using the radix of asked number
systems.
Step 2 convert the fractional part by doing successive multiplication using radix of asked
number system
3.2 Conversion of decimal to binary number system The radix of asked number system is 2
Convert 8710 to ( )2
(1010111)2
Convert (14.625)10 decimal number to binary number
PPS UNIT-I Kiran Kumar. B
16
1st Multiplication Iteration
Multiply 0.625 by 2 0.625 x 2 = 1.25(Product) Fractional part=0.25 Carry=1 (MSB)
2nd Multiplication Iteration
Multiply 0.25 by 2 0.25 x 2 = 0.50(Product) Fractional part = 0.50 Carry = 0
3rd Multiplication Iteration
Multiply 0.50 by 2 0.50 x 2 = 1.00(Product) Fractional part = 1.00 Carry = 1 (LSB)
(101)2
The binary number of (16.625)10 is (1110.101)2
3.3 Conversion of decimal to octal number system
The radix of asked number system is 8
Convert (264)10 decimal number to octal number
(410)8
The octal number of (264)10 is (410)8
PPS UNIT-I Kiran Kumar. B
17
3.4 Conversion of decimal to Hexadecimal number system
The radix of asked number system is 16
Convert (1693)10 decimal number to Hexadecimal number
1693/16 = 105 Reminder (13) D (LSB)
105/16 = 6 Reminder 9
6/16 = 0 Reminder 6 (MSB)
(1693)10 (69D)16
Convert (1693.0628)10 decimal fraction to hexadecimal fraction (?)16
1693/16 = 105 Reminder (13) D (LSB)
105/16 = 6 Reminder 9
6/16 = 0 Reminder 6 (MSB)
(69D)
Multiply 0.0628 by 16
0.0628 x 16 = 1.0048(Product) Fractional part=0.0048 Carry=1 (MSB)
Multiply 0.0048 by 16
0.0048 x 16 = 0.0768(Product) Fractional part = 0.0768 Carry = 0
Multiply 0.0768 by 16
0.0768 x 16 = 1.2288(Product) Fractional part = 0.2288 Carry = 1
Multiply 0.2288 by 16
0.2288 x 16 = 3.6608(Product) Fractional part = 0.6608 Carry = 3 (LSB) (.1013)
(1693.0628)10 = (69D.1013)16
3.5 Conversion of any number system to decimal number system
In general the numbers can be represented as
Where
n= number in decimal
A= digit
r= radix of number system
n= The number of digits in the integer portion of number
m= the number of digits in the fractional portion of number
PPS UNIT-I Kiran Kumar. B
18
PPS UNIT-I Kiran Kumar. B
19
= 0. 6154782
PPS UNIT-I Kiran Kumar. B
20
Introduction to the C Language
Background
C is a structured programming language. It is considered a high-level language
because it allows the programmer to concentrate on the problem at hand and not worry about
the machine that the program will be using. While many languages claim to be machine
independent, C is one of the closest to achieving that goal. That is another reason why it is
used by software developers whose applications have to run on many different hardware
platforms.
C, like most modern languages, is derived from ALGOL, the first language to use a
block structure. ALGOL never gained wide acceptance in the United States, but it was widely
used in Europe.
In 1967, Martin Richards
developed a language he called Basic
Combined Programming Language, or
BCPL. Ken Thompson followed in 1970
with a similar language he simply called B.
B was used to develop the first version of
UNIX, one of the popular network
operating systems in use today. Finally, in
1972, Dennis Ritchie developed C, which
took many concepts from ALGOL, BCPL.
What is known as traditional C is
this 1972 version of the language, as
documented and popularized in a 1978 hook by Brian W. Kernighan and Dennis Ritchie. In
1983, the American National Standards Institute (ANSI) began the definition of a standard
for C. It was approved in December 1989. In 1990, the International Standards Organization
(ISO) adopted the ANSI standard. This version of C is known as C89.
In 1995, minor changes were made to the standard. I bis version is known as C95. A
much more significant update was made in 1999. The changes incorporated into the standard,
now known as C99, are summarized in the following list.
PPS UNIT-I Kiran Kumar. B
21
1. Extensions to the character type to support non-English characters
2. A Boolean type
3. Extensions to the integer type
4. Inclusion of type definitions in the for statement.
5. Addition of imaginary and complex types
6. Incorporation of the C++ style line comment (//)
C Programs
It’s time to write first C program!
Structure of a C Program
Every C program is made of one or more preprocessor commands, a global
declaration section, and one or more functions. The global declaration section comes at the
beginning of the program and they are visible to all parts of the program.
The work of the program is carried out by its functions, blocks of code that
accomplish a task within a program. One, and only one, of the functions must be named
main. The main function is the starting point for the program. All functions in a program,
including main, are divided into two sections: the
declaration section and the statement section. The
declaration section is at the beginning of the
function. It describes the data that you will be using
in the function. Declarations in a function are
known as local declarations (as opposed to global
declarations) because they function that contains
them.
The statement section follows the
declaration section. It contains the instructions to
the computer that cause it to do something, such as
add two numbers. In C, these instructions are
written in the form of statements, which gives us the
name for the section.
First C Program: (The Greeting Program)
PPS UNIT-I Kiran Kumar. B
22
Preprocessor Commands
The preprocessor commands come at the beginning of the program. All preprocessor
commands start with a pound sign ( # ); this is just one of the rules of C known as its syntax.
Preprocessor commands can start in any column, but they traditionally start in column 1 .
The preprocessor command tells the compiler to include the standard input/output library file
in the program. You need this library file to print a message to the terminal. Printing is one of
the input/output processes identified in this library. The complete syntax for this command is
shown below.
#include
main
The executable part of program begins with the function main, which is identified by the
function header shown below. In function definition int says that the function will return an
integer value to the operating system, that the function's name is main and that it has no
parameters (the parameter list is void). Note that there is no punctuation alter the function
header.
int main(void)
Comments
Comments are merely internal
program documentation. The
compiler ignores these comments
when it translates the program into
executable code. To identify a
comment, C uses two different
formats: block comments and line
comments
Block Comment: A block
comment is used when the
comment will span several lines.
Line Comment: The second format, the line comment, uses two slashes (//) to identify a
comment.
PPS UNIT-I Kiran Kumar. B
23
Identifiers
One feature present in all computer languages is the identifier. Identifiers allow us to name
data and other objects in the program. Each identified object in the computer is stored at a
unique address. If we didn’t have identifiers that we could use to symbolically represent data
locations, we would have to know and use object’s addresses. Instead, we simply give data
identifiers and let the compiler keep track of where they are physically located.
Different programming languages use different syntactical rules to form identifiers. In C, the
rules for identifiers are very simple.
• The only valid name symbols are the capital letters A through Z, the lowercase letters
a through z, the digits 0 through 9, and the underscore.
• The first character of the identifier cannot he a digit.
• Name cannot be a keywords.
Types
A type identifies a set of values and a set of operations that can be applied on those values.
Void Type – designated by keyword void, has no values and no operations.
PPS UNIT-I Kiran Kumar. B
24
Integral Type
The C language has three integral types: Boolean, Integral types cannot contain a fraction
character, and integer.
Floating-Point Types
The C standard recognizes three floating-point types: real, imaginary, and complex.
Real - The real type holds values that consist of an integral and a fractional part, such as
43.32. The C language supports three different sizes of real types: flout, double, and long
double.
Imaginary Type - An imaginary number is a real number multiplied y the square root of -1.
The imaginary type, like the real type, can e of three different sizes: float imaginary, double
imaginary and long double imaginary.
Complex – It is a combination of real and an imaginary number. The complex type, like the
real type, can be of three different sizes: float complex, double complex, and long long
complex.
PPS UNIT-I Kiran Kumar. B
25
Variables
Variables are named memory locations that have
character, which is inherited from their type. The type
determines the values that a variable may contain the
operations that may he used with its values.
A variable’s type can he any of the data types, such as
character, integer, or real.
The one exception to this
rule is the type void: a
variable cannot be type
void
Variable Initialization
To initialize a variable when it is defined, the identifier is followed by the assignment
operator and then the initializer, which is the value the variable is to have when the function
starts. This simple initialization
format is shown below
Ex: int count = 0;
int count, sum = 0;
int sum = 0, count = 0;
int sum=0;
int count=0;
PPS UNIT-I Kiran Kumar. B
26
Constants
Constants are data values that cannot be changed during the execution of a program.
Input/Output
Streams
In C, data is input to and output from a stream. A stream is a source of or destination for data.
It is associated with a physical device, such as a terminal, or with a file stored in auxiliary
memory.
C uses two forms of streams: text and binary. A text stream consists of a sequence of
characters divided into lines with each line terminated hv a newline (\n). A binary stream
consists of a sequence of data values such as integer, real, or complex using their memory
representation.
PPS UNIT-I Kiran Kumar. B
27
Output Formatting: printf
The output formatting function is printf . The printf function takes a set of data values,
converts them to a text stream using formatting instructions contained in a format control
string, and sends the resulting text stream to the standard output (monitor).
Input Formatting: scanf
The standard input formatting function in C is scanf (scan formatting). This function takes a
text stream from the keyboard, extracts and formats data from the stream according to a
format control string, and then stores the data in specified program variables.
PPS UNIT-I Kiran Kumar. B
28
Input formatting Rules:
PPS UNIT-I Kiran Kumar. B
29
PPS UNIT-I Kiran Kumar. B
30
PPS UNIT-I Kiran Kumar. B
31
Define Constants
Pgm : Calculate a Circle’s Area and Circumference
#include<stdio.h>
#define PI 3.1416
int main(void)
{
//Local Declarations
float circ;
float area;
float radius;
//Statements
printf(“\n Please enter the value of the radius”);
scanf(“%f”, &radius)
circ = 2 * PI * radius;
area = PI * radius * radius;
printf(“\n Radius : %10.2f” , radius);
printf(“\n Circumference : %10.2f” , circ);
printf(“\n Area : %10.2f” , area);
return 0;
} //main
PPS UNIT-I Kiran Kumar. B
32
EXPRESSIONS
An expression is a sequence of operands and operators that reduces to a single value.
Expressions can be simple or complex. An operator is a syntactical token that requires an
action to be taken. An operand is an object on which an operation is performed; it receives an
operator’s action.
A simple expression contains only one operator.
2+5 is a simple expression whose value is 7.
A complex expression contains more than one operator.
2+7*5
To evaluate a complex expression, reduce it to a series of simple expressions. The order in
which the operators in a complex expression are evaluated is determined by a set of priorities
known as precedence; the higher the precedence, the earlier the expression containing the
operator is evaluated.
• A primary expression consists of only one operand with no operator.
• The postfix expression consists of one operand followed by one operator
• In prefix expressions, the operator comes before the operand
• A unary expression, like a prefix expression, consist of one operator and one
operand
• The sizeof operator tells the size, in bytes, of a type or a primary expression.
• Binary expressions are formed by an operand-operator-operand combination
If two operators with the same precedence occur in a complex expression, another attribute of
an operator, its associativity, takes control. Associativity is the parsing direction used to
evaluate an expression.
Precedence and Associativity
Precedence is used to determine the order in which different operators in a complex
expression are evaluated. Associativity is used to determine the order in which operators with
the same precedence are evaluated in a complex expression.
PPS UNIT-I Kiran Kumar. B
33
PPS UNIT-I Kiran Kumar. B
34
Type Conversion
Implicit Type Conversion When the types of the two operands in a binary expression are
different, C automatically converts one type to another. This is known as implicit type
conversion.
Promotion
There is normally no problem with promotion. The rank of the right expression is elevated to
the rank of the left variable. The value of the expression is the value of the right expression
after the promotion. The following examples demonstrate some simple promotions.
Ex:
#include <stdio.h>
int main() {
int a = 10;
float b = 5.5;
float result;
// Implicit conversion from int to float
result = a + b; // 'a' (int) is implicitly converted to float during the addition
printf("Result of a + b: %f\n", result);
return 0;
}
PPS UNIT-I Kiran Kumar. B
35
Explicit Type Conversion (Cast)
Rather than let the compiler implicitly convert data, convert data from one type to another
using explicit type conversion. Explicit type conversion uses the unary cast operator, which
has a precedence of 14. To cast data from one type to another, we specify the new type in
parentheses before the value we want converted. For example, to convert an integer, a, to
afloat, we code the expression shown below.
(float) a;
Example :
#include <stdio.h>
int main() {
int a = 10, b = 3;
float result;
// Explicit conversion from int to float
result = (float)a / b; // 'a' is explicitly converted to float before division
printf("Result of a / b: %f\n", result);
return 0;
}
PPS UNIT-I Kiran Kumar. B