OPERATORS IN C PROGRAMING PART-II



OPERATORS IN C PROGRAMING:
Assignment Operators:
        The assignment operators is represented by the equal singh (=). The variable appearing on the left side of = singh and it is assigned the vakue appearing on the right side of this sign.
       The assignment statement takes the following format:
Variable_name = expression;
      The expression can be a single variables or loteral, or a combination of variables, literals, and operators. In C, shortcuts are possible. For wxample, in place of the following statement:
a = a + b;
We may use the shorthand form as given below:
a += b;
Many similar assignment can be made as mentioned below:
a -= b;   is the same as a = a – b;
a *= b;  is the same as a = a * b;
a /= b;  is the same as a = a / b;
a %=b;  is the same as a = a % b;






Pointer Operators:
       The pointer operators are he adress-of operator (&) and the indirection operator(*). The pointer or indirection operator is used to get the content of the adress operand pointing to a particular memory element or cell. The & operator returns the address of the variable. For example, if we declare an integer variable by the statement:
int  salary;
then the pointer “&salary” returns the adress of the variable “salary” where the variable salary is stored in the main memory of the computer. Similarly, if you declare an integer pointer with the statement give below:
int *ptr;
then you may assign the address “&salary” to the pointer variable ptr using the statement
ptr = &salary;
         In this case the pointer “ptr” points to the address of “salary”. Using the operator * you can reach the content of the variable “salary”.  Actually, the value in *ptr is the content of the variable salary.
For Example:
#include<stdio.h>
main()  {
int salary;
int *ptr;
salary = 30000;
ptr = &salary;
printf("The value of salary is %d", salary);
printf("\nThe value pointed to is %d", *ptr);
*ptr = 33000;
printf("\nThe value of salary is %d", salary);  }
Output:






Bitwise Operators:
         Using the programming language C, we can get access to every bit of data available in the main memory. This would enable us to perform machine level operations.

Bitwise Operator
Meaning
&
Bitwise AND
||
Bitwise OR
^
Bitwise XOR
~
One’s complement
>> 
Right shift
<< 
Left shift

          In order to understand the bitwise operators, let us consider first the SHIFT operations.There are two types of shift operators that shift the bits in any integer variable by a specified number of positions. The “>>” operator shifts bits to the right, and the”<<” operator shifts bits to the left.
A.  The Right Shift Operator “>>”:
       We know that the decimal number 8 is represented in binary form as:
1000
Similarly, the decimal number 4 is represented as:
100
     If we represent data in a computer using 8 bits form, then decimal digits 8 and 4 will be represented by 00001000 and 00000100 respectively. The only difference are between these two numbers that the “1” in the second number is shifted one bit to your right. If the variable “a” holds the value “8”, then you can shift it right one position, by the statement:
a >> 1
     In this expression new number when converted to decimal value would be evaluated as “4”. You can shift a number by several bits. For example, you can change “8” into “2” by shifting the digit “1” by tow bits to the right by using the expression
a >> 2
In general, the right shift operator is used in the form:
variable >> number-of-bits
B. The Left Shift Operator “<<”:
        The left shift operator shifts bits to the left for a specified number of bit position. The expression takes the form:
variable << number-of-bits

For Example:

#include<stdio.h>
main()  {
int a = 8, b, c, d,e;
b = a << 1;
 c = a >> 1;
 d = a << 2;
e = a >> 2;
printf("The orignal number is %d", a);
printf("\nThe number shifted one bit to the left is %d", b);
printf("\nThe number shifted one bit to the right is %d", c);
printf("\nThe number shifted two bit to the left is %d", d);
printf("\nThe number shifted two bit to the right is %d", e);   }
Output:
OPERATORS IN C PROGRAMING PART-II OPERATORS IN C PROGRAMING PART-II Reviewed by vijay pratap singh on August 20, 2017 Rating: 5

No comments:

Powered by Blogger.