top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Unary Arithmetic Operators in C++

+1 vote
477 views

Unary operators in C++ 

The Unary operators in C++ use only a single operand. The unary operator is either specified before or after the operand depending on the usage. C++ provides the following unary arithmetic  operators:

  1. Negation operator
  2. Increment operator and Decrement Operator
 

1. Negation Operator (-)

The negation operator negates the associated operand and returns the negated value of the associated operand. The negation operator is specified to the left of the operand.

Examples:

Int x;

X = -3

Here, the constant value 3 will be negated and assigned to the variable x.

int new, old = 5;

new = - old;

Here the value of the variable ‘old’ is negated and assigned to the variable ‘new’. Hence, the variable ‘new’ will be assigned the value -5.

2. Increment (++) and decrement (--) operators

Consider that a school register is automated and with the entry of the details of every student, the student count is incremented by 1. The student count is associated to the variable ‘std_count’. This assignment is done using the following code statement:

Std_count = std_count+1;

For example, the same could be coded as:

Std_count++;

     OR

++std_count; 

The increment operator consists of two addition signs that follow each other without any spaces in between.

The decrement operator is the opposite of the increment operator. For instance, in the example, in case there was a student who was leaving the school, then when his details are deleted from the school register, the student count should also decrease by 1. This could be represented as:

Std_count=std_count – 1;

While using the decrement operator, the same is represented as:

std_count--;  OR  --std_count;

The decrement operator consists of two subtraction signs following each other without any spaces in between.

Using increment or decrement operators increases the readability of the code.

The difference between pre- and post- fixing the operator is useful when it is used in an expression. When the operator precedes the operand, increment or decrement operation takes place after using the value of the operand.

Consider the following,

a = 10;

b = 5;

c = a*b++;

In the expression, the current value of b is used for the product and then the value of b is incremented. That is, C is assigned 50 (a*b) and then the value of b is incremented to 6.

If however, the expression was

C =  c * ++b;

The value stored in c would be 60, because b would first be incremented by 1, and then the value stored in c (10 * 6). The use of the increment and decrement operators in both pre- and post-fixing formats has been illustrated in example:-

#include <iostream.h>

Void main(void)

{

 int value;

value = 1;

cout<” The Increment Operator” <<endl;

cout<<”Value of Value:” <<value <<endl;

cout<<”Pre-fix increment operator (++value):”<<++value;

cout<<endl;

cout<”value of value:” <<value <<endl;

cout<<”Post-fix increment operator (value++): ” <<value++;

cout<<endl;

cout<<”value of value:” <<value <<endl;

cout<< “\n\n The decrement operator”<<endl;

cout<<”value of value: ”<< value <<endl;

cout<< “Pre-fix decrement operator (--value):” << -- value;

cout<<endl;

cout<<”value of value:” <<value<<endl;

cout<<”Post-fix decrement operator (value--):” <<value- -;

cout<< endl;

cout<<”value of value:” <<value <endl;

}

Output:

The Increment Operator

Value of value:1

Using the pre-fix increment operator (++value) : 2

Value of value: 2

Using the Post-fix increment operator (value++): 2

Value of value: 3

The Decrement Operator

Value of value: 3

Using the pre-fix decrement operator (--value) : 2

Value of value:2

Using the post-fix decrement operator (value --): 2

Value of value: 1

posted Jul 31, 2017 by Rameshwar Singh

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

In this article I will discuss about the various arithmetic operations in C. Lets discuss them under the following three sub topics -

1. Increment/Decrement And Addition/Subtraction
2. Difference & Comparison
3. Invalid operations

1. Increment/Decrement And Addition/Subtraction

Increment or decrement of a pointer is dependent on the size of the variable on which we are applying the increment/decrement operation.

new value of the pointer =  current value (address) +  i * size_of(data type) 
new value of the pointer =  current value (address) -  i * size_of(data type)  

Behavior with the array
When pointer is pointing to the array and if we increase the pointer then the new value (address) contains would be

New Address of Pointer = Address of Pointer + i*(Size of Data Type)*(Size of Array)

Check the following example

#include<stdio.h>

int main(){

 float var[5]={1.01, 2.02, 3.03, 4.04, 5.05};
 float(*ptr)[5];

 ptr=&var;
 printf("Value inside ptr : %u",ptr);

 ptr=ptr+1;
 printf("Value inside ptr : %u",ptr);

 return 0;
}

Sample Output

Value inside ptr : 100 (if)
Value inside ptr : 120 

One should always remember that only integer can be added to a pointer variable. But for subtraction we can subtract one pointer from another if they are of same type.

2. Difference & Comparison

Two pointer can be subtracted if they hold same memory space and same type.

ptr2 - ptr1 = (Address of ptr2 - Addres of ptr1) / sizeof(*ptr1 or *ptr2)

Comparison of two pointer is perfectly valid in C even if they hold different type of variable, see the following example

int main()
{
 int *ptr1;
 float *ptr2;

 ptr1 = (int *)1;
 ptr2 = (float *)2;

 if(ptr2 > ptr1)
   printf("Ptr2 is far from ptr1");

 return(0);
}

3. Invalid operations

Following operations are invalid operations with pointers
1. Addition of two pointers
2. Multiplication of two pointers/one pointer and one variable(int/float etc)
3. Division between two pointers/One pointer and one variable (int/float etc)
4. Modulo operation on any pointer
5. Any Bitwise operation, negate operation

READ MORE
...