Vous êtes sur la page 1sur 17

Unit-1

Introduction:
* The purpose of computers is to solve problems, which are difficult to solve manually.
* Computer software is divided into application software and system software.
* Application software is the set of programs necessary to carry out operations for specified
applications. System software is the general purpose programs written for the system, which
provide the environment to facilitate the writing of application software.
High Level Languages:
FORTRAN: It stands for FORmula TRANslation, it is oldest high level language. This language
is developed by John Backus and his team in 1956-57. The main aim behind the development of
this language is to solve scientific and engineering applications.
COBOL: It stands for COmmon Business Oriented Language. This language was developed by
Murray Hooper, in 1959. The main aim behind the development of this language is for Business
data processing.
BASIC: It stands for Beginners All Purpose Symbolic Instruction Code, developed in 1963 by
John and Thomas. The main aim behind the development of this language is for users who are
starting their learning in computers and it can be learnt quickly.
PASCAL: It was named after the French mathematician Blaise Pascal and it is designed,
developed by Nickolus Wirth in 1971. The main objective behind the development of this
language is which allows beginners to learn good problem solving and programming methods.
History of C language:

C is one of the most popular languages; it was developed by Dennis Ritchie at AT & Ts
Bell Laboratories at USA in 1972.

It is upgraded version of two earlier languages BCPL and B. The root map for all modern
languages is started through the ALGOL language in 1960s.

We know COBOL was developed for business applications; FORTRAN is developed for
scientific applications. PASCAL is for problem solving and programming methods.

At this time people started to thinking about the single language which can program all
possible applications. A committee was formed to develop a new language called CPL
(Combined Programming Language) which it seems to BCPL.

At this time, a language called B was developed by Ken Thompson. Based on these two
Dennis Ritchie developed a new language with some additional features of BCPL and B
which is very simple, good programming efficiency and a relatively good machine
efficiency called C language.
1

How we can support C is middle level Language


Low Level: Low level language in terms of 0s and 1s. C has the certain features of Low-level
language that allows the programmer to carry out operations on bits that are normally available
in assembly language or machine language.
High Level: High level language looks like normal English, whose instructions are more
compatible with human languages. These languages are easily understandable and have been
designed to give a better program efficiency. Ex: C, C++, FORTRAN, PASCAL, COBOL.
C stands in between these two categories. It is neither a low level language nor a high
level language. It is a middle level language. It means it performs the task of low level
language as well as high level language. We can write code for operating system,
application programs. Unix operating system is written in C language.
Features and Applications of C language.
1. C is structured programming language.
2. C is powerful, efficient and flexible.
3. C is highly portable (i.e., it can run different operating systems).
4. C is a robust language whose rich set of built in functions and operators.
5. C has ability to extend itself. We can add our own functions to the existing library.
6. C is middle level language i.e., it supports both low level and high level languages.
7. C language allows dynamic memory allocation.
8. C programs are fast and efficient.
9. C has rich set of operators.
10. C can be applied in system programming areas like compilers, interpreters,
assemblers.
Structure of C Program
A 'C' program may contain one or more sections like
i).
Documentation section: It consists a set of comment lines used to specify the name of
program, the author and other details etc,
ii).
Comments: Comments are very helpful in identifying the program features and
underlying logic of the program. The lines begins with '/*' and ending with '*/' are known as
comment lines. These are not executable, the compiler is ignored anything in between /* and */.
iii)
Preprocessor section: It is used to link system library files, for defining the macros and
for defining the conditional inclusion. Ex: #include<stdio.h>, #define A 10, #if def, #endif... etc.

iv).
Global Declaration Section : The variables that are used in more than one function
throughout the program are called global variables and declared outside of all the function i.e.,
before main().
v).
Declaration part: This part is used to declare all the variables that are used in the
executable part of the program and these are called local variables.
vi). Executable part: It contains at least one valid 'C' statement. The execution of a program
begins with opening brace '{' and ends with closing brace '}'. The closing brace of the main
function is the logical end of the program.
D o c u m e n ta tio n s e c tio n
P re p ro c e s s o r s e c tio n
D e fin itio n s e c tio n
G lo b a l d e c la ra tio n s e c tio n
m a in () {
D e c la ra tio n P a rt
E x e c u ta b le P a rt
}
s u b p ro g ra m s e c tio n
{
B o d y o f th e p ro g ra m
}

