You are on page 1of 37

Operators and

expressions
Oyebola Akande (Ph.D)
Computer Science Department
Babcock University
Libraries in C standard library
 < stdio.h> -> defining I/O routines
 < ctype.h> -> defining character manipulation routines
 < string.h> -> defining string manipulation routines
 < math.h> -> defining mathematical routines
 < stdlib.h> -> defining number conversion, storage allocation and similar tasks
 < stdarg.h> -> defining libraries to handle routines with variable numbers of
arguments
 < time.h> -> defining time-manipulation routines
 < assert.h> -> defining diagnostic routines
 < setjmp.h> -> defining non-local function calls
 < signal.h> -> defining signal handlers
 < limits.h> -> defining constants of the int type
 < float.h> -> defining constants of the float type
C OPERATORS
Arithmetic operators
C provides five (5) arithmetic operators
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
The arithmetic operators are binary operators because
they operate on two operands.
C OPERATORS contd
C provides the remainder operator, %, which
yields the remainder after integer division.
The remainder operator is an integer operator that
can be used only with integer operands.
The expression x % y yields the remainder after x
is divided by y.
Thus, 7 % 4 yields 3 and 17 % 5 yields 2.
C OPERATORS contd
Relational operators
C provides six relational operators:
Operator Explanation
== (Equal to – equality)
!= (Not equal to – inequality)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
C OPERATORS contd
Assignment operators
=
 The syntax is:
Operand = operand
 Examples
int gross_pay = 0;
int tax = 0;
int net_pay = 0;
gross_pay = 12000;

A simple assignment changes the value of the operand


on the left of the operator to the value of the
expression on the right.
C OPERATORS contd
Compound assignment operators
Compound assignment allows you to write shorter
code statements and it allows for concise coding,
which can shorten execution time.
Examples:
compound assignment simple assignment
counter += 2 counter = counter +2
counter +=1 counter = counter + 1
counter*=1 counter = counter * 1
counter/=1 counter = counter/1
C OPERATORS contd
Logical operators
C provides the following logical operators
Operator Explanation
&& The AND operator
|| The OR operator
! The NOT operator
Both AND (&&) and OR (||) are binary operators and
NOT (!) is a unary operator
C OPERATORS contd
Increment and Decrement operators
Increment operator
 ++
 Pre-increment (++ var)

 Post-increment (var++)

Decrement operator
 --
 Pre-decrement (-- var)
 Post-decrement (var--)

var is a variable of integer type


C OPERATORS contd
Conditional Operator
The symbols ? and : make up the conditional operator
The symbols are used on three operands - it is a ternary
operator
The syntax is
condition ? expr1 : expr2
First the condition is evaluated.
If it's TRUE, the return value of the conditional
expression takes on the value of expr1.
If it's FALSE, the return value of the conditional
expression takes on the value of expr2.
C OPERATORS contd
Every expression has a datatype and produces a return
value
Because the conditional expression produces a value,
the return value of the conditional expression can be
assigned to a variable.
Therefore, the conditional operator provides a
shorthand version of the if-else statement:
if condition
expr1
else
expr2
C OPERATORS contd
The code
if (x > y)
max = x;
else
max = y;
If you use the conditional operator, the statement can be
rewritten like this:
max = (x > y) ? x : y;
The conditional statement corresponds exactly to the if-
else statement.
Illustration of equality and relational operators
PRECEDENCE AND ASSOCIATIVITY IN C
The order of evaluation in C expressions is governed
by the rules of precedence and associativity.
The rules of precedence are important due to the
complexity of expressions permitted in C.
Precedence identifies the operator that has the highest
rank and evaluates the operation based on this
criterion.
C operators, arranged in order of precedence

Operator Order of Precedence

Operators Description Associativity


() Parenthesized expression Left-to-right
[] Array subscript
. Member selection by
object
-> Member selection by
pointer
+- Unary + and - Right-to-left
++ -- Prefix increment and
prefix decrement
!~ Logical NOT and bitwise
complement
C operators, arranged in order of precedence
& Address-of
sizeof Size of expression or type
(type) Explicit cast to type such
as (int) or (double) Type
casts such as (int) or
(double)
*/% Multiplication and Left-to-right
division and modulus
(remainder)
+- Addition and subtraction Left-to-right
<< >> Bitwise shift left and Left-to-right
bitwise shift right
< <= Less than and less than or Left-to-right
equal to
> >= Greater than and greater
C operators, arranged in order of precedence

