Operators and Hierarchy
Operators and Hierarchy
Types of Operators
Increment/
Arithmetic Logical Bitwise
Decrement
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
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)
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
double
float
int
Syntax:
(datatype) expression
Ex:
int x,y;
float x=(float)x/y;
Input/Output
Types of Operations
Input Output
Ex:
char variable_name;
variable_name=getchar();
Writing a Character
putchar(variable_name);
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….);
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);
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(“%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.