Program Development Steps:


The process of writing a program is called programming. But before programming can begin the
problem to be solved needs preparation. The computer can neither think nor make any decision
on its own. Hence computers have to be told exactly what to do, in a step-by-step fashion-called
problem analysis. While solving a problem the following steps are required.Problem specification: The programmer must know exactly what he wants to do before he can
begin to do it. A careful examination of all inputs provided, the question asked, type of output
specifications may be useful in finding out just what he has to be done.
Outlining the solution: In some cases, it is necessary to invent a new method or to incorporate
some concessions, that is either a method must be developed, or the problem has to be simplified
to the point where a solution becomes feasible.
Selecting and representing algorithm: When a solution method has been outlined, it must be
translated into an algorithm. An algorithm is a way of representing the solution used to solve the
particular problem.

The algorithm is programmed: It will be very easy to convert the algorithm into a program.
The choice of programming languages depends on the nature of the problem and the availability
of programming languages.
Removing errors: Errors are common while developing a program that causes wrong results.
There are 3 types of errorsi). Syntax errors: When rules are not followed, then that language terms it as
syntax error. Ex: sum= a+b+c); /* the error is missing left parenthesis*/.
ii). Runtime errors: These runtime errors are not known till the program is
executed. Ex: Dividing by zero, range out of bounds, exceeding the limit.
iii). Logical errors: Logical errors are due to the existence of logically incorrect
instructions in the program. It is very hard to find out. It gives undesired results.
Testing and Validation: Once a program is written, a program must be tested and then
validated. There are 4 classes of test datai). Every logical flow of path must contain at least one test case.
ii). Boundary cases: These cases are derived from the valid cases. They signify some of the
possible values through which decisions can be made.
iii). It requires some special methods to complete their execution.
iv). Generally, they do not occur during the processing of a program.
Documentation and Maintenance: Program must be written in a clear and easy way, so that
other users who use this program may not find any difficulty. Maintained well by using
i). Proper comments. ii) Program indentation. iii). Description about methods used.
Various criteria used for judging the algorithm:
Algorithm is a method of representing the step-by-step logical procedure for solving a
problem in a finite number of steps. An algorithm must possess the following properties.
i). Input: For every program input is essential, sometimes need not be. Input means the data
supplied for the algorithm
ii). Output: The main aim of the program is to get an output. It is nothing but processed
information.
iii). Finiteness: An algorithm must terminate in a finite number of steps.
iv). Definiteness: Each step of the algorithm must be clear and unambiguously stated.
Ex: compute 5/0 or add 5 or 6 to x are not permitted because it is not clear what the result is.
v). Effectiveness: A step specified in an algorithm must be solvable, and it can be done by a
person with the help of a pencil and paper.
Ex: if user enters a negative number as input in evens, if we have step like
Step: if n>0
goto ERROR
4

FLOW CHART: Flowchart is diagrammatic representation of an algorithm. It is constructed


using different types of boxes and symbols. All symbols are constructed by arrows among
themselves to indicate the flow of information and processing.
T e r m in a l

O val

I n p u t/O u tp u t

P a r a lle lo g r a m

P r in to u t

D ocu m en t
R e c ta n g le

P ro cess

D ia m o n d

D e c is io n

C ir c le

C o n n e c to r

A rrow

D o u b le s id e d
R e c ta n g le

<

F lo w
P r e d e fin e d
P ro cess

S ta rt/S to p /B e g in /e n d
M a k in g d a ta a v a ila b le fo r p ro c e s s in g in p u t
o r re c o rd in g o f th e p ro c e s s e d in fo rm a tio n o /p
S h o w d a ta o u tp u t in th e fo rm o f d o c u m e n t
A n y p ro c e s s in g to b e p e rfo rm e d . A p ro c e s s c h a n g e s
o r m o v e s d a ta . A n a s s ig n m e n t o p e ra tio n n o rm a lly
re p re s e n te d b y th is s y m b o l.
D e c is io n o r s w itc h in g ty p e o f o p e ra tio n
th a t d e te rm in e s w h ic h o f th e a lte rn a tiv e
p a th s is to b e fo llo w e d
U s e d to c o n n e c t d iffe re n t p a rts o f flo w c h a rt
J o in s tw o s y m b o ls a n d a ls o re p re s e n ts
flo w o f e x e c u tio n
M o d u le s o r s u b ro u tin e s s p e c ifie d e ls e w h e re

