Operators, Expressions
& Built-in Function
Courtesy: Dr. Md. Alamgir Hosssain, Associate Professor, Dept. of ICT, IU
1/26
Operators
Q1. What is operator? Why is it used?
An operator is a symbol that tells the computer to perform
certain mathematical or logical manipulation.
Operators are used in programs to manipulate data and
variables.
2/26
Expression
In programming , an expression is any legal combination of symbols
and operands that represents a value.
Each programming language and application has its own rules for what
is legal and illegal
For example, in C language x+5 is an expression
Every expression consists of at least one operand and can have one or
more operators
x+5, where x and 5 are operands, and + is an operator.
3/26
Operators and Expressions
Q2. List different types of operators.
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
4/26
Arithmetic operators
Q3. List arithmetic operators.
Here are the most common arithmetic operators:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Reduction(Remainder from integer
division)
Note: *, / and % will be performed before + or - in
any expression.
5/26
Relational Operators
Q4. List relational operators
Relational Operators Description
== Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to
6/26
Logical Operators
Q5. list logical operators
Operator Meaning
&& logical AND
|| logical OR
! logical NOT
7/26
Assignment operator
Q6. Depict assignment operator. Write the general form of
this operator.
Assignment operator is = .
The assignment statement of the form
var = expression;
var = var;
8/26
Shorthand Assignment operators
Q7.Explain shorthand operators
The op= is known as the shorthand assignment
operator.
The assignment statement:
var +=expression; //means
var = var + expression;
Shorthand assignment operators are +=, -=, *=, /=, %=.
9/26
Shorthand Assignment Operators
Example
10/26
Assignment Operators Priority
Each assignment operator has a priority and they are
evaluated from right to left based on its priority.
Assignment operator’s priority is : =, +=, -=, *=, /=, %=.
11/26
Bitwise Operator
Q8. List bitwise operator.
C Bitwise Operators Description
& Bitwise AND
| Bitwise inclusive OR
^ Bitwise exclusive OR
<< Bitwise left shift
>> Bitwise right shift
~ one's complement
12/26
C increment and decrement operator
Q9. Depict increment and decrement operator
C provides two operators for incrementing and
decrementing the value of variables.
The increment operator ++ adds 1 to its operand.
The decrement operator -- subtracts 1 from its operand
13/26
Prefix and postfix
Q10. Explain prefix and postfix operator.
Prefix or postfix operators are as follows:
variable++;
variable--;
++variable;
--variable;
The expression ++variable increments variable before its
value is used,
whereas variable++ increments variable after its value has
been used.
14/26
Prefix and postfix Example
Q11. Distinguish between ++m and m++
Consider the following
m=5;
y=++m;
The value of y and m would be 6.
m=5;
y=m++;
The value of y would 5 and m would be 6.
15/26
Conditional Operator
Q12. Explain conditional operator.
The conditional or “ternary‘” operator is ?:
C constructs conditional expression of the form
e1 ? e2 : e3;
where e1,e2, and e3 are expressions.
If e1 is true , e2 will be evaluated first otherwise e3 will be
evaluated.
16/26
Compare Conditional Operator
& if..else statement
Q13.Compare Conditional Operator & if…else statement
A ternary operator “?:” is available in C to construct
conditional expressions of the form
exp1? exp2: exp3 ;
Consider the following statements.
a=10;
b=15;
x= (a>b)? a: b;
In other words, the conditional expression is sort of an
if/else statement.
if(a>b)
x=a;
else
x = b;
17/26
Special operator (1/2)
The [] operator is used to access elements of an array.
For example:
int a[5] = { 1, 2, 3, 4, 5 };
The unary * operator is used to dereference pointers.
For example:
int a = 1;
int *b = &a;
*b = *b + 1;
18/26
Special operator (2/2)
The '.' and '->' operators are used to access
members of a structure or union.
The sizeof operator returns the size in bytes of that
its operand takes up.
For example:
m=sizeof(sum);
n=sizeof(long int);
19/26
Symbolic constant
Q14. Define symbolic constant. Give an example
A symbolic constant is a constant represented by symbol.
A constant is defined as follows:
#define symbolic-name constant-value
Example:
#define PI 3.141593
#define TRUE 1
#define FALSE 0
20/26
INPUT AND OUTPUT FUNCTION
In ‘C’ language input and output of data is done by a collection
of library functions.
Example:
getchar() ;
putchar () ;
scanf () ;
printf () ;
gets();
puts () ;
21/26
GETCHAR FUNCTION
Q15. Explain getchar() function
getchar function reads a single character from standard
input. It takes no parameters and its returned value is the
input character. In general, a reference to the getchar
function is written as character
variable = getchar();
For example:
char c;
c= getchar () ;
The second line causes a single character to be entered from
the standard input device and then assigned to c.
22/26
PUTCHAR FUNCTION
Q16. Explain putchar() function
The standard C function that prints or displays a single
character by sending it to standard output is called putchar.
This function takes one argument, which is the character to
be sent.
Syntax:
int putchar(int char);
For example:
char ch=‘G’;
putchar(ch);
putchar (‘N’);
23/26
Puts and gets function
Q17. Explain gets() and puts() function
gets() function is capable of receiving multi-word strings.
puts() function is capable of displaying one-word strings.
For example:
char name[30];
gets(name);
puts(name);
24/26
printf and scanf function
A call to scanf () looks like this:
scanf( "conversion-string", &variable, ... );
For Example:
scanf(“%d”, &sum);
A call to printf looks like this:
printf( "format-string", expression, ... );
For example:
printf(“ Masters in Information Technology”);
printf(“ Sum is equal to:%d”, sum);
25/26
Thank You
26/26