Operators
Kinds of Operators Precedence of Operators Type Casting
Classifications
Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special operators
Arithmetic Operators
Operator for arithmetic operation
Unary operator : +, Binary term operator : +, -, *, /, %
a-b a*b a+b -a*b a%b a/b
7/4 = 1 ; 9.0 / 2= 4.5; 9/4 = 2 ; 4/7 = 0; 4%7 = 4; 7%4 = 3; 9/2.0 = 4.5; 9.0/2.0 = 4.5;
% is purely integer operator.
9.0 % 2; 9%2.0; 9.0%2.0;
Integer arithmetic
Note: In modulo division, the sign of the result is always the sign of the first operand (dividend).
6/3 = 2 -6/3 = -2 7%3 = 1 -7%3 = -1
6/-3 = -2
-6/-3 = 2
7%-3 = 1
-7%-3 = -1
Precedence of arithmetic operators
High priority Low priority Left to right evaluation Ex: 1. a = 9, b = 12, c = 3 x = a b/3 + c*2 - 1 * / % + -
Precedence of arithmetic operators
Order of evaluation can be changed by introducing parentheses Ex: 1. a = 9, b = 12, c = 3 x = a b/(3 + c)*(2 1) For nested parentheses, inner most parentheses will be first evaluated. Ex: x = 9 ((12/3) + 3*2) 1
Relational Operators
Compare two value. Result : true (1)or false (0). Zero is false, non zero integer is true.
Relational Operators
Relational Operators
7>4 = 1 Examples: 7>9 = 0; 7<9 = 1; 7 == 7 = 1; 7 == 9 =0; 7 !=7 =0; 10<7+5 true a+b == c+d true if a+b is equal to c+d
7>=4 = 1; 7>=7 = 1; 9<=7 = 0; if at least one condition is true , result is true.
Logical Operators
Operators
&& Logical AND ! Logical NOT || Logical OR
a < b && b < c
An expression which combines two or more relational expressions is called as logical expression or compound relational expression.
Logical Operators Truth table
Op1 Non zero Op2 Non zero Op1 && Op2 1 Op1 || Op2 1
Non zero
0 0
0
Non zero 0
0
0 0
1
1 0
Assignment Operators
Expr1 = Expr1 op Expr2 Expr1 op= Expr 2
Ex:
sum = sum + i ;
sum += i ;
x = x * y + 1;
x *= y + 1;
x = x * (y+1)
Increment & Decrement Operators
Operator
++, -Prefix operator (evaluation and then assignment)
n = 1; x = ++n; // x=2, n=2
Postfix operator (assignment and then evaluation)
n = 1; x = n++; // x=1, n=2
Cannot use at expression, only at variable (a + b)++ // error Cannot apply at real type
Increment & Decrement Operators
Examples: 1. a[i++] = 10;
=>
a[i] = 10; i = i+1
2. m = n++ -j+10;
What is the output of the following C segments: ( Be careful )
x = 0 ; if ( x++ ) y = 1; else y = -1; printf (x=%d,y=%d\n, x,y);
What is the output of the following C segments: ( Be careful )
x = 0 ; if (++x) y = 1; else y = -1; printf (x=%d,y=%d\n, x,y);
The Conditional Operator
Expr1 ? Expr2 : Expr3 (3 Terms Operator)
if (x > y) max = x; else max = y;
max = x > y ? x : y ;
m = a > b ? (c > a ? c : a) : (c > b ? c : b) ;
Bitwise Operators
Operand should be integer type
Bitwise Operators
Precedence
Operator Precedence
~ << >> >>> & ^ |
(H)
(L)
Bitwise Operators
Bitwise AND
10012 & 00112 = 00012 To extract the special area in variable by masking that area
Bit OR
10012 | 00112 = 10112
Exclusive OR
10012 ^ 00112 = 10102
1s Complement
~ 000010102 = 111101012
Bitwise Operators
Bitwise Shift Operator
Shift left(<<)
x << y = x * 2y
Shift right(>>)
x >> y = x / 2y
Special operators
1. Ex: The Comma operator: used to link the related expressions together. Evaluated left to right has lowest precedence, parentheses are necessary.
value = (x=10,y=5,x+y);
Special operators
2. The sizeof operator: It returns the number of bytes the operand occupies. the operand can be a variable, constant or a data type qualifier. Ex: m = sizeof(sum); n = sizeof(long int); k = sizeof(235L);
Category Postfix Unary Multiplicative Additive Shift Relational Equality Bitwise AND Bitwise XOR Bitwise OR Logical AND Logical OR Conditional Assignment Comma
Operator () [] -> . ++ - + - ! ~ ++ - - (type) * & sizeof */% +<< >> < <= > >= == != & ^ | && || ?: = += -= *= /= %= >>= <<= &= ^= |= ,
Associativity Left to right Right to left Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Left to right Right to left Right to left Left to right
Operator Precedence
Example: 3 + 4*5 6/2 + 7*8 3 + 20 - 6/2 + 7*8 3 + 20 - 3 + 7*8 3 + 20 - 3 + 56 23 -3 +56 20 + 56 76
Operator Precedence
Example:
x = y += z -= 4 x = y += (z -= 4) x = (y += (z -= 4)) (x = y += (z -= 4))
Operator Precedence
a=x+y-z; b = -x ; c = -x++ ; d = -++x ; e = -x + z ; e = ++z + z--; f= p++ + p++; // Left Association // Right Association
Type conversion
Automatic type conversion
Operands of lower type is automatically converted to higher type and result is of higher type. 1. Float to int causes truncation of fractional part. 2. Double to float causes rounding of digits. 3. Long int to int causes dropping of excess higher order bits.
Type conversion
Example: int i,x; float f; double d; long int l;
Expression: x = l / i + i * f - d
long long
float
float
float
float double int double
Type Conversion
Casting a value
The process of local conversion is known as casting a value.
Type Conversion
Casting a value
Examples: x = (int)7.5 A = (int)21.3/ (int)4.5 B = (double)sum/n
Y = (int)(a + b)
P = cos ((double)x)
Managing I/O: Input-Output Statements, formatted I/O.
getchar()
Used to read a single character from the standard input unit (keyboard) #include<ctype.h> Ex: char name; name=getchar();
Variable name = getchar();
Character test functions
char character; character=getchar(); isalpha(character): it assumes a non-zero value(TRUE) if the argument character contains an alphabet, otherwise it assumes zero. isdigit(character)
Character test functions
Function isalnum(c) isalpha(c) isdigit(c) Test Is c an alphanumeric character? Is c an alphabetic character? Is c a digit?
islower(c)
isprint(c)
Is c a lower case letter?
Is c a printable character?
ispunct(c)
isspace(c)
Is c a punctuation mark ?
Is c a white space character?
isupper(c)
Is c an upper case letter?
Character test functions
char c; c=getchar(); if(isalpha(c)>0) printf(letter); else if(isdigit(c)>0) printf(digit); else printf(not alphanumeric);
putchar()
Used to write characters one at a time to the terminal putchar(variable name ); Ex: answer = Y; putchar(answer);
putchar()
Ex: char alphabet; alphabet = getchar(); if(islower(alphabet)) putchar(alphabet);
Formatted Input: Scanf () function
Its an input function, used to read values in run time. Its a call by address function. It is predefined function in stdio.h scanf (control string, &var1, &var2,-------);
If & is missing, variable contains garbage value.
Field specifications
The field specification for reading an integer number is
% wd
% wc
% ws
% conversion specification w field width of the number to be read d data type character in integer mode c character mode s string mode
I /O Formats
Data type Int Float Char format %d %f %c
Long
Double Unsigned String
%ld
%lf %u %s
% is called format specifier. %d : Decimal integer %0 : Octal integer %0x : Hexadecimal integer
Scanf () function
Example2: int a, b, c; scanf(%d %d %d, & a, &b, &c); Example3: int d, m, y; scanf(%d, %d ,%d, & a, &b, &c); Example4: int x; float y; char ch; scanf(%d %f %c, & x, &y, &ch); Order of the variables and order of formats must match.
Try this.. 1. scanf(%2d %5d, &a,&b); Input1: 50 31426 Input2: 31426 50 2. scanf(%d %*d %d, &a, &b); Input: 123 456 789
skipped because of *
gets()
recieves a string from keyboard until the user presses enter key. Spaces and tabs are acceptable as part of input string.
scanf(%s, a); If user enters sankar dayal sharma, only sankar is stored in array a. a
Sankar \0
gets(a); entire sankar dayal sharma is stored in array a. a
Sankar dayal sharma \0
scanf() reads characters until user press space bar.
scanf() reads only one word.
gets() reads characters until user presses enter key.
gets() function can read multiword strings. gets(arrayname)
Formatted function
Unformatted function
Formatted output: Printf()
It is an output function. Its a call by value function. It is predefined function in stdio.h. printf (control string, arg1, arg2,-------);
Example: int a = 25; Printf(%d,a);
printf()
Type the following and observe the results: int x = 25; float y = 10.68f; char ch = g; 1. printf(%d %f %c,x,y,ch); 2. printf(%d\n %f\n %c,x,y,ch); 3. printf(%d\t %f \t%c,x,y,ch); 4. printf(%d, %f, %c,x,y,ch); \n and \t cannot be used in scanf() but can be used in printf. printf(result is %d\n,x*2+y);
scanf()
Input function To read values at run time
printf()
Output function To display values
Call by address function.
deals with keyboard We cant write \n and \t It can contain only variables.
Call by value function.
deals with monitor Not allowed It can contain variables, messages and expressions.
puts()
It outputs a string to the screen. puts(GRIET);
puts()
Type the following and observe the results: 1. Printf(hyd\n); Printf(sec\n); Printf(cyb\n); 2. Puts(hyd); Puts(sec); Puts(cyb);
3. printf(%d,.); printf(%d,A); printf(%d,7); printf(%d,\0); 4. printf(%c,\0); 5. float a = 123.6789; printf(%.2f,a); printf(%.3f,a); printf(%f,a);
6. int a=4567; printf(%7d,a); printf(%.07d,a); printf(%2d,a); printf(%d,a); Try this.. Display 07:08:09
7. printf(\\n); printf(\\); printf(\); printf(%%); printf(); printf(%); printf(\%);