Rules for Flow Charts:


Use common and easily understandable words.
Use consistent methods in drawing a flowchart.
Use consistent words or names in the flowchart.
Avoid crossing flow lines in the flowchart.
Draw the flow chart from top to bottom and left to right.
Advantages of Flowchart:
A flow chart can easily explain the program logic to the program development team.
A flow chart is useful to prepare detailed program documentation.
A flow chart is useful to test the logic of the program.
The flow chart helps detect and remove the mistakes in a program.
The flow chart details help prepare efficient program coding.
Limitations:
Flow chart is a laborious process when proper symbols are used.
Modifications in flowcharting are very difficult and it consume more time.
Redrawing the flowchart is also tedious work.
Example: Draw a flowchart to find whether the sum of two numbers is odd or even is as follows.
5

S ta r t

R e a d x ,y

S u m -x + y

T ru e

F a ls e

P r in t o d d

P r in t E v e n

S to p

PSEUDOCODE:
Pseudocode is a tool used for planning a computer program logic or method. Pseudo
means imitation, and code refers to the instructions written in a computer language.
Some programmers prefer to write the actual words or pseudocode to represent the
various steps rather than drawing the flowchart.
Pseudocode is useful to design structured programs.
Pseudocode looks similar to the actual coding.
The following Pseudocode to find the biggest of given two numbers.
Start
Read Number1, Number2
If Number1>Number2 then
Print Number1
Else
Print Number2
Endif
End
Advantages of Pseudocode:
Flexibility is a great advantage in using pseudocode.
Pseudocode instructions may be easily converted into programming language
instructions.
Pseudocode instructions can be easily modified.
Prepare the pseudocode requires less time than drawing a flow chart.
Limitations of Pseudocode:
Reading pseudocode instructions takes more time.
There is no standard procedure for preparing pseudocode instructions.
Beginners find it difficult to write pseudocode than drawing a flowchart.
6

Execution of a C program:
A C program has to pass through many phases for its successful execution and to achieve
desired output. The phases or steps required for successful execution of a C program can
be visualized as1). Editing Phase: In the editing phase, the program is entered into a file through a text
editor. Corrections in the program at later stages are done through these editors. Once the
program has been written it requires to be translated into machine.
2). Compilation Phase: This program is carried out by a program called as a compiler.
Compiler translated the source code into the object code. The compilation phase cannot
be processed successfully until and unless the source code is error free. The source code
is translated into object code and stored in a separate file with extension obj.
3). Linking phase: In this phase, the linker links all the files and functions with the
object code under execution. Now the object code is ready for next phase.
4). Execution Phase: In this phase the executable object is loaded in to the memory and
the program execution begins.
S ta rt

E d itin g s o u rc e c o d e
C c o m p ile r

C o m p ilin g
so u rc e co d e
S y n ta x E rro rs
E rro rs

S y s te m lib ra ry

Yes
N o
O b je c t c o d e
L in k in g c o d e w ith
s y s te m lib ra ry
L o a d in g o b je c t c o d e
E x e c u tin g o b je c t
code

E rro rs?

R u n tim e a n d
lo g ic a l e r r o r s
Yes

N o
O u tp u t
S to p

Character Set
Any programming language needs a set of characters to write a program. The set of characters
can be represented in the computer. C has got its own character set. The character set for ANSIC
Alphabet: Uppercase -A,B,.Z; Lower case -a,b.z;
Digits: 0, 1,2,3,4,5,6,7,8,9
Special characters: ? , !, ~, , , ; :. Etc
Execution Character set: These are also called as escape sequences.- \t, \n, \a, \b etc.
7

Token: The tokens are usually referred to as individual text and punctuation in a passage of text.
The C language a program can contain the individual units called the C Tokens.
T O K E N S

I d e n tifie rs
to ta l
av g

C o n s ta n ts

