Professional Documents
Culture Documents
CSE287JUL2021 - LectureSlide - 04 (Operators+Expressions)
CSE287JUL2021 - LectureSlide - 04 (Operators+Expressions)
Computer Programming
Lecture 4:
Operators and Expressions in C
These operators are used for comparison. The result is boolean. All are binary operators.
Example
Operator Description (A=1, B=0)
Checks if the values of two operands are equal or not. If yes, then the condition (A == B) is not true.
== becomes true.
Checks if the values of two operands are equal or not. If the values are not equal, (A != B) is true.
!= then the condition becomes true.
Checks if the value of left operand is greater than the value of right operand. If yes, (A > B) is not true.
> then the condition becomes true.
Checks if the value of left operand is less than the value of right operand. If yes, then (A < B) is true.
< the condition becomes true.
Checks if the value of left operand is greater than or equal to the value of right (A >= B) is not true.
>= operand. If yes, then the condition becomes true.
Checks if the value of left operand is less than or equal to the value of right operand. (A <= B) is true.
<= If yes, then the condition becomes true.
Sample Code: Relational Operators
#include<stdio.h>
int main()
{
int a,b;
//Relational Operators: ==, !=, >, <, <=, >=
a=20;
b=20;
printf("%d\n",a==b);//Output: 1
printf("%d\n",a!=b);//Output: 0
printf("%d\n",a>b);//output: 0
printf("%d\n",a<b);//output: 0
printf("%d\n",a>=b);//output: 1
printf("%d\n",a<=b);//output: 1
return 0;
}
7
Logical Operators Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm
Example
Operator Description Type (A=1, B=0)
Called Logical AND operator. If both the operands Binary (A && B) is
&& are non-zero, then the condition becomes true. false.
These operators are used for bitwise logic operations. The operands must be integer values
Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm
Sample Code: Bitwise Operators
#include<stdio.h>
Explanations of the Bitwise Operations
int main()
a=60 00111100 a=60 00111100
{
b=30 00011110 ------------------------------
char a,b;
------------------------------ ~a= 11000011
//binary: &, |, ^, >>, <<, unary : ~ a&b=28 00011100
a=60; a=60 00111100
b=30; a=60 00111100 -------------------------------
printf("%u\n",a & b);//Output: 28 b=30 00011110 a<<2=240 11110000
printf("%u\n",a | b);//Output: 62 -----------------------------
a|b=62 00111110 a=60 00111100
printf("%u\n",a ^ b);//Output: 34
-------------------------------
a=60 00111100 a>>2=15 00001111
b=30 00011110
printf("%u\n",~a);//Output: ??
----------------------------
printf("%u\n",a<<2); //Output: 240
a^b=34 00100010
printf("%u\n",a>>2); //Output: 15
return 0;
12
}
Assignment Operators Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm
These operators assign the value of the expression on the right to the variable on the left
Operator Description Example
Simple assignment operator. Assigns values from right side operands C = A + B will assign the
= to left side operand value of A + B to C
+= Add AND assignment operator. It adds the right operand to the left C += A is equivalent to
operand and assign the result to the left operand. C=C+A
-= Subtract AND assignment operator. It subtracts the right operand from C -= A is equivalent to C
the left operand and assigns the result to the left operand. =C-A
Multiply AND assignment operator. It multiplies the right operand with C *= A is equivalent to
*= the left operand and assigns the result to the left operand. C=C*A
/= Divide AND assignment operator. It divides the left operand with the C /= A is equivalent to
right operand and assigns the result to the left operand. C=C/A
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
Precedence
x= 7/6*4+3/5+3
• If there are a chain of operations, then C
defines which of them will be applied first.
x= 1*4+3/5+3 • Example: *, / and % are higher in precedence that +
and -
x= 4+3/5+3 • Precedence can be altered by using
parentheses
• Innermost parentheses evaluated first
x= 4+0+3 • Example: 6+4/2 = 8 whereas (6+4)/2 = 5
Associativity
x= 4+3 • Evaluation order of operators of same
precedence is determined by associativity
x= 7 • All the operators associate from left to right
except for assignment operators
Precedence Chart Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm
f=(double)7/2;
printf ("%lf \n", f);
21