answersLogoWhite

0


Best Answer
  1. include

    main()

{

int b=5;

int c= (b++) + (++b) + (++b) + (++b);

printf("%d",c);

}

Ans: The answer will be 27.

The answer will be 32

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

When you use a post-increment (n++) you are first making a copy of the variable. This is the value used in your statement. The variable will be incremented, but not until the current line is done executing. When it finishes, the incremented value will replace n.

When you use a pre-increment (++n) you are first making a copy of the variable, as in post-increment. The difference is that the value you copied will be incremented right away. This value will then be copied back into n after the current line is done executing

Just remember that post-increment happens AFTER you use it, and pre-increment happens right when you use it. Try running the following code to see it in action.

int main(int argc, char** argv) {

int n = 0;

printf("%d\n", n); // this would print out 0, since n = 0

printf("%d\n", n++); // this would print out 0, since n isn't incremented until after this line

printf("%d\n", n); // now n would print out as 1

printf("%d\n", ++n); // now n would print out as 2

// more illustrative examples:

printf("%d\t%d\n", n++, n); // this time we see that both ns are printed out as 2

printf("%d\n", n); // n is only shown as 3 after the post-increment line

printf("%d\t%d\n", ++n, n); // and now we see that the first n is printed as a 4

// while the second is still a 3

printf("%d\n", n); // and once again n is set to 4 after the last line

return 0;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain more about post and pre increment with examples?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is x plus equals y is post increment or pre increment?

The '+=' operator behaves like a pre increment operator.


How can you differentiate overloading of pre-fix and post-fix increment operator?

The prefix increment operator is overloaded as operator++() while the postfix increment operator is overloaded as operator++(int).


What are applications of pre-increment and post-increment operators why these operators behaviour are made like this is there any reason for such behaviour?

All operators must return a value, and it is this return value (the evaluation of the operation) that differentiates the pre-increment and post-increment operators. If the return value is of no concern, then the pre-increment operator is the preferred version, particularly when working with objects. This is because the post-increment operator's implementation will generally make a copy of the original object which it will return after incrementing the original object. If the return value is of no concern, then making a copy adds an expensive overhead (this doesn't apply to primitive data types since CPU optimisation can pre-empt the return value without requiring a copy to be made). Applications for post-increment and pre-increment are many and varied, but ultimately the goal is to increment the value. Whether you use post-increment or pre-increment is largely down to whether you need to use the return value or not, and whether you need to use the original value or the incremented value. Although the same thing can be achieved with longhand code, it is more efficient to use the appropriate post-increment or pre-increment operators. For instance: int x=1; int y=x; ++x; Can be implemented more efficiently with: int x=1; int y= x++; Whereas: int x=1; ++x; int y=x; Can be implemented more efficiently with: int x=1; int y= ++x; Use caution when pre-incrementing the same value in a compound statement. For example: int x=0; int y= ++x * ++x; The expected result is 2, but the actual result is 4. This is because both expressions (++x) return the same value: a reference to x. And since x is incremented twice, the expression evaluates to 2*2, not 1*2. To get the expected result of 2, each operation must be evaluated separately: int x=0; int y= ++x; y*= ++x;


What is increment and decrement operators?

increment operator increments the variable by 1 at a time and decrement operator decrements by 1 in this we have two types increments pre_increment and post increment. In pre_increment the original value is incremented by 1 and assign the new value n=10 i=++n then i =11 In post increment the original value is assigned and after it increments value by 1. n=10 i=n++ then i=10 example: k=5 i=k++ + ++k i=? ans: in first k++ value is 5 second ++k value is 7 i=5+7=12


What is the meaning of plus plus in c?

++a (plus plus a) is pre-incrementing operator to aa=10;printf("%d",++a); /* it will print 11 as ++a increment first a by 1 then prints it */printf("%d",a++); /*it will printf 10 as it is post _ increment operator , it prints the value a first then increment it by 1 */

Related questions

How different is pre and post increment in c plus plus from pre and post increment in C programming?

The pre and post increment (and decrement) operator is the same in C++ as it is in C.


Is x plus equals y is post increment or pre increment?

The '+=' operator behaves like a pre increment operator.


How can you differentiate overloading of pre-fix and post-fix increment operator?

The prefix increment operator is overloaded as operator++() while the postfix increment operator is overloaded as operator++(int).


Program in java to illustrate the pre increment and post increment?

An example might help:int a, b;a= 2;b= a++;/* now a=3, b=2 */a= 2;b= ++a;/* now a=3, b=3 */Read more: What_is_difference_between_pre_increment_and_post_increment


What is the use of using pre increment operator?

int a, b; b = 5; /* post-increment operator */ a = b++; /* a is now 5, and b is now 6. */ /* pre-increment operator */ a = ++b; /* a and b are now both 7 */


