Chapter3-Formulas and Operators
Chapter3-Formulas and Operators
Yongseok Son
Department of Computer Science and Engineering
Chung-Ang University
Operator
An operator is a symbol that tells the compiler to perform specific m
athematical or logical functions
C language is rich in built-in operators and provides the following ty
pes of operators
Arithmetic operators
• They take numerical values (either literals or variables) as their operands a
nd return a single numerical value
• The standard arithmetic operators are addition(+), subtraction(-), multiplicat
ion(*), and division(/)
Relational operators
• They tests or defines some kind of relation between two entities
• These include numerical equality (e.g., 5 == 5) and inequalities (e.g., 4>=
3)
slide 2
Operator
Logical operators
• Logical operators are used to evaluate two or more conditions
• An expression containing logical operator returns either 0 or 1 dependi
ng on whether expression results true or false
• There are 3 logical operations in C (e.g., AND (&&), OR(||), and logical
NOT(!)
Bitwise operators
• They operate on one or more bit patterns at the level of individual bits
• For example, & (bitwise AND) takes two numbers as operands and do
es AND on every bit of two numbers. The result of AND is 1 only if bot
h bits are 1
Assignment operators
• They are used to give a variable the value
• Values for the variables are assigned using assignment operators
The equal sign (=) is an assignement operator
slide 3
Arithmetic Operators
The following table shows all the arithmetic operators
supported by the C language
Assume variable A holds 10 and variable B holds 20
slide 4
Prefix and Postfix Operators
Depending on the position of the operator, prefix or
postfix operators can be classified
Suppose you use ++ operator as prefix like: ++var
• The value of var is incremented by 1 then, it returns the value
Suppose you use ++ operator as postfix like: var++
• The original value of var is returned first then, var is
incremented by 1
#include <stdio.h>
int main()
{
int var=5;
slide 5
Prefix and Postfix Operators
Example
#include <stdio.h>
int main()
{
int x=10, y=10;
printf(“x=%d\n”, x); <Results>
x=10
printf(“++x’s result = %d\n”, ++x); ++x’s result = 11
printf(“x=%d\n\n”, x); x = 11
y=10
printf(“y=%d\n”, y); y++’s result = 10
printf(“y++’s result = %d\n”, y++); y = 11
printf(“y=%d\n”, y);
return 0;
}
slide 6
Relational Operators
The following table shows all the relational operators
supported by C language
Assume variable A holds 10 and variable B holds 20
slide 7
Relational Operators
Example
#include <stdio.h> if ( a > b ) {
printf("Line 3 - a is greater than b\n" );
main() { } else {
printf("Line 3 - a is not greater than b\n" );
int a = 21; }
int b = 10;
int c ; /* Lets change value of a and b */
a = 5;
if( a == b ) { b = 20;
printf("Line 1 - a is equal to b\n" );
} else { if ( a <= b ) {
printf("Line 1 - a is not equal to b\n" ); printf("Line 4 - a is either less than or equal to b\n" );
} }
if ( a < b ) { if ( b >= a ) {
printf("Line 2 - a is less than b\n" ); printf("Line 5 - b is either greater than or equal to b\n" );
} else { }
printf("Line 2 - a is not less than b\n" ); }
}
slide 8
Logical Operators
Following table shows all the logical operators supported
by C language
Assume variable A holds 1 and variable B holds 0
slide 9
Logical Operators
Example
#include <stdio.h>
Int main() {
int a = 5;
int b = 20;
int c ;
if ( a && b ) {
printf("Line 1 - Condition is true\n" );
}
if ( a || b ) {
<Result>
printf("Line 2 - Condition is true\n" );
Line 1 - Condition is true
}
Line 2 - Condition is true
Line 3 - Condition is not true
/* lets change the value of a and b */
Line 4 - Condition is true
a = 0;
b = 10;
if ( a && b ) {
printf("Line 3 - Condition is true\n" );
} else {
printf("Line 3 - Condition is not true\n" );
}
if ( !(a && b) ) {
printf("Line 4 - Condition is true\n" );
}
}
slide 10
Bitwise Operators
Bitwise operators supported by C language
Assume variable A holds 60 and variable B holds 13
A = 0011 1100
B = 0000 1101
A & B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
slide 11
Bitwise Operators
Following table shows all the bitwise operators
supported by C language
Assume variable A holds 60 and variable B holds 13
A = 0011 1100
B = 0000 1101
Operator Description Example
Binary AND Operator copies a bit to the result if it exists in both
& (A & B) = 12,( i.e., 0000 1100)
operands.
| Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, (i.e., 0011 1101)
Binary XOR Operator copies the bit if it is set in one operand
^ (A ^ B) = 49, (i.e., 0011 0001)
but not both.
Binary Left Shift Operator. The left operands value is moved left
<< A << 2 = 240, (i.e., 1111 0000)
by the number of bits specified by the right operand.
Binary Right Shift Operator. The left operands value is moved
>> A >> 2 = 15, (i.e., 0000 1111)
right by the number of bits specified by the right operand.
slide 12
Bitwise Operators
Example
#include <stdio.h>
int main()
{
int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a&b); // The result is 00000001
printf("a|b = %d\n", a | b); // The result is 00001101
printf("a^b = %d\n", a^b); // The result is 00001100
printf("b<<1 = %d\n", b << 1); // The result is 00010010
printf("b>>1 = %d\n", b >> 1); // The result is 00000100
return 0;
}
a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
b<<1 = 18
b>>1 = 4
slide 13
Assignment Operators
The following table lists the assignment operators supported by C language
Operator Description Example
Simple assignment operator. Assigns values from right side operands C = A + B will assign the value of A + B to
=
to left side operand C
Add and assignment operator. It adds the right operand to the left
+= C += A is equivalent to C = C + A
operand and assign the result to the left operand.
Divide and assignment operator. It divides the left operand with the
/= C /= A is equivalent to C = C / A
right operand and assigns the result to the left operand.
slide 14
Assignment Operators
Example
#include <stdio.h>
int main()
{
int Total=0, <Result>
int i; Total = 45
for(i=0;i<10;i++)
{
Total+=i; // This is same as “Total = Total+I”
}
printf("Total = %d", Total);
}
slide 15
Misc Operators
Operator Description Example
sizeof(a), where a is integer, will
sizeof() Returns the size of a variable.
return 4.
&a; returns the actual address of the
& Returns the address of a variable.
variable.
* Pointer to a variable. *a;
If Condition is true ? then value X :
?: Conditional Expression.
otherwise value Y
slide 16
Misc Operators
Conditional Expression
True
Expression1
exp1 ? exp2 : exp3
True ? : False
False
Expression2 Expression3
Results
slide 17
Misc Operators
Example
#include <stdio.h>
int main()
{
int a, b, c; <Result>
a = 2; 7
b = 7;
c = (a > b) ? a : b;
printf(“%d\n”, c);
}
slide 18
Automatic Type Conversion
“Lower” types are promoted to “higher” types
The expression itself will have the type of its highest operand
The type hierarchy is as follows:
long double
double
float
int
short, char
If either operand is long double, convert the other to long
double
slide 19
Automatic Type Conversion
Example of type implicit conversion
#include<stdio.h>
int main()
{
int x = 10; //integer x
char y = 'a'; //character c
slide 20
Type Casting
Type casting refers to changing an variable of one data
type into another
The compiler will automatically change one type of data into
another
An example would be the conversion of a floating point value
to integer value
Programmers can enforce type conversion to a variable
Examples
<Example 1> <Example 2>
double x = 3.5; double x = 3.5;
double y = 2.7; printf(“integer number of x = %d\n”, (int)x);
double below_point;
below_point = x * y – (int)(x * y);
slide 21