Computer Programming CMIS 1223
Operators and Arithmetic Expressions in C
Conducted by
Supun
Chamara
Thenuwara
Department of Computing and Information Systems
Operators
The operators(+,=,++,...) define how the variables and
literals(1,a,3,d,...) in the expression will be manipulated. C
supports several types of operators and they can be classified
as:
● Assignment operator
● Arithmetic operators
● Relational operators
● Logical operators
● Bitwise operators
●
Special operators (, => .)
● Pointer operators (*)
Pointer operators and special operators will be addressed in a
later chapter
Assignment Operator
● The assignment operator is the simple equal sign (=)
● The assignment operator is used to assign a value to a variable
● The format of an assignment statement is:
variable-name = expression;
Examples:
a = 5; // value of variable “a” becomes 5
a = 5+10; // value of variable “a” becomes 15
a = 5 + b; // value of variable “a” becomes 5 + value of b
a = b + c; // value of variable “a” becomes value of b + value of c
a = (x*x + y*y)/2;
a=a+b;
a+=b;
a -= b; // is same as a = a-b;
a *= b; // is same as a = a*b;
a /= b; // is same as a = a/b;
a %= b; // is same as a = a%b;
Arithmetic Operators
● Defined to do arithmetic operations(+, -, /, *, %,...)
Operator Action
+ Addition
- Subtraction
* Multiplication
/ Division (26/5=5)
% Modulo division (26%5=1, both values need to be integers)
++ Increment (a++ is Same as a=a+1 and Same as a+=1)
-- Decrement (a-- is Same as a=a-1 and Same as a-=1)
Relational and Logical Operators
● The relational operators are used to compare values forming
relational expressions (a>=4, a==5,...)
● The logical operators are used to connect relational expressions
together using the rules of formal logic(p AND q, p OR q,..,)
● Both types of expressions produce TRUE or FALSE results.
Operator Action
Relational Operators
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
== Equal
!= Not equal
Logical Operators
&& AND (a>=p && a==q)
|| OR (a>=p || a==20)
! NOT (!(a>=p) && a!=q)
Bitwise Operator
● Using C you we can access and manipulate bits in variables
● It allows us to Perform low level Operations
● There are 6 bitwise operators
Operator Action
& Bitwise AND (12 & 8=8)
| Bitwise OR (12 | 10=14)
^ Bitwise XOR ('A' ^ 32='a' and 'a'^32='A')
~ One's complement
>> Right shift (12 >>2=3)
<< Left shift (3<<2=12)
int c=a<<2; (Equivalent to “a” is multiplied by 4)
char c='B'^32; (Equivalent to converting “A” to its Lower case)
Relational and Logical Operators
● The relational operators are used to compare values forming
relational expressions (a>=4, a==5,...)
● The logical operators are used to connect relational expressions
together using the rules of formal logic(p AND q, p OR q,..,)
● Both types of expressions produce TRUE or FALSE results.
Operator Action
Relational Operators
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
== Equal
!= Not equal
Logical Operators
&& AND (a>=p && a==q)
|| OR (a>=p || a==20)
! NOT (!(a>=p) && a!=q)
Arithmetic Expression
A=3, B=5, c
c=++A+B++
c=++A+A++
c=++A+B*7/2+3
c=(++A+B)*7/(2+3) What is the Value of C?
c=A-- + ++A-25/5 What are the Values of A and c?
Sample Program
#include <stdio.h>
void main()
{
int A=3, B=5, c;
c=++A+B++;
printf( "A = %d, c = %d\n", A, c );
c=++A+A++;
printf( "A = %d, c = %d\n", A, c );
c=++A+B*7/2+3;
printf( "A = %d, c = %d\n", A, c );
c=(++A+B)*7/(2+3);
printf( "A = %d, c = %d\n", A, c );
c=A-- + ++A-25/5;
printf( "A = %d, c = %d\n", A, c );
}
Operator and implicit type conversion(casting)
● Smaller Data types are implicitly converted to larger data types
when a mix of them used within a statement
int=short, long=int+short, long=int+long, long=char, int=char,
long double=double+float, double=float, double=int
● Data types of the bigger size can be converted to smaller size if
they are not over flowed by the value of the BIGGER data type
char=int (within the range of ascii value can be converted)
int=double (only the integer part is converted)
float=int (integers less than some value can be converted)