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

Operators and Hierarchy

The document discusses different types of operators in C including arithmetic, logical, relational, assignment, increment/decrement, conditional, bitwise, and special operators. It provides examples and explanations of each operator type. It also covers operator precedence and rules for expression evaluation. The document then discusses input/output operations in C including formatted input using scanf() and formatted output using printf(). It provides examples of reading/writing characters, integers, real numbers, and strings using the standard input/output functions.

Uploaded by

pco000
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
224 views

Operators and Hierarchy

The document discusses different types of operators in C including arithmetic, logical, relational, assignment, increment/decrement, conditional, bitwise, and special operators. It provides examples and explanations of each operator type. It also covers operator precedence and rules for expression evaluation. The document then discusses input/output operations in C including formatted input using scanf() and formatted output using printf(). It provides examples of reading/writing characters, integers, real numbers, and strings using the standard input/output functions.

Uploaded by

pco000
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Operators and Hierarchy

Types of Operators

Increment/
Arithmetic Logical Bitwise
Decrement

Relational Assignment Conditional Special


Arithmetic Operator
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo Division
Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Relational Operators
Operator Meaning
< is less than
<= Less than or equal to
> is greater than
>= greater than or equal to
== is equal to
!= is not equal to
Assignment Operator
Assignment operator are used to assign the result to
an expression
“=” is the assignment operator

Statement with simple assignment Statement with shorthand


operator assignment operator
a=a+1 a+=1
a=a-1 a-=1
a=a*(n-1) a*=n-1
a=a%b a%=b
Increment/Decrement Operator
“++” and “--” are the operators

++ adds one to operand while -- subtracts one

Both are unary operators


Postfix/Prefix ++ and --
Require variable as their operands

Postfix ++ (or --): First the value of variable is used in


the operation and then increment/decrement is
performed

Prefix ++ (or --): First the value of variable is


incremented/decremented then new value is used
Conditional Operator
Also known as ternary operator
exp1 ? exp2 : exp3

true
exp1 exp2

false
a=10;
b=15;
x=(a>b)?a:b; exp2
Bitwise Operator
For manipulation of data at bit level
Bitwise operator cannot be applied to float or double

Operator Meaning
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right
Special Operators

Pointer Member selection


comma sizeof
operators operators
Comma Operator
To link the related expressions together
Evaluated from Left to Right
Ex:
value=(x=10,y=5,x+y);
t=x,x=y,y=t;
sizeof operator
Returns the number of bytes the operand occupies
The operand can be variable, constant or data type
qualifier
Ex:
 m=sizeof(sum);
 n=sizeof(long int);
 k=sizeof(235L);
Precedence of Operators
There are 2 different priorities of arithmetic
expression
High Priority: * / %
Low Priority: + -

The equation is evaluated in two passes


First pass: High priority operators
Second pass: Low priority operators
Rules for Evaluation of Expression
Parenthesized sub expression from left to right are
evaluated

If parenthesis are nested evaluation begins with


innermost braces

If operators of same precedence are encounter then


associativity is used

Arithmetic expression are evaluated from left to right


Expression: x=9-12/3+3*2-1
1st Pass
x=9-4+3*2-1
x=9-4+6-1

2nd Pass
x=5+6-1
x=11-1
x=10
Operator Description Precedence Associativity
left-to-right
() Parentheses (function call) (see Note 1)
[] Brackets (array subscript)
. Member selection via object name 1
-> Member selection via pointer
++  -- Postfix increment/decrement (see Note 2)

++  -- Prefix increment/decrement right-to-left


+  - Unary plus/minus
!  ~ Logical negation/bitwise complement
(type) Cast (change type) 2
* Dereference
& Address
sizeof   Determine size in bytes
*  /  % Multiplication/division/modulus 3 left-to-right
+  - Addition/subtraction 4 left-to-right
<<  >> Bitwise shift left, Bitwise shift right 5 left-to-right
left-to-right
<  <= Relational less than/less than or equal to
>  >= Relational greater than/greater than or equal to 6

==  != Relational is equal to/is not equal to 7 left-to-right


& Bitwise AND 8 left-to-right
^ Bitwise exclusive OR 9 left-to-right
| Bitwise inclusive OR 10 left-to-right
&& Logical AND 11 left-to-right
|| Logical OR 12 left-to-right
?: Ternary conditional 13 right-to-left
right-to-left
= Assignment
+=  -= Addition/subtraction assignment
*=  /= Multiplication/division assignment
%=  &= Modulus/bitwise AND assignment 14
^=  |= Bitwise exclusive/inclusive OR assignment
<<=  >>= Bitwise shift left/right assignment
, Comma (separate expressions) 15 left-to-right
Type Conversions