^ Bitwise Left-to-right
exclusive OR
| Bitwise OR Left-to-right
&& Logical AND Left-to-right
|| Logical OR Left-to-right
?: Conditional Right-to-left
operator
= Assignment Right-to-left
+= -= Addition
assignment and
subtraction
assignment
/= *= Division
assignment and
C operators, arranged in order of precedence

&= |= Bitwise AND


assignment and
bitwise OR
assignment
^= Bitwise exclusive
OR assignment
, Comma operator Left-to-right
EXPRESSIONS
An expression in a programming language is a
combination of variables, operators and functions.
Arithmetic Expression
Suppose x and y are variables
x+y, x-y, x*y, x/y, x%y: binary arithmetic
A simple statement: y = x+3∗x/(y−4);
Numeric literals like 3 or 4 valid in expressions
Semicolon ends statement (not new line i.e. \n)
x += y, x -= y, x *= y, x /= y, x %= y: arithmetic and
assignment
Exercise
Construct statements that do the following:
Decrease the variable x by 1.
Assigns to m the remainder of n divided by k.
Divide q by b minus a and assign the result to
p.
Assign to x the result of dividing the sum of a
and b by the product of c and d.
Exercise
Construct statements that do the following:
Decrease the variable n by 50
Assigns to z the remainder of r divided by k.
Divide q by b minus t and assign the result to p.
Assign to x the result of dividing the sum of a
and b by the product of c and d.
Exercise
1.Construct statements that do the following (or, in
other terms, have the following side effects):
Increase the variable x by 10.
Increase the variable x by 1.
Assign twice the sum of a and b to c.
Assign a plus twice b to c.
2.What is the numerical value of each of the following
expressions?
5 > 2
3 + 4 > 2 && 3 < 2
Operators
It specifies how an object can be manipulated. We
have numeric and string operations.
Operators can be unary ++, - -, binary +,-./, or ternary?
Operators
Order of operations:
Operator Evaluation direction
+,-(sign) right-to-left
*,/,%,+,-left-to-right
=,+=,-=,*=,/=,%= right-to-left
Use parentheses to override order of evaluation
Arithmetic Operators
 +……addition for examples
x=3+2; /∗constants∗/ y+z; /∗variables∗/ x+y+2; /∗both∗/
 -…..subtraction for examples
3−2; /∗constants∗/ int x=y−z; /∗variables∗/ y−2−z; /∗both∗/
 *….multiplication for examples
int x=3∗2; /∗constants∗/ int x=y∗z; /∗variables∗/ x∗y∗2; /∗both∗/

 /….division for examples


float x=3/2; /∗produces x=1 (int /) ∗/ float x=3.0/2 /∗produces x=1.5 (float /) ∗/ int x=3.0/2; /∗produces
x=1 (int conversion)∗/
 %....modulus (remainder)
int x=3%2; /∗produces x=1∗/ int y=7;int x=y%4; /∗produces 3∗/ int y=7;int x=y%10; /∗produces 7∗/
Relational Operators
> greater than: for example 3>2; /∗evaluates to 1 ∗/
2.99>3 /∗evaluates to 0 ∗/
>= greater than or equal to
< lesser than
<= lesser than or equal to
== equality operator
!= not equal to
Note that the "==" equality operator is different from the "=",
assignment operator.
LOGICAL OPERATORS
 && means AND for example
((9/3)==3) && (2∗3==6); /∗evaluates to 1 ∗/
(’A’==’a’) && (3==3) /∗evaluates to 0 ∗/
 || means OR
2==3 || ’A’==’A’; /∗evaluates to 1 ∗/
2.99>=3 || 0; /∗evaluates to 0 ∗/
 ! means NOT
