Programming in C: Operators, and Expressions
Programming in C: Operators, and Expressions
Part III
Operators, and Expressions
C-Operators
• Operators are used to manipulate data
– Perform specific mathematical or logical functions.
• C language provides the following types of
operators:
– Arithmetic Operators
– Relational Operators
– Logical Operators
– Assignment Operators
– Increment/Decrement Operators
– Other Operators (conditional and sizeof)
Arithmetic Operators
• Arithmetic operators are used to perform
numerical calculations among the values.
Precedence of Arithmetic Operators
Precedence of Arithmetic Operators
• (*,/, and %) are executed first, followed by (+,
and -).
• Operators of the same precedence are
executed sequentially(from left to right)
2+3-4+5 = ((2+3)-4)+5) = ((5-4)+5) = (1+5) = 6
• Parenthesis can be used to override the
evaluation or
(2+3)-(4+5) = 5-9 = -4
Relational Operators
• Relational Operators are used to compare two
quantities and take certain decision depending on
their relation.
– The specified relation is either true or false.
Logical Operators
• These operators are used for testing more than
one condition and making decisions. C language
has three logical operators they are:
Assignment Operators
• An assignment operator is used for assigning
the result of an expression to a variable.
• The most common assignment operator is the
equal sign (=) which refers to (←) in
algorithms.
Other Assignment Operators
Implicit Data type Conversion
• If the type of the values in an expression are
not the same, data type conversion is made.
• All the data types of the variables in that
expression are upgraded implicitly to the data
type of the variable with largest data type
according to the flowing order:
bool -> char -> int -> float -> double
Example
int a, x;
float z, y;
z = x + y;
/* x is first converted to float then x+y is evaluated and
assigned to z*/
a = x + y;
/* x is first converted to float then x+y is evaluated. The
result is then converted to int and assigned to a*/
z= a / x;
/* a/y is first evaluated. The result is then converted to
float and assigned to z */
Example
int a, x; // x=3
float z, y; // y=2.000000
z = x + y;
//z = 3+2.000000 = 3.000000+2.000000 = 5.000000
/* x is first converted to float then x+y is evaluated and
assigned to z*/
a = x + y;
//a = 3+2.000000 = 3.000000+2.000000 = 5
/* x is first converted to float then x+y is evaluated. The result
is then converted to int and assigned to a*/
z= a / x;
//z = 5/3 = 1.000000
/* a/y is first evaluated. The result is then converted to float
and assigned to z */
Explicit Data type Conversion
• Explicit type conversion is the process where
the user can define the type to which the
result is made of a particular data type.
• The syntax in C:
(type) expression;
Example