Operators and Expressions in C Language
Operators and Expressions in C Language
Operators in C Programs
OBJECTIVES LEARNING OUTCOMES (LO)
5. Apply the different elements in 2. Write a C program, compile it,
C with programming commands link it, execute it and debug it.
and code. 11. Use strings and libraries in
6. Explain the process of program C.
debugging with 12. Recognize a program written
documentations. in C.
13. Solve a simple real world
problems by using
programming in C. 1
Different types of Operators are available in C
language used for different types of
processing and computations in programs.
Some of these operators are:
1. Arithmetic operators.
2. Assignment operators.
3. Relational operators.
4. Logical operators.
5. Increment & decrement operators.
6. The Conditional operator.
2
•Some operators operate on one operand
(called Unary Operand Operators),
• others operate on two operands (called
Binary Operand Operators)
• while others operate on three operands
(called Ternary Operand Operators).
4
•Examples of Algebraic expressions and
their corresponding forms in C programs.
5
Implicit conversion from integer to real
values in Arithmetic Expressions:
1. An operation between two integer
values always yields an integer result.
2. An operation between an integer value
and a real value always yields a real
result.
3. An operation between two real values
always yields a real result.
6
Examples:
7
Note:
In an assignment statement:
1. If the right hand expression results in
an integer value and assigned to an
integer variable, then the result will
integer.
9
Example: If two variables are declared:
int k; float a;
10
Precedence and Associativity in Arithmetic
Expressions:
Any arithmetic expression will be evaluated
as the following precedence of operators:
12
•Examples of how to consider precedence of
operators and type conversion in writing some
Algebraic Expressions in C programs:
13
3.2 Assignment Operators:
•All these operators are Binary Operand
Operators.
•They operate on variables and constants.
Some of these operators are listed below:
14
15
3.3 Relational Operators:
•All these operators are Binary Operand
Operators.
•They operate on integer and real
variables and constants in order to
perform comparison operations between
their values.
•The result of any relational operation is
either 1 (True) or 0 (False).
Relational operators with some examples
are listed below: 16
17
3.4 Logical Operators:
•Used with variables and constants of
integer data types only.
•In C language the number 0 is considered
as False. All other positive or negative
integers are considered as True.
•The result of any logical operation is either
1 (True) or 0 (False).
•Logical Operators are used in programs to
join multiple relational expressions.
Logical operators are listed below:
18
19
3.5 The Conditional Operator:
•Two symbols are used with it ‘?’ and ‘:’ .
•It is a ternary operator since it operates
on three expressions.
Syntax:
Expression1 ? Expression2 : Expression3
•If expression 1 is true (if its value is non-
zero), then the value returned from all the
operation will be expression 2.
•If expression 1 is false the value returned
will be expression 3. 20
This operator is sometimes used in
programs instead of if else conditional
statement.
Example: (Draw Flowchart)
#include <stdio.h>
void main()
{int x= 8, y;
y= x<5 ? -6: 13 ;
printf(“y=%d \n”, y); }
21
Note: Flowchart for ? : Operator.
#include <stdio.h>
void main()
{ 7!=7 ? printf(“Good”): printf(“Bad”);
}
22
3.6 The Increment (++) and Decrement
(- -) Operators:
•Both operators operate on variables only.
•Both are Unary operand.
23
24
3.7 The Mathematical Library Functions:
•A number of mathematical library
functions available in library of C compiler.
•Used to perform mathematical operations
such as calculation of the square root, sin,
cos, ...etc which can not be performed
using the available C Arithmetic operators.
•The directive #include<math.h> must be
used when any mathematical library
function is used in the program. 25
26
27
28
Exercise Problems Pages 32, 33: Solve all
problems that not solved in the class.
29