K ey w o rd

O p e ra to rs

2 2 .4 5
3 .1 4

in t
d o u b le

+ * /

S p e c ia l
C h a ra c te rs
#

$ &

Identifiers: Identifiers are names given to various program elements, such as variables,
functions and arrays.
Rules for naming an identifier:
1).Identifier consists of letters, digits in any order.
2).The first character must be a letter/character or may begin with underscore.
3).No space and special symbols are allowed between the identifier.
4).The identifier cannot be a keyword.
5).The uppercase identifier is not equivalent to lower case.
Some valid identifiers are: name, marks, y2k.
Some invalid identifiers are: 4record, stu name, return.
Keywords: There are certain reserved words called keywords that have standard and predefined
meaning in C language. Those are
a u to
const
d o u b le
flo a t
in t
s ig n e d
s tru c t
u n s ig n e d

b re a k
c o n tin u e
enum
fo r
lo n g
sh o rt
s w itc h
v o id

case
d e fa u lt
e ls e
g o to
re tu rn
s ta tic
ty p e d e f
v o la tile

char
do
e x te rn
if
re g is te r
s iz e o f
u n io n
w h ile

Data Types: Data type is the type of the data that are going to access within the program. 'C'
supports different data types; each data type may have predefined memory requirement and
storage representation.
'C' supports the following 4 classes of data types
C D a ta ty p e s
P r im a ry

U s e rd e fin e d

char
in t
flo a t
d o u b le

ty p e d e f

D e riv e d
a rra y s
p o in te rs
s tru c tu re s
u n io n s

E m p ty

v o id

Integer type: Integers are numbers with the supported range. Usually the integers occupy one
word of storage typically 16 to 32 bits. The size of the integer depends on the system.
Ex: If we use 16 bit word length, the size can be limited upon the -32768 to 23768.
8

Character type: Characters are generally stored in 8 bits of the internal storage of computer and
single character can be defined as char data type.
Float type: It is generally stored in 32 bits with the 6 digit precision, where as double data type
uses 64 bits with the 14 digits of precision.
Null data type: The void is null data type in C language.
Sizes and Ranges of data types with type qualifier
Type

S iz e

c h a r o r s ig n e d c h a r
unsogned char
in t o r s ig n e d in t
u n s ig n e d in t
s h o rt o r s ig n e d s h o rt in t
u n s ig n e d s h o rt in t
lo n g in t o r s ig n e d lo n g in t
u n s ig n e d lo n g in t
flo a t
d o u b le
lo n g d o u b le

1
1
2
2
1
1
4
4
4
8
10

C o n tro l
S tr in g

R ange

-1 2 8 to 1 2 7
% c
0 to 2 5 5
% c
- 3 2 ,7 6 8 to 3 2 ,7 6 7
% d or %
0 to 6 5 ,5 3 5
% u
-1 2 8 to 1 2 7
% d or %
0 to 2 5 5
% d or %
- 2 ,1 4 7 ,4 8 3 ,6 4 8 to 2 ,1 4 7 ,4 8 3 ,6 4 8 % ld
0 to 4 ,2 9 4 ,9 6 7 ,2 9 5
% lu
3 .4 E - 3 8 to 3 .4 E + 3 8
% f or %
1 .7 E - 3 0 8 to 1 .7 E + 3 0 8
% lf
3 .4 E - 4 9 3 2 to 1 .1 E + 4 9 3 2
% lf

i
i
i

Variables, Constants and Delimiters


A variable is an identifier that is used to represent some specified type of information
within a designated portion of the program. A variable may take different values at different
times during the execution. It is named as memory location.
Rules for naming the variables:
1. A variable can be combination of alphabets digits or underscore.
2. The first character must be an alphabet or an underscore (_). No special symbol is used.
3. The length of the variable cannot exceed upto 8 characters long and some of the C
compilers can be recognized upto 31 characters long.
4. No commas or blank spaces are allowed within a variable name.
Syntax: data_type v1, v2, v3,,vn; Ex int avg=30;
Constants in C
C Constants (1) Numeric constant: (i) Integer constants (ii) Real constant
(2) Character constant: (i) Single Character constant (ii) string constant
'C ' C o n s ta n ts

