0% found this document useful (0 votes)
8 views41 pages

Operators

Uploaded by

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

Operators

Uploaded by

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

Technical Training (C Basics)

Operators in ‘C’

In C language, operators are categorized in following six categories :


Arithmetic: Perform basic mathematical operations over one or more than one operands.
Relational and Logical operator: Relational operators are used to compare two expressions
and return the value as 0 and 1. Logical operator combines two or more operands.
Bitwise: Bitwise operator operates at bit level..
Assignment Operator: Assigns value.
Technical Training (C Basics)
Operators in ‘C’
Simplest meaning of operator is which works on operand.
Arithmetic:
Perform basic mathematical operations on operands.
1-Unary Operator (one operand)
2-Binary Operator (two operand)
Unary Operator (one operand)
Increment- The ++ operator is used to increment the value of integer.
a++ post increment ++a pre increment
Decrement- The -- operator is used to decrement the value of integer.
a-- post decrement --a pre decrement
Technical Training (C Basics)
Operator Definition Example Unary or Binary

+ adds two operands a+b binary

- Subtracts second operands from first. a-b binary

* multiply two operands a*b binary

/ Divides numerator by denominator. a/b binary

% Gives remainder after an integer division. a%b binary

++ Increase an integer value by one. ++a , a++ unary

-- Decrease an integer value by one. --a , a-- unary


Technical Training (C Basics)

