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

Xii Computer Operators

The document provides an overview of operators in the C programming language, including definitions and classifications such as unary, binary, and ternary operators. It explains operator precedence, arithmetic operators, relational operators, logical operators, and assignment operators, along with examples for each. Additionally, it includes sample C programs demonstrating the calculation of areas and swapping of numbers.

Uploaded by

am9252766
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 views7 pages

Xii Computer Operators

The document provides an overview of operators in the C programming language, including definitions and classifications such as unary, binary, and ternary operators. It explains operator precedence, arithmetic operators, relational operators, logical operators, and assignment operators, along with examples for each. Additionally, it includes sample C programs demonstrating the calculation of areas and swapping of numbers.

Uploaded by

am9252766
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

Operators in C language

Q. What is Operator?
An operator is a symbol that is used to perform certain mathematical or logical operation. Operator
is used to manipulate data. A statement containing operators and operands is called an expression.
An operand can be a variable, constant or an expression. For example
C=A+20
C programming language supports various operators to perform various operations like logical,
mathematical, relational, arithmetic, bitwise operators etc.
Classification of operators with respect to number of operands:
There are three types of operators with respect to number of operands:
Unary Operator: Those that requires one operand. (Such as ++)
Binary Operator: Those that requires two operands. (Such as *)
Ternary Operator: Those that requires three operands. (Such as : ?)
Q. What is operator precedence?
An expression in C may contains more than one operands and operators. C language has a
predefined rule of priority for evaluation of the operators. This rule of priority of operators is called
operator precedence. For example
X = 1 + 2 * 3; // X will be assigned 7.
In C, precedence of arithmetic operators ( *, %, /, +, -) is higher than relational operators(==, !=,
>, <, >=, <=) and precedence of relational operator is higher than logical operators(&&, || and !).
Below table lists the operators in decreasing order of their precedence.
Operator Meaning of Operator Associativity
() Parenthesis Left to right
! Logical NOT Right to left
+ Unary plus
- Unary minus
++ Increment
-- Decrement
* Multiply Left to right
/ Divide
% Remainder
+ Binary plus(Addition) Left to right
- Binary minus(subtraction)
< Less than Left to right
<= Less than or equal
> Greater than
>= Greater than or equal
20 | P a g e OPERATORS IN C LANGUAGE
== Equal to Left to right
!= Not equal to
&& Logical AND Left to right
|| Logical OR Left to right
?: Conditional Operator Right to left
= Simple assignment Right to left
*= Assignment product
/= Assignment quotient
%= Assignment remainder
+= Assignment sum
-= Assignment difference

Q. What are arithmetic operators in C.


C Arithmetic operators are used to perform mathematical operations. There are five fundamental
arithmetic operators supported by C language, which are addition(+), subtraction(-), multiplication(-
), division(/) and modulus(%) of two numbers. All arithmetic operators compute the result of
specific arithmetic operation and returns its result.
Operator Description Syntax Example

+ Adds two numbers a+b 15 + 5 = 20

- Subtracts two numbers a-b 15 - 5 = 10

* Multiplies two numbers a*b 15 * 5 = 75

/ Divides numerator by a/b 15 / 5 = 3


denominator

% Returns remainder after a%b 15 % 5 = 0


an integer division

Q. Define Relational operators with suitable examples.


Relational Operators in C programming are used to compare two values. It specifies the relation
between two values like equal, greater than, less than etc. Relational operators always returns Boolean
value (zero for false and non-zero value for true). For Example (A > B) : It checks whether A is
greater than B or not. It will return none-zero(true) if A is greater than B otherwise zero(false).

Below is the list of Relational Operators Supported in C. if A=5 and B=10.


21 | P a g e OPERATORS IN C LANGUAGE
Relational Example Result Description
Operator

> A>B FALSE Checks if A is greater than B

< A<B TRUE Checks if A is less than B

>= A >= B FALSE Checks if A is greater than or equal to B

<= A <= B TRUE Checks if A is less than or equal to B

== A == B FALSE Checks if A is equal to B

!= A != B TRUE Checks if A is not equal to B

Q. What are logical operators?


A logical operator is used to compare or evaluate logical and relational expressions. Logical Operators
always produce Boolean results, either TRUE (non-zero value) or FALSE (zero value).
Below is the list of Logical Operators Supported in C. if c=5 and d=2.
Operator Meaning Expression Result

