0% found this document useful (0 votes)
13 views

Programming in C: Operators, and Expressions

The document discusses various types of operators in the C programming language including arithmetic, relational, logical, assignment, increment/decrement, conditional, and sizeof operators. It provides examples of how each operator works and precedence rules that determine the order in which operations are performed in expressions that contain multiple operators. It also covers implicit and explicit type conversion that can occur when operands in expressions are of different data types.

Uploaded by

Yanis Kemoun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Programming in C: Operators, and Expressions

The document discusses various types of operators in the C programming language including arithmetic, relational, logical, assignment, increment/decrement, conditional, and sizeof operators. It provides examples of how each operator works and precedence rules that determine the order in which operations are performed in expressions that contain multiple operators. It also covers implicit and explicit type conversion that can occur when operands in expressions are of different data types.

Uploaded by

Yanis Kemoun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Programming In C

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

1) int x=7, y=5 ;


float z;
z =x / y; /*Here the value of z is 1.000000*/

2) int x=7, y=5;


float z;
z = (float)x / y; /*Here the value of z is 1.400000*/
Increment/Decrement Operators
• Two most useful operators which are present
in C are increment and decrement operators.
• Operators: ++ and --
• The operator ++ adds one to the operand
• The operator -- subtracts one from the
operand.
Prefix and Postfix
• Increment and decrement operators can be either
prefix or postfix forms
Postfix Vs Prefix form
Conditional Operator
• The conditional operator is used to construct
conditional expression of the form:
Syntax:
identifier=(test_expression)?expression1:expression2;
Meaning:
If test_expression is true then dentifier=expresion1,
otherwise identifier=expression2.
• Examples:
x=(y>0)?y:-y;// if y>0 then x=y else x=-y
min=(x<y)?x:y;// if x<y then min=x else min=y
Sizeof Operator
• Sizeof is an operator used to return the number of
bytes the operand occupies.
Example:
int i , j;
j = sizeof(i); // j=4 because i is an integer and occupies 4 bytes.
Sizeof Operator
• Another Example:
Precedence of C-operators

You might also like