#include <stdio.h>
int main(void)
{
float a=5.0,b=2.0,c;
c=a % b;
printf("%f",c);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 1.0 B. 5.0
C. 2.0 D. Error
Technical Training (C Basics)

#include <stdio.h>
int main(void)
{
float a=5.0,b=2.0,c;
c=a % b;
printf("%f",c);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 1.0 B. 5.0
C. 2.0 D. Error
Technical Training (C Basics)

#include <stdio.h>
int main(void)
{
int a=5,b=2,c;
c=a++ + ++b + b++ + ++a + a++ + ++b;
printf("%d",c);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 22 B. 32
C. 24 D. 30
Technical Training (C Basics)

#include <stdio.h>
int main(void)
{
int a=5,b=2,c;
c=a++ + ++b + b++ + ++a + a++ + ++b;
printf("%d",c);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 22 B. 32
C. 24 D. 30
Technical Training (C Basics)

#include <stdio.h>
int main(void)
{
int a=5,b=3,c;
c=a-- + --a + b-- + --b + --b + --a;
printf("%d",c);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 22 B. 12
C. 24 D. Compiler
dependent
Technical Training (C Basics)

#include <stdio.h>
int main(void)
{
int a=5,b=3,c;
c=a-- + --a + b-- + --b + --b + --a;
printf("%d",c);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 22 B. 12
C. 24 D. Compiler
dependent
Technical Training (C Basics)

#include <stdio.h>
int main(void)
{
int a=5;
printf("%d%d%d",a++,++a,++a);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 7 7 6 B. 5 7 8
C. 5 6 7 D. Compiler
dependent
Technical Training (C Basics)

#include <stdio.h>
int main(void)
{
int a=5;
printf("%d%d%d",a++,++a,++a);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 7 7 6 B. 5 7 8
C. 5 6 7 D. Compiler
dependent
Technical Training (C Basics)

#include <stdio.h>
int main()
{
int i = 3;
printf("%d", (++i)++);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 4 B. error
C. 5 D. Compiler
dependent
Technical Training (C Basics)

#include <stdio.h>
int main()
{
int a = 4;
printf("%d", (++a)++);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 4 B. error
C. 5 D. Compiler
dependent
Technical Training (C Basics)

Bitwise Operator: In C bitwise operator works at bit level.


Operator Definition

& Bitwise And

| Bitwise Or

^ Bitwise XOR

~ Bitwise NOT

>> Right Shift Operator

<< Left Shift Operator


Technical Training (C Basics)

Bitwise Operator:
points to remember-
1.Left and right shift operators should not be used for negative numbers.
2.if the number is shifted more than the size of integer the behavior is undefined.
3.The bitwise operator should not be used in place of logical operators.
Technical Training (C Basics)

#include <stdio.h>
int main()
{
int i = 3;
printf("%d", (++i)++);
return 0;
}

What would be the output of the above code ? Choose


the correct option.
A. 4 B. error
C. 5 D. Compiler
dependent
Technical Training (C Basics)
Operators in ‘C’
In C language, operators are categorized in following six categories :
Logical Operator: Perform basic mathematical operations over one or more than one
operands.
Operator Definition
&& Logical And
|| Logical Or
Technical Training (C Basics)
Operators in ‘C’
In C language, operators are categorized in following six categories :
Assignment Operator:
It is a binary Operator
Evaluates the operator on Right side and assigns it to on Left side.
Example
Technical Training (C Basics)
Operators in ‘C’
In C language, operators are categorized in following six categories :
Operators Type Associativity
() [] -> . Left to right
! ~ ++ -- + - * & (type) sizeof Unary Right to left
* / % Binary Left to right
+ - Binary Left to right
<< >> Binary Left to right
< <= > >= Binary Left to right
== != Binary Left to right
& Binary Left to right
^ Binary Left to right
| Binary Left to right
&& Binary Left to right
|| Binary Left to right
?: Binary Right to left
= += -= *= /= %= &= |= ^= >>= <== Binary Right to left
Technical Training (C Basics) TCS

1 #include <stdio.h>
2 int a = 2; What would be the output of the above code ? Choose
3 int a; the correct option.
4 int main(void) { A. 2 0 1 2 3 B. 2 2 2 2 3
5 printf("%d", a);
6 int a; C. 2 1 1 2 2 D. Compiler error
7 for(int i = 0 ; i < 3 ; i++ )
8 {
9 printf("%d", a);
10 a++;
11 }
12 printf("%d", a);
13 return 0;
14 }
Technical Training (C Basics) TCS

1 #include <stdio.h>
2 int a = 2; What would be the output of the above code ? Choose
3 int a; the correct option.
4 int main(void) { A. 2 0 1 2 3 B. 2 2 2 2 3
5 printf("%d", a);
6 int a; C. 2 1 1 2 2 D. Compiler error
7 for(int i = 0 ; i < 3 ; i++ )
8 {
9 printf("%d", a);
10 a++;
11 }
12 printf("%d", a);
13 return 0;
14 }
Technical Training (C Basics) Capegemini

1 #include <stdio.h>
2 int a = 12; What would be the output of the above code ? Choose
3 int main(void) { the correct option.
4 for(int j = 0; j <= 1; j++) A. 4 B. 12
5 {
6 int a = 4; C. 5 D. Compiler error
7 a++;
8 }
9 printf("%d" , a);
10 return 0;
11 }
Technical Training (C Basics) Capegemini

1 #include <stdio.h>
2 int a = 12; What would be the output of the above code ? Choose
3 int main(void) { the correct option.
4 for(int j = 0; j <= 1; j++) A. 4 B. 12
5 {
6 int a = 4; C. 5 D. Compiler error
7 a++;
8 }
9 printf("%d" , a);
10 return 0;
11 }
Technical Training (C Basics) TCS

1 #include <stdio.h>
2 int main(void) {
3 for(int i = 0; i < 2; i++)
4 {
5 static int a = 4;
6 a++;
7 }
8 printf("%d", a);
9 return 0;
10 }

What would be the output of the above code ? Choose


the correct option.
A. 4 B. 5
C. 6 D. Error
Technical Training (C Basics) TCS

1 #include <stdio.h>
2 int main(void) {
3 for(int i = 0; i < 2; i++)
4 {
5 static int a = 4;
6 a++;
7 }
8 printf("%d", a);
9 return 0;
10 }

What would be the output of the above code ? Choose


the correct option.
A. 4 B. 5
C. 6 D. Error
Technical Training (C Basics) Capegemini

1 #include <stdio.h> What would be the output of the above code ? Choose
2 int a; the correct option.
3 int main(void) {
A. 0 1 B. 2 3
4 {
5 a = 2; C. 3 4 D. Error
6 }
7 for(int i = 0; i < 2; i++)
8 {
9 a++ ;
10 printf("%d " , a);
11 }
12 return 0 ;
13 }
Technical Training (C Basics) Capegemini

1 #include <stdio.h> What would be the output of the above code ? Choose
2 int a; the correct option.
3 int main(void) {
A. 0 1 B. 2 3
4 {
5 a = 2; C. 3 4 D. Error
6 }
7 for(int i = 0; i < 2; i++)
8 {
9 a++ ;
10 printf("%d " , a);
11 }
12 return 0 ;
13 }
Technical Training (C Basics) Infosys

1 #include <stdio.h> What would be the output of the above code ? Choose
2 int main(void) { the correct option.
3 int x = 1 ;
A. 1 10 0 B. 1 10 1
4 printf("%d", x ); {
5 int x = 10; C. 1 10 10 D. Error
6 printf("%d", x ); {
7 x;
8 printf("%d", x );
9 }
10 }
11 return 0;
12 }
Technical Training (C Basics) Infosys

1 #include <stdio.h> What would be the output of the above code ? Choose
2 int main(void) { the correct option.
3 int x = 1 ;
A. 1 10 0 B. 1 10 1
4 printf("%d", x ); {
5 int x = 10; C. 1 10 10 D. Error
6 printf("%d", x ); {
7 x;
8 printf("%d", x );
9 }
10 }
11 return 0;
12 }
Technical Training (C Basics) Accenture

1 # include<stdio.h> What would be the output of the above code ? Choose


2 int a = 10; the correct option.
3 int main(void) { A. 20 20 B. 30 20
4 {
5 a = 30; C. 10 20 D. 20 30
6 {
7 {
8 a = 20;
9 printf("%d", a);
10 }
11 }
12 printf("%d", a);
13 }
14 return 0;
15 }
Technical Training (C Basics) Accenture

1 # include<stdio.h> What would be the output of the above code ? Choose


2 int a = 10; the correct option.
3 int main(void) {
A. 20 20 B. 30 20
4 {
5 a = 30; C. 10 20 D. 20 30
6 {
7 {
8 a = 20;
9 printf("%d", a);
10 }
11 }
12 printf("%d", a);
13 }
14 return 0;
15 }
Technical Training (C Basics) TCS

1 #include<stdio.h> What would be the output of the above code ? Choose


2 int main(void) { the correct option.
3 int a = 7;
4 int s = a++ + ++a + a-- + a % 2; A. 28 B. 25
5 printf("%d", s); C. 29 D. 30
6 }
Technical Training (C Basics) TCS

1 #include<stdio.h> What would be the output of the above code ? Choose


2 int main(void) { the correct option.
3 int a = 7;
4 int s = a++ + ++a + a-- + a % 2; A. 28 B. 25
5 printf("%d", s); C. 29 D. 30
6 }
Technical Training (C Basics) Capegemini

1 #include<stdio.h> What would be the output of the above code ? Choose


2 int main(void) { the correct option.
3 int i = 1, j = 2, k, l = 1;
A. 2 1 1 B. 3 2 1
4 {
5 k = i++ + ++i + j || l++; C. 3 1 1 D. 2 2 1
6 printf("%d %d %d", i, l, k);
7 }
8 return 0;
9 }
Technical Training (C Basics) Capegemini

1 #include<stdio.h> What would be the output of the above code ? Choose


2 int main(void) { the correct option.
3 int i = 1, j = 2, k, l = 1;
A. 2 1 1 B. 3 2 1
4 {
5 k = i++ + ++i + j || l++; C. 3 1 1 D. 2 2 1
6 printf("%d %d %d", i, l, k);
7 }
8 return 0;
9 }
Technical Training (C Basics) Infosys

1 #include <stdio.h> What would be the output of the above code ? Choose
2 int main(void) { the correct option.
3 int a = 5;
A. 3 B. 4
4 int s = a-- || a-- && 4;
5 printf("%d", s); C. 2 D. 1
6 }
Technical Training (C Basics) Infosys

1 #include <stdio.h> What would be the output of the above code ? Choose
2 int main(void) { the correct option.
3 int a = 5;
A. 3 B. 4
4 int s = a-- || a-- && 4;
5 printf("%d", s); C. 2 D. 1
6 }
Technical Training (C Basics) Accenture

1 #include<stdio.h> What would be the output of the above code ? Choose


2 int main(void) { the correct option.
3 int i = 0, j = 2, k, l = 0;
{ A. 1 0 2 B. 1 1 3
4
5 k = (i++ && l++ ) || j++ + i++; C. 2 0 3 D. 2 1 2
6 printf("%d %d %d", i, l, j);
7 }
8 return 0;
9 }
Technical Training (C Basics) Accenture

1 #include<stdio.h> What would be the output of the above code ? Choose


2 int main(void) { the correct option.
3 int i = 0, j = 2, k, l = 0;
{ A. 1 0 2 B. 1 1 3
4
5 k = (i++ && l++ ) || j++ + i++; C. 2 0 3 D. 2 1 2
6 printf("%d %d %d", i, l, j);
7 }
8 return 0;
9 }
Technical Training (C Basics) Infosys

1 #include<stdio.h> What would be the output of the above code ? Choose


2 int main(void) { the correct option.
3 int i = 10, j = 10;
int k = 4; A. 4 5 B. 4 4
4
5 { C. 5 4 D. 5 5
6 printf("%d", sizeof(k /= i+j));
7 printf("%d", k) ;
8 }
9 return 0;
10 }
Technical Training (C Basics) Infosys

1 #include<stdio.h> What would be the output of the above code ? Choose


2 int main(void) { the correct option.
3 int i = 10, j = 10;
int k = 4; A. 4 5 B. 4 4
4
5 { C. 5 4 D. 5 5
6 printf("%d", sizeof(k /= i+j));
7 printf("%d", k) ;
8 }
9 return 0;
10 }

You might also like