Unit I - Basic Concepts of Programming
Unit I - Basic Concepts of Programming
STRUCTURE
1.1 Introduction
1.3 Programming In C
1.3.2 Tokens
1.3.4 Operators
1.3.5 Expression
1.4 Summary
1.5 Keywords
1.8 References
3
Describe the problem-solving techniques
Understand the features of c programming
Describe the various data types used in c.
1.1 INTRODUCTION
Another reason to study algorithms is the usefulness in developing analytical skills. After
that, algorithms can be seen as special kinds of solutions for the problems, but precisely
defined procedures for getting answers. Consequently, specific algorithm design techniques
can be interpreted and involved course the precision inherently imposed by algorithmic
thinking limits the kinds of problems than can be solved with an algorithm.
In the 1930s, before the advent of computers, mathematicians worked very actively to
formalize and study the notation of simple instructions were given for solving a problem or
computer as a solution. Various formal modes of computation were desired and investigated.
Much of the emphasis in the early work in this field computability theory was on describing
and characterizing those problems that could be solved algorithmically and on exhibiting
some problems that could not be solved. One of the important negative results was
insolvability of the “halting problem”. The halting problem is to determine whether an
arbitrary given algorithm (or computer program) will eventually halt (rather than, say get into
an infinite loop) while working on a given input.
Program Design
4
Overview
Program design consists of the steps a programmer should do before they start coding the
program in a specific language. These steps when properly documented will make the
completed program easier for other programmers to maintain in the future. There are three
broad areas of activity:
If you are working on a project as one of many programmers, the system analyst may have
created a variety of documentation items that will help you understand what the program is to
do. These could include screen layouts, narrative descriptions, documentation showing the
processing steps, etc. If you are not on a project and you are creating a simple program you
might be given only a simple description of the purpose of the program. Understanding the
purpose of a program usually involves understanding its:
• Inputs
• Processing
• Outputs
This IPO approach works very well for beginning programmers. Sometimes, it might help to
visualize the program running on the computer. You can imagine what the monitor will look
like, what the user must enter on the keyboard and what processing or manipulations will be
done.
At first, you will not need a hierarchy chart because your first programs will not be complex.
But as they grow and become more complex, you will divide your program into several
modules (or functions).
The first modeling tool you will usually learn is pseudocode. You will document the logic or
algorithm of each function in your program. At first, you will have only one function, and
thus your pseudocode will follow closely the IPO approach above.
5
There are several methods or tools for planning the logic of a program. They include:
flowcharting, hierarchy or structure charts, pseudocode, HIPO, Nassi-Schneiderman charts,
Warnier-Orr diagrams, etc. Programmers are expected to be able to understand and do
flowcharting and pseudocode. These methods of developing the model of a program are
usually taught in most
computer courses. Several standards exist for flowcharting and pseudocode and most are very
similar to each other. However, most companies have their own documentation standards and
Later in your programming career, you will learn about using application software that helps
create an information system and/or programs. This type of software is called Computer-
Aided Software Engineering (CASE).
Understanding the logic and planning the algorithm on paper before you start to code is a
very important concept. Many students develop poor habits and skipping this step is one of
them.
Test data consists of the programmer providing some input values and predicting the outputs.
This can be quite easy for a simple program and the test data can be used to check the model
to see if it produces the correct results.
4. Finiteness: The algorithm terminates; that is, it terminates after finite number of steps.
6
5. Correctness: The produced output by the algorithm is correct.
Notation of Algorithm
Problem
Problem
i/p o/p
Computer
FIGURE 1.1
EXAMPLE
Consider the given algorithm to find the largest of three numbers x, y and z.
print y
else
print z
Thus, in the given above algorithm, consider the first step is to start the program, the second
step to read the values of x, y and z and after that compare the first number with second
number and also compare the first number with third number, if the condition is true then
print the value of first number else compare the second number with third number if the
condition is satisfied then display the second number otherwise display the third number.
7
The given figure shows the sequence of steps in arithmetic problem solving
Design an algorithm
Prove Correctness
The first step in algorithm problem solving is to understand the complexity of the problem
given. The fundamental importance of both algorithms and data structures for computer
programming is very useful algorithm design techniques (or “strategy” or “paradigm”) is a
general approach for solving problems algorithmically that is applicable to a variety of
problems from different area of computing. There are two methods used for designing an
algorithm.
1. Pseudocode
2. Flowchart
In the second approach for specifying algorithms was a flowchart, a method of expressing an
algorithm by a collection of connected geometric shapes containing descriptions of the
algorithm’s steps. Once an algorithm has been specified, you have to prove its correctness.
8
That is, you have to prove that the algorithm yields a required result for every legitimate
input in finite amount of time.
After correctness, by far the most important is efficiency. In fact, there are two kinds of
algorithmic efficiency: time efficiency and space efficiency. Time efficiency indicates how
fast the algorithm runs; space efficiency indicates how much extra memory the algorithm
needs. Another desirable characteristic of an algorithm is simplicity and generality. Two
issues have; in generality of the problem the algorithm solves and the range of inputs it
accepts. On the first issue, note that it is sometimes easier to design an algorithm for a
problem posed in more general terms. Consider for example, the problem for determining
whether two integers are relatively prime. Most algorithms are designed to be ultimately
implemented as a computer programs.
To create an algorithm, it may never fully optimized. One of the major goals of an algorithm
is to use variety of designing techniques to yield good algorithms. By using dynamic
programming techniques, it is used to devise an algorithm (i.e., devise means divided) in a
good manner.
After devising the algorithm, the next field is validating the algorithms. Thus, computer will
produce correct result for all possible legal inputs.
9
3. HOW TO ANALYSE ALGORITHMS
Testing a program consists of two phases: debugging and profiling (or performance
measurement). Debugging means to identify errors in a program. Performance measurement
is the process of executing a correct program, it measures time and space it takes to compute
the results.
FLOWCHART
A flow chart is a graphical or symbolic representation of a process. Each step in the process is
represented by a different symbol and contains a start description of the process.
The following symbols that are commonly used in flowcharts they are,
1. Oval / Terminator: Ovals indicates both the start and end of the process.
10
2. Box / Process: A Rectangular flow shape indicates the activity in the process.
3. Diamand / Decision: Diamonds indicates the decision point, such as yes/no or on/off or
go/not go.
4. Circle / Connector: A circle indicates the particular step is connected to another page or
part of the flowchart.
5. Triangle / Data: A triangle indicates data input or output (I/O) for a process.
Start / end
1 Oval/Terminator Start/end
Process
2 Box/Process Activity in the Process
N
D
3 Diamond/Decision Indicating a Decision
Point
Y
11
5 Data Triangle/Data Data Input/Output(I/O) for
a Process
Document
6 Document Indicating a Document or
Report
Predefined
8 Process Predefined Process Invoke a Subroutine or
Interrupt Program
The given table represents symbol, symbol name and function of flowchart.
The following flow chart shows the symbolic representation of flow diagram.
Start Terminator
Process
12
No
Decision
Yes
Data
Document
Stop Terminator
2. Flowchart symbols have an entry point on the top of the symbol only. The exit point for all
flowchart symbols is on the bottom except for decision making
2. from the bottom of the page to the top of the same page
Advantages of Flowchart
13
Communication: Flowchart is used for better way of communication in all connections.
Efficient Coding: The flowcharts act as a guideline during the system analysis and
development phase.
Efficient Program Maintenance: The maintenance of operating program Is an easy way for
drawing the flowchart.
Limitations of Flowchart
1. Complex Logic: The program logic is very complicated; in this manner drawing flowchart
is difficult
2. Alterations And Modifications: Alteration and modifications cannot be made and hence
flowchart is very complex process
A divide and conquer strategy process is given below. If the problem is small, it’s solved
directly. If the problem is large, it’s divided into two or more parts called subprograms. Each
subprogram is solved after solutions to the sub problems are combined into a solution to the
original problem. The divide and conquer strategy are used in same process that is to solve
the sub problems are further divided into several sub problems and so on. The solutions to the
various sub problems are the combined into a solution to the original problem. Recursion is a
method it is used to solve a subprogram. An example is array. An array of two or more
elements can be sorted by using a divide and conquer strategy. In this manner to use merge
sort method for the sorted array of elements where it combines into original elements.
14
DIVIDE: Divide the n-element sequence to be sorted into two subsequences of n/2 elements
each.
CONQUER: Sort the two subsequences recursively that is by using merge sort again on the
Subsequences. Merge sorts on latter.
A COMBINE: Merge the two sorted subsequences to produce the sorted answer.
To write pseudocode for to prepare a student mark sheet processing using control statement
Step 2: Initialize the variables roll no, stud name, m1, m2, total, Average and result
Step 3: Read the values for roll no, stud name, m1, m2
else
end if
print result=”fail”
1.3 PROGRAMMING IN C
C is a programming language developed AT & T’s Bell Laboratories of USA in 1972. It was
designed and written by a man named Dennis Ritchie. In the late seventies C began to replace
the more familiar languages of that time like PL/I, ALGOL, etc.
15
ANSI C standard emerged in the early 1980s, this book was split into two titles: The original
was still called Programming in C, and the title that covered ANSI C was called
Programming in ANSI C. This was done because it took several years for the compiler
vendors to release their ANSI C compilers and for them to become ubiquitous. It was initially
designed for programming UNIX operating system. Now the software tool as well as the C
compiler is written in C. Major parts of popular operating systems like Windows, UNIX,
Linux is still written in C. This is because even today when it comes to performance (speed of
execution) nothing beats C. Moreover, if one is to extend the operating system to work with
new devices one needs to write device driver programs. These programs are exclusively
written in C. C seems so popular is because it is reliable, simple and easy to use. often heard
today is – “C has been already superseded by languages like C++, C# and Java.
Program
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 on 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.
16
Fig 1.3 Steps in Learning C
Low level languages are machine level and assembly level language. In machine level
language computer only understand 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 also
error prone. The 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. Although language is bit easier, programmer has
to know low level details related to low level language. In the assembly level language, the
data are stored in the computer register, which varies for different computer. Hence it is not
portable.
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
17
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 in to machine level language.
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. If it
checks error, statement by statement and hence of take more time.
The process of editing, compiling, running, and debugging programs is often managed by a
single integrated application known as an Integrated Development Environment, or IDE for
short. 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.
On Mac OS X, CodeWarrior and XCode are two IDEs that are used by many programmers.
Under Windows, Microsoft Visual Studio is a good example of a popular IDE. Kylix is a
popular IDE for developing applications under Linux. Most IDEs also support program
development in several different programming languages in addition to C, such as C# and
C++.
1) Comment line
2) Preprocessor directive
18
4) main function ()
{
Comment line
/*……………………………..*/
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.
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
&declaration of system defined function such as printf( ), scanf( ), pow( ) etc. Generally
printf () function used to display and scanf () function used to read value
Global 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 :
19
main ()
It is the user defined function and every function has one main () function from where
actually program is started and it is enclosing 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.
Syntax: main ()
……..
……..
……..
The main () function return value when it declared by data type as int main ()
return 0
The main function does not return any value when void (means null/empty) as void main
(void) or void main ()
Output: C language
The program execution starts with opening braces and end with closing brace.
And in between the two braces declaration part as well as executable part is mentioned. And
at the end of each line, the semi-colon is given which indicates statement termination.
20
{
computer program developed in the C programming language and the typical Unix
commands that would be entered from the command line.
Step 1: The program that is to be compiled is first typed into a file on the computer system.
There are various conventions that are used for naming files, typically be any name
provided the last two characters are “.c” or file with extension.c. So, the file name prog1.c
might be a valid filename for a C program. A text editor is usually used to enter the C
program into a file. For example, vi is a popular text editor used on Unix systems. The
program that is entered into the file is known as the source program because it represents the
original form of the program expressed in the C language.
Step 2: After the source program has been entered into a file, then proceed to have it
compiled. The compilation process is initiated by typing a special command on the system.
When this command is entered, the name of the file that contains the source program must
also be specified. For example, under Unix, the command to initiate program compilation is
called cc. If we are using the popular GNU C compiler, the command we use is gcc.
In the first step of the compilation process, the compiler examines each program
statement contained in the source program and checks it to ensure that it conforms to the
syntax and semantics of the language. If any mistakes are discovered by the compiler during
this phase, they are reported to the user and the compilation process ends right there. The
21
errors then have to be corrected in the source program (with the use of an editor), and the
compilation process must be restarted. Typical errors reported during this phase of
compilation might be due to an expression that has unbalanced parentheses (syntactic error),
or due to the use of a variable that is not “defined” (semantic error).
Step 3: When all the syntactic and semantic errors have been removed from the program, the
compiler then proceeds to take each statement of the program and translate it into a “lower”
form that is equivalent to assembly language program needed to perform the identical task.
Step 4: After the program has been translated the next step in the compilation process is to
translate the assembly language statements into actual machine instructions. The assembler
takes each assembly language statement and converts it into a binary format known as object
code, which is then written into another file on the system. This file has the same name as the
source file under Unix, with the last letter an “o” (for object) instead of a “c”.
Step 5: After the program has been translated into object code, it is ready to be linked. This
process is once again performed automatically whenever the cc or gcc command is issued
under Unix. The purpose of the linking phase is to get the program into a final form for
execution on the computer.
If the program uses other programs that were previously processed by the compiler, then
during this phase the programs are linked together. Programs that are used from the system’s
program library are also searched and linked together with the object program during this
phase.
The final linked file, which is in an executable object code format, is stored in another file on
the system, ready to be run or executed. Under Unix, this file is called a.out by default.
Under Windows, the executable file usually has the same name as the source file, with the c
extension replaced by an exe extension.
22
Step 6: To subsequently execute the program, the command a.out has the effect of loading
the program called a.out into the computer’s memory and initiating its execution.
When the program is executed, each of the statements of the program is sequentially executed
in turn. If the program requests any data from the user, known as input, the program
temporarily suspends its execution so that the input can be entered. Or, the program might
simply wait for an event, such as a mouse being clicked, to occur. Results that are displayed
by the program, known as output, appear in a window, sometimes called the console. If the
program does not produce the desired results, it is necessary to go back and reanalyze the
program’s logic. This is known as the debugging phase, during which an attempt is made to
remove all the known problems or bugs from the program. To do this, it will most likely be
necessary to make changes to original source program.
23
Fig 1.4 Unix Command
24
/* Simple program to add two numbers */
int v1, v2, sum; //v1, v2, sum are variables and int is data type declared v1 = 150;
v2 = 25;
sum = v1 + v2;
printf ("The sum of %i and %i is= %i\n", v1, v2, sum); return 0;
Output:
1. Portability
2. Flexibility
3. Wide acceptability
1. PORTABILITY
Program written in these languages can be executed in different computers and operating
systems if compilers are available for these systems. The ability of a program is to run in
different environments. A different environment refers to different computers, operating
systems or different compilers. Machine language or assembly language various from
computer to computer and hence programs written in these languages are not portable can be
termed as most portable language.
25
2. FLEXIBILITY
C combines the convenience and portable nature of a high-level language with the flexibility
of a low-level language.
3. WIDE ACCEPTABILITY
This language is suitable for projects both at the system-level (building operating system or
compilers) and at the application level (building graphical user interfaces).
In C, it can support set of control and flow structures such as if…statement, select case
statement.
Programs written in these languages can be executed using set of operators and expressions
such as arithmetic operators, logical operators, bitwise and shift operators etc.
1.3.2 Tokens
The C character set consists of upper- and lower-case alphabets, digits, special characters and
white spaces. The alphabets and digits are together called as alphanumeric characters.
1. Letters
2. Digits
3. Special Characters
4. White Spaces
26
(1) Letters
(2) DIGITS
Blank space new line carriage returns form feed horizontal tab and vertical tab.
Individual words and punctuation marks are called tokens. In “C Programming” language
smallest individual units are called C tokens.
27
C TOKENS
28
CONSTANTS
In C programming language the fixed values do not change during the execution of a
program. In C supports several types of constants are listed below,
Constants
Integer Constants
Integer constants refer to a sequence of digits. In integer constants can be specified in three
ways,
1. Decimal Numbers
2. Octal Numbers
3. Hexadecimal Numbers
For example, 25 is a decimal number in base 10. In octal numbers are specified with a
leading zero, rest of the digits consists of those lying between 0 and 7. For example,
0125.The hexadecimal numbers are specified with ox or OX the beginning. The digits that
follow 0x must be numbers in the range 0-9 or one of the letters a-f or A-F.For example,
0x125.
29
An integer constant can be appended at the end of the constant. The suffix u is used for
unsigned int constants for long int constants and s for short int constants. Either of the cases
can be used.
47424U
47424u
74524L
74524l
647272UL
647272ul
140S
140s
170s
Real Constant
Decimal Notation
The number is represented as a whole number followed by a decimal point and a fractional
part. It is possible to omit digits before or after decimal point.
30
Valid floating-point constants are: 247.0, 0.416, -0.22 and +0.22
Exponential Notation
Exponential Notation is useful in representing numbers whose magnitudes are very large or
very small. The exponential notation consists of a mantissa and an exponent. The exponent is
positive unless preceded a minus sign. Consider the number 472.45. This can also be written
as 0.47245e3 representing the number 0.47245×103. The sequence of 47245 after the decimal
point of the number 0.47245 is the mantissa, and 3 is the exponent.
Another example, the number 62000000000 can be written as 62e9 or 0.62e-9. Similarly,
0.00000000045 can be written as 0.45e-9.
EXAMPLE
20.04
4.7e-4
3E8
EXAMPLE
Note
31
String Constant
EXAMPLE
VARIABLES
Variable is a name that can be used to store data value. A variable can have only one value
assigned to it any given time during the execution of the program.
EXAMPLE
Marks
Average
Total
(2) emp_name
(3) mark
(1) 3a
32
(2) salary
(3) student-name
Data types are used to store various types of data that is processed by program. Data type
attaches with variable to determine the number of bytes to allocate the variable and valid
operations which can be performed. It supports various data types such as character, integer
and floating-point types.
DATA TYPES
C Stores character type internally as an integer. Each character has 8 bits. So, we can have
256 different character values 0 to 255.
Integer data types are used to store numbers and characters. Here is the able of integer data
type in various forms:
33
Unsigned short 2 bytes 0 to 216-1(0 to 65535)
2147423647)
The floating-point data types are used to represent floating point numbers. Floating point data
types come in three sizes. Float (single precision) double (double precision) and long double
(extended precision).
1.3.4 Operators
1. Arithmetic operators
2. Unary operators
3. Relational operators
4. Logical operators
5. Assignment operators
7. Conditional operators
8. Bitwise operators
9. Comma operators
ARITHMETIC OPERATORS
34
The arithmetic operators can perform arithmetic operations and can be classified into unary
and binary arithmetic operators.
The use of unary ± does not serve any purpose by default, numeric constants are assumed to
be possible. However, it can be used as follows:
A=+50
The unary minus operator can be used to negate the value of variable. It is also used specify a
negative number. Here a minus sign (-) is prefixed to the number. The unary minus operation
has 0 the effect of multiplying its operand by -1.
EXAMPLE
2. int x=7
int sum=-x
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
35
% Modulus operators
The operator acted upon by arithmetic operators must represent numeric values. Thus; the
operator can be integer values, floating point values or characters. The modulus operator (%)
requires both the operands to be integers and the second operand as a non-zero. If one of the
operands represents negative values, then the addition, subtraction, multiplication and
division operators will result in values whose signs are determined by the rules of algebra.
EXAMPLE
Expression Value
a+b 15
a-b 7
a*b 50
a/b 2
a%b 1
If x and y are two floating point variables whose values are 10.7 and 5.5 then
Expression Value
x+y 16.2
x-y 5.2
x*y 58.85
36
x/y 1.9
If c1 and c2 are two characters variables that represent the characters P and T respectively,
then
Expression Value
C1 80
C1+C2 164
C1+C2+5 169
C1+C2+‟5‟ 217
UNARY OPERATORS
Unary operators are another type of operators that act upon a single operand to produce a
value.
Operator Meaning
- Unary minus
++ Increment operator
-- Decrement operator
The unary minus is minus sign which precedes as a numerical constant a value or an
expression. Unary minus, negates the value of the number. Examples are -5, -(a+b), -0.5, -
root2.
If a=-2, b=7, c=-3 then the values of a, b and c would be 2, =7 and 3 respectively.
37
The increment operator ++ causes its operand to be increment by one where as the decrement
operator – causes its operand to be decrement by one. These operators can each be utilized in
two different ways, depending on whether the operator is written before the operand. If the
operator precedes the operand (++i), then the operand will change in value before it is
utilized for its intended purpose within the program. These operators are called pre increment
and decrement operators.
RELATIONAL OPERATORS
The relational operators are used to compare arithmetic, logical and character expressions.
We can compare two similar values and depending on their relation take some decision.
These comparisons can be done with the help of relational operators. Each of these operators
compares their left-hand side with their right-hand side. The relational operator then
evaluates to an integer. If evaluates to zero if the condition is false, and one if it is true.
Operator Meaning
!= Not equal to
If i, j and k integer variables having the values 1,2 and 3 respectively, then
i<j True 1
(i+j)>=k True 1
(i+k)>((i+5) False 0
38
K!=3 False 0
J==2 True 1
LOGICAL OPERATORS
Operator Meaning
|| Logical OR
! Logical NOT
Taking logical AND when two expressions are added the resulting expression will be true
only if both sub expressions are true. Example such as (x>15) && (y<10) will be true only if
x is greater than 5 and if y less than 10. The second expression is not evaluated if the first
expression is false.
EXAMPLE
(ii) Logical OR
A logical expression is evaluated from any one of the sub expressions are true. Example
(x>15) || (y<10) will be true any first expressions is true or second expressions is true. The
entire expression will be true or either any expression is true.
39
EXAMPLE
if (number<0 || number>100)
The logical! (NOT) operator takes single expression and evaluates to true (1) if the
expression is false(zero) and evaluates to false(zero) if the expression is true.
EXAMPLE
! (x>=y)
The expression after the! Operator in this case is x>=y. The not expression evaluates to true
only if the value of x is neither greater than nor equal to y that is only if x is less than y.
ASSIGNMENT OPERATORS
In C language assignment operator = (Equal Sign). In this operator evaluates the expression
on the right side and assigns the resulting value to the variable of left. Assignment
expressions are written in the form
SYNTAX
Identifier=Expression
EXAMPLE
40
INCREMENT AND DECREMENT OPERATORS
C provides two operators for incrementing and decrementing variables. The increment
operator ++ add 1 to its operand, while the decrement operator – subtracts.
The increment (++) and decrement (--) operator may be used either as prefix operators
(before the variable, as in ++x), or postfix operators (after the variable++). In both cases, the
effect is to be increment the value of x. But the expression ++x increments x before its value
is used, while x++ increments x after its value has been used.
EXAMPLE
x=5 then
In both cases x becomes 6. The increment and decrement operators can only apply to
variables.
CONDITIONAL OPERATORS
SYNTAX
Expression1? Expression2:Expression3
The conditional (?) works as done similar to see ternary operator. If expression1 is evaluated
first, if the expression1 is true, expression2 evaluated, if the expression is false, expression3
is evaluated.
EXAMPLE
41
a=20; b=25;
x= (a>b)? a: b
In this example if check the condition if a>b that is 20>25 condition is false so x will be
assigned of b.
BITWISE OPERATORS
Bitwise operator operates on each bit of data. These operators are used for testing the bits or
shifting them either left or right. Generally, the bitwise operators are not applicable in the
cases of float and double variables.
Operator Meaning
| Bitwise Or
^ Bitwise XOR
~ Bitwise Complement
EXAMPLE
According to my convenience, let us assume that the integer copies 16 bits (2 bytes).
42
(i) Bitwise AND
This statement can be executable with the help of bitwise AND operator. After this statement
is executed, each bit of c will produce the result 1 only if the corresponding bits in the both
the bits a and bare 1. Otherwise, it will return to 0. The bitwise AND operator is a binary
operator.
(ii) Bitwise OR
This statement can be executed with the help of bitwise OR operator. After this statement is
executed, each bit of c will produce the result 1 whenever at least any one of the bit positions
as 1. otherwise, it will return to 0. The bitwise OR operator is a binary operator.
This statement can be executed of the bitwise XOR operator. After this statement is executed
each bit of c will produce the result 1, whenever the corresponding bits in a and b differ. The
bitwise XOR operator is also binary operator.
43
a:0000 0000 0000 1010
(iv) Left shift operator: The left shift operator to use the symbol << is a binary operator.
EXAMPLE
c=a<<5
The value of c in the integer of a is shifted to the left by one bit position. The result will be
assigned to c.
After executing the left shift operator one zero are inserted in to right.
(v) Right shift operator: The right shift operator to use the symbol >> is a binary
operator.
EXAMPLE
c=a>>2
The value of c in the integer of a is shifted to right by one bit position. Finally, the result will
be assigned to c.
after executing right shift operator one zero are inserted into left.
44
(vi) Bitwise complement operator
The bitwise complement operator (~) is a unary operator. It gives the value complementing
each bit to the operand.
Bitwise complements (~) are, b: 0000 0000 0000 0101 after bitwise complement that is ~b
c:1111 1111 1111 1010
COMMA OPERATORS
The set of expressions separated by using the operator are called comma operator. If any
expression is represented are evaluated from left to right
EXAMPLE 1
where, int i, j;
EXAMPLE 2
i= (j=2, j+3)
In above statement two expressions are separated by comma. If first expression j=2 is
evaluated after the expression j+3 is evaluated. In the above statement value 5 is assumed the
variable of i.
45
Text
expression
SPECIAL OPERATORS
(i) The size of () operator is denoted as special operators when used an operand it returns the
number of bytes the operand occupies.
EXAMPLE
(ii) The other special operator is (. and ->) which is used with structures and unions.
1.3.5 Expression
ARITHMETIC EXPRESSION
46
EXAMPLE 1
1. a+b+c*d
2. a*(b-c/d)-e/g
EXAMPLE 2
1+3 is 4
2.23-0.45 is 0.78
3.3*8 is 24
4.-.5**2 is -25
5.12/4 is 3
EVALUATION OF EXPRESSION
In C language the expressions are evaluated using assignment operators. When expression is
executed that is evaluated in right side and then the evaluated result is stored in left-side of
variable name.
EXAMPLE
z=(m/n)/(x+a-b)
47
(i) In arithmetic expression is represented without parentheses will be evaluated from left to
right. Suppose in expression are represented with parentheses, first evaluated as
parentheses of expression, so it is highest priority.
(ii) If expression is represented with two or more parentheses appear the expression is
evaluated with left-most parentheses of expression after it evaluated with right-most.
EXAMPLE 1
x=5-12/3+5*2+1
48
5 12 / 3 + 5 * 2 + 1
1
2
4 3
x=12
EXAMPLE 2
49
Fig 1.10 C Operators
1.4 SUMMARY
50
The relational operators are used to compare arithmetic, logical and character
expression.
The logical operator allows a programmer to combine into single relational
expression.
In C language assignment operator = (Equal sign). In this operator evaluates the
expression on the right side and assigns the resulting value to the variable to left.
The reaming type of operator conditional operations, bitwise operators, comma
operators and special operators.
Oval Ovals indicates both the start and end of the process.
___________________________________________________________________________
___________________________________________________________________________
51
2. Write an algorithm that reads two values, determines the largest value and print message
largest value
___________________________________________________________________________
___________________________________________________________________________
A. Descriptive Questions
Short Questions
Long Questions
1. What is flowchart? Briefly explain different symbols used in flowchart with Example.
2. Discuss the following
a) Rules for creating flowchart
b) Advantages of flowchart
c) Limitations of flowchart
3. What are different data types in C?
4. What do you mean by identifier and keywords?
5. What are the applications of C programming?
a. Algorithm
b. Program
c. Code
d. Flow chart
52
2. Among unary operation which operator represents increment?
a. --
b. ++
c. -
d.!
d. Do mathematical manipulations
b. Calling function
c. Main function
d. Void function
5. A function _____
c. Both a and b
d. None of these
Answers
53
1.8 REFERENCES
References book
54