Wednesday, 6 June 2018

Unary and Binary operator







Unary Operator 

Unary operator , single operand पे use किये जाते है , जिससे उस operand की एक नई value generate हो जाती है


Unary Operator के प्रकार 
1. unary minus (-)
2. increment (++)
3. Decrement (--)
4. NOT (!)
5. Address of Operator (&)
6. sizeof()

1. unary minus-
int =-10 ;
2. Increment :
a. Prefix increment 
example 1.1    

 #include<stdio.h>
  int main()
{
int a= 10;
++a;
printf("%d\n",a);
return 0;
}
Output : 11 
example1. 2 
#include<stdio.h>
int main()
{
int a= 10;
printf("%d\n",++a);
return 0;
}
Output : 11 

b . Post increment 

example 1.1    

 #include<stdio.h>
  int main()
{
int a= 10;
a ++;
printf("%d\n",a);
return 0;
}
Output : 11 
example1. 2 
#include<stdio.h>
int main()
{
int a= 10;
printf("%d\n",++a);
return 0;
}
Output : 10  

3 . Decrement  operator 

a  Prefix decrement

example 1.1  

#include<stdio.h>

  int main()

{

int a= 10;

--a ;

printf("%d\n",a);

return 0;

}

Output : 9 



example1. 2 

#include<stdio.h>

int main()

{

int a= 10;

printf("%d\n",--a);

return 0;

}

Output : 9 



4. NOT (!)
example 1.1 
#include<stdio.h>
int main()
{
int a= 10;
printf("%d\n",!a);
return 0;
}
Output : 0 

example1. 2 
#include<stdio.h>
int main()
{
int a= 0;
printf("%d\n",!a);
return 0;
}

Output : 1  

5. Address of Operator(&) 
#include<stdio.h>
int main()
{
int a= 15;
printf("%d\n",&a);
return 0;
}
Output :8672 

6. sizeof () operator 
#include<stdio.h>
int main()
{
printf("%d\n",sizeof(float));
return 0;

}
output : 4 

Binary Operator 


Binary Operator , दो operand के बीच में use किया जाता है | ये bitwise operator भी कहलाते है|
Bitwise operator  में evaluation  bit के आधार पे किया जाता है |


operator signMeaning of operator sign 
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise Compliment
<<Bitwise Left Shift
>>Bitwise Right Shift


Bitwise AND(&) Operator

#include<stdio.h>
            int main()
            {
            int a= 10,b=25;
            printf("%d",a&b);
            return 0;
            }
Output:  8

Bitwise OR(|)Operator

            #include<stdio.h>
            int main()
            {
            int a= 10,b=25;
            printf("%d",a|b);
            return 0;
            }
Output:- 27

Bitwise XOR or exclusive OR(^) Operatror

            #include<stdio.h>
            int main()
            {
            int a= 10,b=25;
            printf("%d",a^b);
            return 0;
            }
Output: 19

Bitwise Compliment(~):

#include<stdio.h>
int main()
{
int a= 10,b=-25;
printf("%d\n",~a);
printf("%d",~b);
return 0;

}
Output:
-11
24

Shift Operator in c

Right Shift OPerator(>>)

#include<stdio.h>

int main()

{

int a= 10,b=25;

printf("%d\n",a>>2);

return 0;

}
Output:2

Left Shift Operator(<<)
#include<stdio.h>
int main()
 {
int a= 10,b=25
 printf("%d\n",a<<2);
 return 0;
 }
Output: 40

Question:

1 . find output
main()
{
Int o=1%2, p=sizeof(float);
Int q,r=5,s=10/2,t;
q=r>>2&&p>31-12&4||r/q+3;
t=(p>q)?(s*r-2):(o/4&2);
printf(“%d%d”,o,s);
}
MCQ
 which of the following is a binary operator group?
    A.  Conditional operator,bitwise XOR 
    B.  <<,+=,dot
    C.  Indirection,|,~
    D.  (int,++

    2. What will be output if you will compile and execute the following c code?
 #include int main()
{
 int a=5;
 float b;
 printf("%d",sizeof(++a+b));
 printf(" %d",a);
 return 0;
}

A) 2 6
B) 4 6
C) 2 5
D) 4 5

3 . What is the output of the following code?
main()
{
int a, b; a=b=4; b=a++;
printf("%d %d %d %d", a++, --b, ++a, b--);
 }
 A) 5 3 7 3
 B) Syntax error
 C) 5 4 5 3
 D) 6 2 6 4

4. The operator & is used for
A)  Bitwise AND
B)  B) Bitwise OR
C)  C) Logical AND
D)  D) Logical OR

 When applied to a variable, what does the unary “&” operator yield?
 A) The variable’s address
B) The variable’s right value
C) The variable’s binary form
D) The variable’s value


54 Assuming var1 has value 20. What will following code print?
Printf(“%d %d\n”, var1--, ++var1);
 A) 20 20
 B) 19 20
C) 20 21

D) 21 21


11 What will be output if you compile and execute the following ‘C’ code?
void main()
{
 int i=4,x;
x=++i + ++i + ++i;
printf("%d",x);
 }
A) 21
B) 18
C) 12

D) Compilation error

True/False

1.Sizeof function that returns size of variable in bytes.
7.           Size of short integer and long integer can be verified using the sizeof() operator.

28.           Right shift of an unsigned integer by one bit is equivalent to multiplying it by two.
2.5 J++ executes faster than J+1 because ++ is faster than +.
2.           sizeof(‘a’) is not 1
19.   If m = 5, ++m + ++m is equal to 12.
31.   Size of short integer and long integer can be verified using the sizeof() operator.

 32.   Right shift of an unsigned integer by one bit is equivalent to multiplying it by two

Question:
1.  What will be printed as the result of the operation below?
main()  

int x=20,y=35; 
x=y++ + x++; 
y= ++y + ++x;
 printf(“%d %d \n”,x,y); 
}


2. Explain working of bit-wise exclusive OR and shift left operators in C with example.

3. What will be printed as the result of the operation below?
main()
 { int x=20,y=35;
 x=y++ + x++; y= ++y + ++x;
printf(“%d %d \n”,x,y);
 }

4. 9 What will be output of the operations below?
main()
{
int a=10,b=25;
a=b++ + a++;
b= ++b + ++a;
printf(“%d %d \n”,a,b);
}

1 comment:

ch-12 File Handling

Mcq 1. What does fp point to in the program?   #include<stdio.h> int main() {   FILE *fp;   fp=fopen("trial",...