!(3==3); /∗evaluates to 0 ∗/
!(2.99>=3) /∗evaluates to 1 ∗/
Increment and decrement operators
Postfix:
 x++ is a short cut for x=x+1
 x−− is a short cut for x=x−1
 y=x++ is a short cut for y=x; x=x+1. x is evaluated before it is incremented.
 y=x−− is a short cut for y=x; x=x−1. x is evaluated before it is decremented.
Prefix:
 ++x is a short cut for x=x+1
 −−x is a short cut for x=x−1
 y=++x is a short cut for x=x+1;y=x;. x is evaluate after it is incremented.
 y=−−x is a short cut for x=x−1;y=x;. x is evaluate after it is decremented.
Bitwise Operators
&-- AND 0x77 & 0x3; /∗evaluates to 0x3 ∗/ 0x77 &
0x0; /∗evaluates to 0 ∗/
|-- OR 0x700 | 0x33; /∗evaluates to 0x733 ∗/ 0x070 |
0 /∗evaluates to 0x070 ∗/
ˆ -- XOR 0x770 ^ 0x773; /∗evaluates to 0x3 ∗/ 0x33 ^
0x33; /∗evaluates to 0 ∗/
«-- left shift 0x01<<4; /∗evaluates to 0x10 ∗/
1<<2; /∗evaluates to 4 ∗/
»-- right shift
Conditional Expression
 if ( conditional )
x=<expressiona>;
else
x=<expressionb>;
Using ternary operator
 sign=x>0?1:−1; /* ? means if, use for giving the condition i.e. x>0
sign=1 and sign=-1 are the expression we are evaluating
: means else*/
if (x>0)
sign =1
else
sign=−1

 isodd=x%2==1?1:0;
if ( x%2==1)
isodd=1
else
isodd=0
Precedence and Order of Evaluation
 • ++,–,(cast), sizeof have the highest priority•
 *,/,% have higher priority than +,-
 ==,!= have higher priority than &&,||
 assignment operators have very low priority
 Use () generously to avoid ambiguities or side effects associated with
precendence of operators.
 y=x∗3+2 /∗same as y=(x∗3)+2∗/
 x!=0 && y==0 /∗same as (x!=0) && (y==0)∗/
 d= c>=’0’&& c<=’9’/∗same as d=(c>=’0’) && (c<=’9’) ∗/
Functions in string library (string.h)
 char *strcpy(s,ct) -> copy ct into s, including ``\0''; return s
 char *strncpy(s,ct,n) -> copy ncharcater of ct into s, return s
 char *strncat(s,ct) -> concatenate ct to end of s; return s
 char *strncat(s,ct,n) -> concatenate n character of ct to end of s, terminate with
``\0''; return s
 int strcmp(cs,ct) -> compare cs and ct; return 0 if cs=ct, <0 if cs0 if cs>ct
 char *strchr(cs,c) -> return pointer to first occurence of c in cs or NULL if not
encountered
 size_t strlen(cs) -> return length of cs
 (s and t are char*, cs and ct are const char*, c is an char converted to type int,
and n is an int.)
Using Constant
Character constant
A character constant is a single character that is written
in single quotes.
Example:

char alpha = 'a';


String constant
A string constant or string literal is a sequence of zero or
more characters enclosed in double quotes.
Example:
printf(“Welcome to c class”);
Using Special Constants
Escape Sequences
 C uses special character constants to represent unprintable
characters
 These characters usually perform control functions
 For example, '\n' is the new line sequence.
The statement below tells the program to start a new
line of text on screen.
 printf('\n');
The difference between a character constant and a
special character constant is the backslash
If you are using a special character constant on its
own, you must enclose it in single quotes
Using Special Constants contd
Some common examples of escape sequences are presented below:
Escape Sequence Description

\a sound (a beep on your


computer not used much
these days)
\b Backspace
\f Form-feed or page eject
\n Newline
\r Carriage return (for
printers) or move to the
beginning of the current
Using Special Constants contd
Symbolic Constants
Symbolic constants are defined using the preprocessor
directive #define.
The preprocessor replaces them with their
corresponding value before compilation takes place.
It is a standard ANSI C practice to use uppercase letters
for symbolic constant names so that it is easily
distinguishes from variable names.
Example
 #define PI 3.142

You might also like