N u m e r ic
C o n sta n ts

In teg er
C o n sta n ts

C h a r a c te r
C o n sta n ts

R eal
C o n sta n ts

C h a r a c te r
C o n sta n ts

S tr in g
C o n sta n ts

I. Numeric Constants:
a). Integer Constants: An integer constant formed with the sequence of digits. There are 3 types
of integer constants forms different number system.
Decimal number: 0 to 9; Octal Number: 0 to 7; Hexadecimal: 0 to 9, A,B,C,D,E,F
Rules for defining an integer constant:
1. It must have at least one digit.
2. Decimal point is not allowed.
3. It can either positive or negative.
4. No commas or blank spaces are allowed.
5. The allowable range for integer constants is -32,768 to +32,767.
b). Real Constant: A real is made up of a sequence of numeric digits with presence of a decimal
point.
Rules for defining a real constant:
1. A real constant must have one digit.
2. A real constant must have decimal point.
3. It can either positive or negative.
4. No commas or blank spaces are allowed.
II. Character Constants:
a). Single character constants: The character constant contains a single character enclosed with a
pair of single inverted commas both pointing to the left.
Eg: b, M, 8. Note: The character constant 8 is different from the number 8.
b). String constants: A string constant is a sequence of characters enclosed in double quotes; the
characters may be letters, numbers, special characters and blank spaces etc. At the end of the
string \0 is automatically placed.
Statements
Statement causes the computer to perform some action. All statements in C language ends with
semicolon except conditional and control structured, the types of statements are1). Assignment Statements: The assignment operator used for assigning values to the variables.
Example : basic = 2977; dob = 2977; flag = 0; l=a=k=2977;
2). Block Of Statements: Block contains several statements that are enclosed within a pair of
braces {}. Example : {
int a=977;
float b=459.77;
printf("%d%f", a,b); }
3). Expression Statements: These consist of expressions and can be arithmetic, relational or
logical. Example: a=29;
b=a+77; fun(a,b);
4).Null Statements: A statement without any characters and it has only semicolon is called null
statement. Example :
; (null statement)
10

Operators
An operator is a symbol that specifies an operation to be performed on operands. The data items
that operators act upon are called operands. The operators are usually form a part of
mathematical or logical expressions. Ex: a+b where + is operator, a & b are operands.
Type of operators. C provides a rich set of operators, depending upon their operation they areO p e ra to rs

A s s ig n m e n t
o p e ra to rs

A rith m e tic
o p e ra to rs

R e la tio n a l
o p e ra to rs

L o g ic a l
o p e ra to rs

In c re /D e c re m e n t
o p e r a to rs

C o n d itio n a l
o p e ra to rs

B itw is e
o p e ra to rs

S p e c ia l
o p e ra to rs

Assignment Operator: Assignment operators are used to assign a value or an expression or a


value of a variable to another variable.
Syntax variable=expression (or) value anything. Example x=10; x=a+b; x=y;;
Arithmetic Operator: 'C' allows us to carryout basic arithmetic operations like addition,
subtraction, multiplication, and division. The following table shows the Arithmetic Operators and
their meaning.
Operator
Meaning
Examples
+
Addition
2+9= 11
Subtraction
9 - 2= 7
*
Multiplication
2*9= 18
/
Division
9/3= 3
%
Modulo division
9 % 2= 1
Example:
#include<stdio.h>
main()
{
int i, j,k;
clrscr();
i = 10;
j = 20;
k = i + j;
printf ("Value of k is %d\n ", k);
getch();
}
Increment and Decrement Operator: 'C' has two way useful operators not generally found in
other languages, these are the increment (++) and decrement ( -- ) operators. The'++' adds one to
the variables and '--' subtracts one from the variable. These operators are called unary operators,
because they acts upon only one variable.
Operator
Meaning
++x
Pre increment
11