Implicit Explicit

Automatic In Assignments

The data type of one operand is converted into data type of another operand
Implicit Type Conversion
Implicit type conversion, also known as coercion

An automatic type conversion by the compiler

If operands are of different types than lower type is


automatically converted to higher type
Automatic
long double

double

float

int

char, short int


In Assignment
Type of right hand side is converted to type of left hand side

If right hand operand is lower rank than it will be promoted


float = int
int = char

If right hand operand is higher rank than it will be demoted


char=int
int=float
Explicit/Type Casting
Is done with the help of cast operator

Cast operator is a unary operator that is used for converting an


expression to a particular data type

Syntax:
(datatype) expression

Ex:
int x,y;
float x=(float)x/y;
Input/Output
Types of Operations

Input Output

The set of library functions that perform input-output operation is known


as standard input/output library (stdio.h)
Reading a Character
getchar();

Accepts any character keyed in including


return (enter)
tab space

Ex:
char variable_name;
variable_name=getchar();
Writing a Character
putchar(variable_name);

Displays char represented by var_name on the


terminal

Ex:
char c=getchar();
putchar(c);
Conversion Specifications
Specifier meaning
%c a single character
%d or %i decimal integer
%f or %e or %g floating point number
%lf long range floating point (double)
%Lf long double
%h short int
%s string
%u unsigned decimal integer
%o octal integer
%x hexadecimal
%[…] Read a string of words
Formatted Input
C provides scanf() function for entering input data
Syntax
 scanf(“control string”, address1, address2….);

 Control string specifies the format in which data has to be entered

 address1, address2 specifies the address of locations where data


is to be stored
Examples Integer Numbers
Format: %wd
 w is the field width

Ex 1
int marks;
scanf(“%d”,&marks);

Ex 2
char str[30];
scanf(“%s”,str); Value will not be stored in str

Ex 3
int basic,da;
scanf(“%d%d”,&basic,&da);
Ex 4
int hra,da;
scanf(“%d:%d”,&hra,&da);

1500:200

Ex 5
int num1,num2;
scanf(“%2d %5d”,&num1,num2);
21345 50

 21 will be assigned to num1 and 345 will be assigned to num2 and 50 that
is unread will be assigned to next scanf call
Ex 6
int a,b;
scanf(“%d %*d %d”, &a,&b);

 123 to a 123 456 789


 456 skipped (because of *)
 789 to b
Examples Real Numbers
Ex 1
float x;
scanf(“%e”,&x);
43.21e-1
 Assigns: 4.321 to x

 Ex 2
double y;
scanf(“%lf”,&y);
Rules for scanf
Each variable must have a field specification
For each field specification there must be variable
address
The scanf reads until
A white space is found in numeric specification
the maximum number of characters have been read
An error is detected
The end of file is reached
Formatted Output
printf() is used for printing results

printf(“control string”, arg1,arg2…..);

Control String specifies


characters that will be printed on screen
Format Specifications
Escape sequence characters
Examples
printf(“Programming in C”);
printf(“\n”);
printf(“%d”,x);
printf(“x=%d\n”,x);
printf(“The value of a is %d”,a);

printf does not supply new line automatically. Thus


‘\n’ is used
Integer Examples
 printf(“%d”,9678); 9 6 7 8

 printf(“%6d”,9678); 9 6 7 8

 printf(“%2d”,9678); 9 6 7 8

 printf(“%-6d”,9678); 9 6 7 8

 printf(“%06d”,9678); 0 0 9 6 7 8
Real Examples
Syntax: %w.pf
w indicates the number of digits used for display
p indicates the number of digits to be displayed after
decimal
Let y=98.7654;
printf(“%7.4f”,y); 9 8 . 7 6 5 4

printf(“%7.2f”,y); 9 8 . 7 7

printf(“-7.2f”,y); 9 8 . 7 7
Assignment
WAP to print exponential value of any real number.
WAP to read the values of x and y and print the
expressions
 x+y/x-y
 x+y/2
 (x+y)(x-y)
WAP to take 3 digit number from keyboard and do the
following
1. Sum of its digit.
2. Product of its digit.
3. Reverse if its digit.

You might also like