What is the declaration of overloaded pre-increment operator implemented as member function?

The pre-increment operator accepts no parameters and returns the same object (by reference) after incrementing. The post-increment operator accepts an unused (dummy) integer parameter and returns a copy of the object (by value) that is made immediately prior to incrementing the object. Note that it is good practice to always use the pre-increment operator even if a post-increment operator exists. The only time you should ever use a post-increment is when you actually store the return value. If you don't store the return value then you will end up making an unnecessary copy, which is highly inefficient. With primitive data types that are less than or equal in length to a pointer this isn't a major issue, but it's good practice nonetheless. If you do it for primitives then you're far more likely to remember to do it for class instances as well. The following example emulates an integer type with pre-increment and post-increment operators implemented: class Simple { public: // Construction: Simple(int data = 0):m_data(data){} Simple(const Simple& simple):m_data(simple.m_data){} public: // Assignment: Simple& operator= (const Simple& simple) { m_data = simple.m_data; return( *this ); } // pre-increment: Simple& operator++ () { // no parameters! ++m_data; // increment this object return( *this ); } // return a reference to this object // post-increment: Simple operator++(int) { // int parameter (not used)! Simple copy( *this ); // call the copy constructor ++m_data; // increment this object return( copy ); } // return the copy (by value) private: int m_data; };


What are applications of pre-increment and post-increment operators why these operators behaviour are made like this is there any reason for such behaviour?

All operators must return a value, and it is this return value (the evaluation of the operation) that differentiates the pre-increment and post-increment operators. If the return value is of no concern, then the pre-increment operator is the preferred version, particularly when working with objects. This is because the post-increment operator's implementation will generally make a copy of the original object which it will return after incrementing the original object. If the return value is of no concern, then making a copy adds an expensive overhead (this doesn't apply to primitive data types since CPU optimisation can pre-empt the return value without requiring a copy to be made). Applications for post-increment and pre-increment are many and varied, but ultimately the goal is to increment the value. Whether you use post-increment or pre-increment is largely down to whether you need to use the return value or not, and whether you need to use the original value or the incremented value. Although the same thing can be achieved with longhand code, it is more efficient to use the appropriate post-increment or pre-increment operators. For instance: int x=1; int y=x; ++x; Can be implemented more efficiently with: int x=1; int y= x++; Whereas: int x=1; ++x; int y=x; Can be implemented more efficiently with: int x=1; int y= ++x; Use caution when pre-incrementing the same value in a compound statement. For example: int x=0; int y= ++x * ++x; The expected result is 2, but the actual result is 4. This is because both expressions (++x) return the same value: a reference to x. And since x is incremented twice, the expression evaluates to 2*2, not 1*2. To get the expected result of 2, each operation must be evaluated separately: int x=0; int y= ++x; y*= ++x;


What is increment and decrement operators?

increment operator increments the variable by 1 at a time and decrement operator decrements by 1 in this we have two types increments pre_increment and post increment. In pre_increment the original value is incremented by 1 and assign the new value n=10 i=++n then i =11 In post increment the original value is assigned and after it increments value by 1. n=10 i=n++ then i=10 example: k=5 i=k++ + ++k i=? ans: in first k++ value is 5 second ++k value is 7 i=5+7=12


Why c plus plus name was given?

In C, the ++ operator means to increment the object associated with it. If you said xyz++; for instance, you would increment xyz. (There are pre-increment and post-increment forms, but that is out of scope for this question.) When the C language was enhanced, the new language was called C++, implying that C++ was the "next", or "incremented", version of C.


How the predecrementer and postdecrementer are taken care of while declaring operator overloading?

When you overload the -- and ++ operators in C++, if you want the pre version, aka --var, simply declare the function without an argument; whereas if you want the post version, aka var--, simply declare the function with an argument of type int. class abc { public: abc& operator++(); // pre-increment form abc& operator++(int); // post-increment form ... }; abc::operator++() { ... pre increment stuff return *this; } abc::operator++(int) { ... post increment stuff return *this; }


What is a 'post fix expression' in java programming?

Postfix expressions are expressions where the operator is at the end of the expression. These include the "++" (increment) and "--" (decrement) operators. Most Java expressions use in-fix notation (e.g. "a + b") but the increment and decrement operators can be postfix ("e.g. "a++" to increment variable a) or even prefix (e.g. "++a").


What is the meaning of plus plus in c?

++a (plus plus a) is pre-incrementing operator to aa=10;printf("%d",++a); /* it will print 11 as ++a increment first a by 1 then prints it */printf("%d",a++); /*it will printf 10 as it is post _ increment operator , it prints the value a first then increment it by 1 */