--x
Pre decrement
x++
Post increment
x-Post decrement
Example:
/* Program using increment and decrement operators * /
#include<stdio.h>
main ()
{
int a=10;
printf("a++= %d\n", a++); /* ++is a post increment * /
printf("++b = %d\n", ++b); /* ++is a pre-increment * /
printf("--a = %d\n", --a); /* -- is a pre-decrement * /
printf("b-- = %d\n", b--); /* -- is a post-decrement * /
}
Relational Operator: Relational operators are used to compare two or more operands. Operands
may be variables, constants or expression. For example we may compare the age of two persons,
or the price of two items and so on. These comparisons can be done with the help of relational
operators. The following table shows the Relational Operators.
Operator
Meaning
Example
Return value
<
is less than
2<9
1
<=
is less than or equal to
2 <= 2
1
>
is greater than
2>9
0
>=
is greater than or equal to
3 >= 2
1
==
is equal to
2 == 3
0
!=
is not equal to
2! = 2
0
Example:

/* Program to use various relational operators */


#include <stdio.h>
main()
{
printf("\nCondition : Return values \n");
printf("\n5!=5
: %5d",5!=5); /* != is assignment operator */
printf("\n5==5
: %5d",5==5); /* == is assignment operator */
printf("\n5>=50 : %5d",5>=5); /* >= is assignment operator */
printf("\n5<=50 : %5d",5<=50); /* <= is assignment operator*/
printf("\n5!=3
: %5d",5!=3);
}
Logical Operator: Logical operators are used to combine the results of two or more conditions.
'C' has the following logical operators.
12

Operator
Meaning
Example
Return value
&&
Logical AND
(10>2) && (15>2)
1
||
Logical OR
(10>2) || (19==7)
1
!
Logical NOT
23!=23
0
Example: /* Program on demonstration of logical operator */
#include<stdio.h>
void main( )
{
int c1,c2,c3; /* Local definitions */
printf("ENTER VALUES OF c1, c2 AND c3 : ");
scanf("%d%d%d",&c1,&c2,&c3);
if((c1 < c2)&&(c1<c3))
/* && is a logical operator */
printf("\n c1 is less than c2 and c3");
if (!(c1< c2))
printf("\n c1 is greater than c2");
if ((c1 < c2)||(c1 < c3))
/* || is a logical operator */
printf("\n c1 is less than c2 or c3 or both");
getch();
}
Conditional Operator: Conditional operator itself checks the condition and executes the
statement depending on the condition.
Example
#include<stdio.h>
main()
{
int a=5,b=3,big;
big=a>b?a:b;
printf("Big is...%d",big);
}
Sizeof() Operator: Along with these operators, the C language supports some of the special
operators are-.
Operators
Meaning
,
comma operators
sizeof
size of operators
&*
pointer operators
(a) Comma operator(,) : Usually the comma operator is used to separate the statement elements
such as variables, constants or expression etc.
13

(b) The sizeof() operator : The sizeof() is a unary operator, that returns the length in bytes of the
specified variable, and it is very useful to find the bytes occupied by the specified variable in the
memory.
(c) Pointer operators :
& : This symbol specifies the address of the variable.
* : This symbol specifies the value of the another variable.
Example: /* Program to illustrate sizeof operator */
#include<stdio.h>
void main()
{
int num=1234567890;
float dec= 0.123456;
double ext=0.123456789;
char ltr='A';
printf("Size of num int is %d bytes\n",sizeof (num));
printf("Size of dec float is %d bytes\n", sizeof (dec));
printf("Size of ext double is %d bytes\n",sizeof (ext));
printf("Size of ltr char is %d bytes\n",sizeof (ltr));
getch();
}
Bitwise Operator: Bitwise operators are used to manipulate the data at bit level. It operates on
integers only. It may not be applied to float or real. The operators and its meaning are given
below.
Operator
Meaning
&
Bitwise AND
|
Bitwise OR
^
Bitwise XOR
<<
Shift left
>>
Shift right
~
One's complement
Bitwise AND (&): This operator represented as & and operates on two operands of integer type.
While operating upon these two operands they are compared on a bit by bit basis.
E g :

X = 7 =

0 0 0 0 0 1 11

Y = 8 =

0 0 0 0 1 0 0 0

X & Y = 0 0 0 0 0 0 0 0

Bitwise OR (|): This operator gives if either of the operand bit is 1 then result is 1 or both
operands are 1s then also given 1.
14

E g:

X = 7=

0000 0111

Y = 8=

0000 1000

X |Y =

0000 1111

Bitwise XOR (^): This operator gives if either of the operand bit is high (1) then it gives high (1)
result. If both operands are same then it gives low (0) result.
E g:

X = 7=

0000 1101

Y = 8=

0000 1000

X ^Y =

0000 0101

For all the operators in all possible combinations of bits area

a /b

a& b

a^b

~a

Bitwise Left shift (<<): The bitwise left operator is used to shift the given number of bits an
integer towards left. The vacant positions on the right of the integer would be filled with zeros.
The general form of its usage is:
operand<<2; Here, 2 bits move at left.
in t x = 1 0

A fte r x < < 2

B in a ry E q u iv a le n t o f x
0

Bitwise Right shift (>>)t: The bitwise right operator is used to shift the given number of bits an
integer towards right. The vacant positions on the right of the integer would be filled with zeros.
The general form of its usage is:
operand >>2; Here, 2 bits move at right.
in t x = 1 0

B in a ry E q u iv a le n t o f x
A fte r x > > 2

Bitwise ones complement (~): The bitwise ones complement operator is used to invert the bits
of an integer. That is, it replaces all zeroes by ones of an integer.
in t x = 1 0
B in a ry E q u iv a le n t o f x

~x

0 0 0 0

0 0 0 0

0 0 0 0

1 0 1 0

1 1 1 1

1 1 1 1

1 1 1 1

0 1 0 1

Operator Precedence and associativity: Usually the arithmetic operators are evaluated from the
left to right using the precedence of operators when the expression is written without the
parameters.
Eg: x=4*5+2;
In the statement the operand 5 is for two operators. But the multiplication operator has higher
precedence than the addition operator, thus 4*5 is evaluated first, which returns into 20. After 2
is added to the result it becomes 22.
Consider the following expression
15

X=3*4/5+10/5+8-2+7/8;
E x p re s s io n
x =
=
=
=
=
=
=
=

1 2 /5 + 1 0 /5 + 8 -2 + 7 /8
2 + 1 0 /5 + 8 -2 + 7 /8
2 + 2 + 8 -2 + 7 /8
2 + 2 + 8 -2 + 0
4 + 8 -2 + 0
1 2 -2 + 0
10+0
10

O p e ra tio n
*
/
/
/
+
+
+

Hierarchy of operators:
a). Any expression within parenthesis is first evaluated.
b). Unary operators are evaluated first in expression.
c). Then priority is given for multiplication and division.
d). Then subtraction and addition are performed.
e). Then relational operators are evaluated.
f). Then the equality checking is performed.
g). Then logical operations are performed.
h). Then the conditions are checked.
i). Finally, the assignment operation is carried out.
Type conversion:
The type conversion or type casting refers to the process of changing of an entity of one data
type into another. There are two types of type conversions.
1. Implicit.
2. Explicit
Implicit Conversion: It is referred to as coercion (force). It is an automatic type conversion. Here,
higher data can be easily converted into lower data type.
Eg: double a;
long b;
int c;
if(a>c)
a=c;
if(c>b)
b=c;
Note: data can be lost when floating point representations are converted to integral representation
Fractional part can be truncated.
Explicit Conversion: The explicit conversion can be made possible to convert one data type to
another by forcefully and it is different from the implicit conversion.
Example:
float a=10;int b;
b=(float)a;
Input/Output Functins:
16

Introduction:
Formatted Input/Output refers to input and output, that has been arranged in a particular format.
The following are the formatted Input/Output statements.
Input
Output
scanf()
printf()
fscanf()
fprintf()
In 'C' language, two types of Input/Output statements are available, and all input and output
operations are carried out through function calls. Several functions are available for input/output
operations in 'C'. These functions are collectively known as the standard I/O library.
i) Unformatted Input/Output statements
ii) Formatted Input/Output statements
Scanf Function:
This is (Formatted Input/Output statements). Input data can be entered into the computer using
the standard input 'C' library function called scanf(). This function is used to enter any
combination of input.
printf Function:
This is (Formatted Input/Output statements). Output data or result of an operation can be
displayed from the computer to a standard output device using the library function printf(). This
function is used to output any combination of data. It is similar to the input function scanf(),
except that it display data rather than input.

17

Vous aimerez peut-être aussi