Logical AND. True only if all


&& ((c==5) && (d>5)) FALSE
operands are true. Its binary operator

Logical OR. True only if either one


|| ((c==5) || (d>5)) TRUE
operand is true. Its binary operator

Logical NOT. True only if the operand


! !(c==5) FALSE
is FALSE. It’s a unary operator

Q. Define an increment or decrement statement in C.


22 | P a g e OPERATORS IN C LANGUAGE
In C language, increment ++ operator increases the value of a variable by 1 and decrement -
- operator decreases the value of a variable by 1. These both are unary operators. For example a=5
and b=9.
a++ // a becomes 6
b-- // b becomes 8
These two operators are used in two different ways prefix and postfix. Prefix increment operator
++var increments the value by 1 then, it returns the value. For example a=5
b=++a // b will be assigned value 6
Postfix increment operator var++ returns the value first then increments the value by 1. For
example a=5.
b=a++ // b will be assigned value 5.
Q. Define assignment operator in C.
Assignment Operator in C is used to assign a value to a variable. "=" is called simple assignment
operator. It assigns values from right side operands(R value) to left side operand (L value). For
Example. A=34, A=B+C etc
Q. What are arithmetic assignment operators in C?
Arithmetic assignment operators are combination of arithmetic and assignment operators. They first
perform arithmetic operation between left and right operand and then assign the result to left
operand. For example
A+=5; is equivalent to A = A+5;
It first add 5 to A, and then assign result to A.

Operator Example Equivalent Description


Expression

+= A += B; A = A+B; It Adds A and B, then assign result to A

-= A -= B; A = A-B; It subtract B from A, then assign result to A

/= A /= B; A = A/B; It divides A by B, then assign result to A

*= A *= B; A = A*B; It multiply A and B, then assign result to A

%= A %= B; A = A%B; It takes modulus of A by B, then assign result A

Conversion of Mathematical expressions into its equivalent C expressions

23 | P a g e OPERATORS IN C LANGUAGE
Mathematical Expression Equivalent C expression
abc a*b*c
1 1/2
2
x2 pow(x,2)

√x sqrt(x)
cos x cos(x)
|a| abs(a)
Points to remember while converting a mathematical expression into its equivalent C expression.
• All operands and operators should be in one line
• No brackets are allowed except ( )

Examples.
Mathematical Expression Equivalent C expression
1
x=2ab4 x=1/2*a*b*4

4
y=3 r3 Y=4/3*pow(r,3)
(x + y)n pow(x+y,n)
−b ± √b 2 − 4ac x=(-b-sqrt(pow(b,2)-4*a*c))/2*a
x= x=(-b+sqrt(pow(b,2)-4*a*c))/2*a
2a
y=|b2 − 4ac|
y=abs(pow(b,2) – 4*a*c

b2 −c3
x= x=pow(b,2)-pow(c,3)/2*b*c
2bc

Q. Write a program to calculate the area of a rectangle/ square (Hint A=w x h)


24 | P a g e OPERATORS IN C LANGUAGE
include<stdio.h>
int main(void)
{
int area,width,length;
printf("\n Enter length:");
scanf("%d",&length);
printf("\n Enter width:");
scanf("%d",&width);
area=length*width;
printf("\n Area is %d ",area);
return(0);
}

Q. Write a program that reads radius of a circle and displays the circles area. Use this
formula Area = PI x radius x radius where PI is the constant whose value is 3.14159.

include<stdio.h>
#define PI 3.14159
int main (void)
{
float radius, area;
printf ("\nEnter radius of circle:");
scanf ("%f",&radius);
area = PI*radius*radius;
printf ("\n Area of circle is =%.2f",area);
return(0);
}
Q. Write a program that swaps two inputted numbers.
#include<stdio.h>
int main(void)
{
int a,b,temp;
printf("\n Enter value of a:");
scanf("%d",&a);
printf("\n Enter value of b:");
scanf("%d",&b);
printf("\n Before Swapping");
printf("\n a=%d b=%d",a,b);
temp=a;
a=b;
b=temp;
printf("\n after Swapping");
25 | P a g e OPERATORS IN C LANGUAGE
printf("\n a=%d b=%d",a,b);
return(0);
}

26 | P a g e OPERATORS IN C LANGUAGE

You might also like