0% found this document useful (0 votes)
11 views19 pages

Operators

The document provides an introduction to computer programming in C, covering the structure of C programs, data types, operators, and input/output statements. It details various types of operators including arithmetic, relational, logical, bitwise, assignment, conditional, and special operators, along with their descriptions and examples. Additionally, it discusses operator precedence and includes sample code snippets demonstrating the use of these operators.

Uploaded by

aggroups2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views19 pages

Operators

The document provides an introduction to computer programming in C, covering the structure of C programs, data types, operators, and input/output statements. It details various types of operators including arithmetic, relational, logical, bitwise, assignment, conditional, and special operators, along with their descriptions and examples. Additionally, it discusses operator precedence and includes sample code snippets demonstrating the use of these operators.

Uploaded by

aggroups2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

23ECE105 COMPUTER PROGRAMMING

Prepared by,
Mrs. SUMITHRA R. P.
UNIT I

Introduction, structure of C program, variables, data types, storage classes,


constants, enumeration constant, keywords, operators, expressions,
input/output statements, assignment statement conditional statements;

Number system: binary, decimal, hexadecimal, conversion between


number system types;

Introduction to tools – IDE, compilation, linking, debugging.


Operators

 C language supports a rich set of built-in operators.


 An operator is a symbol that tells the compiler to perform a
certain mathematical or logical manipulation.
 Operators are used in programs to manipulate data and variables.
 C operators can be classified into following types:
• Arithmetic operators
• Relational operators
• Logical operators
• Bitwise operators
• Assignment operators
• Conditional operators
• Special operators
Arithmetic operators
Opera Description Example
tor
+ Adds two operands. A + B = 30
− Subtracts second operand from the A − B = -10
first.
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of B%A=0


after an integer division.
++ Increment operator increases the A++ = 11
integer value by one.
-- Decrement operator decreases the A-- = 9
integer value by one.
Relational operators
Assume variable A holds 10 and variable B holds 20 then −
Operator Description Example
== Checks if the values of two operands are equal or (A == B) is not true.
not. If yes, then the condition becomes true.
!= Checks if the values of two operands are equal or (A != B) is true.
not. If the values are not equal, then the
condition becomes true.
> Checks if the value of left operand is greater (A > B) is not true.
than the value of right operand. If yes, then the
condition becomes true.
< Checks if the value of left operand is less than (A < B) is true.
the value of right operand. If yes, then the
condition becomes true.
>= Checks if the value of left operand is greater (A >= B) is not true.
than or equal to the value of right operand. If
yes, then the condition becomes true.
<= Checks if the value of left operand is less than or (A <= B) is true.
equal to the value of right operand. If yes, then
the condition becomes true.
Logical Operators
Opera Description Example
tor
&& Called Logical AND operator. If both the operands (A && B) is false.
are non-zero, then the condition becomes true.

|| Called Logical OR Operator. If any of the two (A || B) is true.


operands is non-zero, then the condition becomes
true.

! Called Logical NOT Operator. It is used to reverse !(A && B) is true.


the logical state of its operand. If a condition is
true, then Logical NOT operator will make it
false.
Bitwise Operators
Operator Description Example

& Binary AND Operator copies a bit to the result if it exists (A & B) = 12, i.e., 0000
in both operands. 1100
| Binary OR Operator copies a bit if it exists in either (A | B) = 61, i.e., 0011 1101
operand.
^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49, i.e., 0011 0001
operand but not both.
~ Binary One's Complement Operator is unary and has the (~A ) = ~(60), i.e,. -0111101
effect of 'flipping' bits.
<< Binary Left Shift Operator. The left operands value is
moved left by the number of bits specified by the right A << 2 = 240 i.e., 1111 0000
operand.

>> Binary Right Shift Operator. The left operands value is


moved right by the number of bits specified by the right
operand. A >> 2 = 15 i.e., 0000 1111
Bitwise Operators

 Assume if A = 60; and B = 13;


 now in binary format they will be as follows:
 A = 0011 1100 B = 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Assignment Operators
Operator Description Example
= assigns values from right side operands to left a=b
side operand

+= adds right operand to the left operand and a+=b is same as a=a+b
assign the result to left

-= subtracts right operand from the left operand a-=b is same as a=a-b
and assign the result to left operand

*= mutiply left operand with the right operand a*=b is same as a=a*b
and assign the result to left operand

/= divides left operand with the right operand a/=b is same as a=a/b
and assign the result to left operand

%= calculate modulus using two operands and a%=b is same as a=a%b


assign the result to left operand
Conditional operator

 The conditional operators in C language are known by two


more names
1. Ternary Operator
2. ? : Operator
It is actually the if condition that we use in C language decision
making, but using conditional operator, we turn the if condition
statement into a short and simple operator.
The syntax of a conditional operator is :
expression 1 ? expression 2: expression 3
Special operator
Operator Description Example
sizeof Returns the size of an sizeof(x) return size
variable of the variable x

& Returns the address of an &x ; return address


variable of the variable x

* Pointer to a variable *x ; will be pointer


to a variable x
Operators Precedence in C
 Operator precedence determines the grouping of terms in an
expression and decides how an expression is evaluated.
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right


Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right


Give the output

#include "stdio.h"
int main()
{
int x, y = 5, z = 5;
x = y == z;
printf("%d", x);

return 0;
}
Ex:2

#include <stdio.h>

int main()
{
int i = 1, 2, 3;

printf("%d", i);

return 0;
}
Output
10 24
11 25
12 26
EX.3

#include <stdio.h>

int main()
{
int i = (1, 2, 3);

printf("%d", i);

return 0;
}
#include <stdio.h>

int main()
{
int a = 1;
int b = 1;
int c = a || --b;
int d = a-- && --b;
printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
return 0;
}
OUTPUT :a = 0, b = 0, c = 1, d = 0
#include <stdio.h>  expression c > b > a is evaluated
as ( (c > b) > a ). And since the (c
int main()
> b) is being the relational
{ operator it will return 1 if True
otherwise 0 is if False. So here
int a = 10, b = 20, c = 30; the value returned is 1 and then
if (c > b > a) it is compared to the a. so now,
the statement becomes, (1 > a),
printf("TRUE"); which is false, so the answer,
else return is 0, therefore, else part is
executed.
printf("FALSE");
return 0;
}
# include <stdio.h>  The main statement in
question is "x += y +=
int main()
10". Since there are two
{ += operators in the
int x = 10; statement, associativity
comes into the picture.
int y = 20; Associativity of
x += y += 10; compound assignment
operators is right to left,
printf (" %d %d", x, y); so the expression is
return 0; evaluated as x += (y +=
10).
}
Thank you

You might also like