Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, June 23, 2020

Unary operator in C




1) Increment/Decrement | Unary Operators:

Increment/decrement operator is also known as unary operator. Unary operators, meaning it operate on a singal operand. The increment operator (++) is used to increase by 1 to its operand and the decrement operator decrease by one 1 to its operand. 

For example: X++ (also can be written X = X + 1) and X-- (or X = X - 1) where, X is a variable which is called operand. And ++ and -- are increment and decrement operators.

Increment/decrement operators are of two types prefix and postfix.
Let's suppose 
x = 10.
++Increment operator will add 1 to its operand.x++ will result to 11
++x will result to 11
--Decrement operator will subtract 1 from an integer value.x-- will result to 9
--x will result to 9
Important note: ++ and -- used with variables (++ and -- operators need only one operand to perform an operation). Using ++ or -- with constant will result in error. Such as expressions like 10++(x+y)++ etc. are invalid.  

Stntax for increment/decrement operator

Increment/decrement operators are of two types prefix and postfix.
 
SyntaxDescriptionExample
++<variable-name>Pre increment++x 
<variable-name>++Post incrementx++ 
--<variable-name>Pre decrement--x 
<variable-name>--Post decrementx-- 

Let's consider an integer variable int x = 10. To increment x by 1, you can use either

x = x + 1  (Simple assignment)
x += 1 (Shorthand assignment)
x++ (Post increment)
++x (Pre increment)

Result of all the above code are same.


Example of ++ increment operator: 

Both post increment x++ (i.e x = x + 1) / pre increment ++x (i.e x = x + 1) does the same task of incrementing the value by 1 to its operand. 
 
  1. /* Example of ++ increment operator */
#include <stdio.h>
int main() { int X=3; X++; // Post increment X=X+1 printf("X=%d, X); ++X; // Pre increment X=X+1 printf("X=%d, X); return 0; }

Output of above program is x = 4 x = 5. Let us understand the code.

x++ Since we have used postfix notation (i.e x = x + 1). Hence, It will increments the current value of x by 1 to  4.

++x Here we have used prefix notation (i.e x = x + 1). Hence, It will also increments the current value of x by 1 to  5.


Example of -- decrement operator: 

Both post decrement x-- (i.e x = x-1) / pre-decrement --x w (i.e x = x-1) does the same task of decrementing the value by 1 to its operand. 
 
  1. /* Example of -- decrement operator */
#include <stdio.h>
int main() { int X=3; X--; // Post decrement X=X-1 printf("X=%d, X); --X; // Pre decrement X=X-1 printf("X=%d, X); return 0; }

Output of above program is x = 2 x = 1


Difference between prefix and postfix in C:


  1. /* Difference between prefix and postfix */

#include <stdio.h>
int main()
{ int a, b, c; a = 10; // a = 10 | Simply assign the value 10 to variable a b = ++a; // a=11, b=11 | Pre increment c = a++; // a=12, c=11 | Post increment printf("a=%d, b=%d, c=%d", a, b, c); return 0; }

Output of above program is a=12, b=11, c=11. Let us understand the code.

a = 10 Simply assigns 10 to variable a

b = ++a Since we have used prefix notation. Hence, first it increments the value of a to 11, then assigns the incremented value of a to b.

c = a++ Here we have used postfix notation. Hence, first it assigns the current value of a i.e. 11 to c, then increments the value of a to 12.





Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us