Operators Expression
Operators Expression
Arithmetic operators
The arithmetic operators in C
Operator meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% modulo division
5
Arithmetic operators
Note:,
Integer division truncates remainder
The % operator cannot be applied to a float or double.
The precedence of arithmetic operators
Unary + or -
* / %
+ -
#include<stdio.h>
main()
{
int months , days ;
printf ( "Enter days \n “ ) ;
scanf ( "%d“ , &days ) ;
months = days / 30 ;
days = days % 30 ;
printf ( "Months =%d Days= %d \n", months, days);
}
Arithmetic expressions
An arithmetic expression is a combination of variables,
constants, and operators.
For example,
a*b-c a*b-c
(m+n)(x+y) (m+n)*(x+y)
ax2+bx+c a*x*x+b*x+c
8
Mathematical functions
Mathematical functions such as cos, sqrt, log, etc. are
frequently used in analysis of real-life problems. Table
3.9 on page 72 lists some standard math functions.
#include<math.h>
Mathematical functions
for example
the roots of ax2+bx+c =0 are
2
b b 4 ac
x 2a
Relational Operators
• The relational operators in C are :
Operator Meaning
< less that
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Relational Operators
A relational expression yields a value of 1 or 0.
5<6 1
-34 + 8 > 23 - 5 0
if a=3, b=2, c =1; then a > b > c is ?
3 >= 2 = = -4 < 0
Logical operators
op-1 op2 op-1&&op-2 op-1||op-2 !op-1
Non-zero Non-zero 1 1 0
Non-zero 0 0 1 0
0 Non-zero 0 1 1
0 0 0 0 1
Logical operators
The precedence of && is higher than that of ||, and
both are lower than relational operators, so
Assignment operators
#include<stdio.h>
main()
{
int a, b, c, d;
a = 15;
b = 10;
c = ++a - b;
printf ("a=%d b=%d c=%d\n", a, b, c );
d = b++ +a;
printf("a = %d b = %d d = %d\n", a, b, d );
printf("a/b = %d\n", a / b);
printf("a%%b = %d\n", a%b );
printf("a *= b = %d\n", a *= b );
printf("%d\n", ( c>d ) ? 1 : 0 );
printf("%d\n", ( c<d ) ? 1 : 0 );
}
#include<stdio.h>
main()
{
float a, b, c, x, y, z;
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 2 - 1;
y = a - b / ( 3+ c ) * ( 2 - 1 );
z = a - ( b / ( 3 + c ) * 2 ) - 1;
printf ( "x = %f \n", x );
printf(" y = %f \n", y ) ;
printf( "z = %f \n", z ) ;
}
float
float
double
int double