answersLogoWhite

0


Best Answer
Short Answer

The short answer is that both the prefix increment and postfix increment operators will increment the operand. The difference is not so much the operation but the evaluation of the operation. Prefix increment evaluates as a reference to the operand itself, after it is incremented. Postfix increment evaluates to a reference to the operand (or its value) before it was incremented. The same principal applies to prefix decrement and postfix decrement operators.

Long Answer

The first thing to bear in mind is that operators not only perform an operation upon one or more operands, the expression must also be evaluated. In other words, the expression must return a value to the caller, even if we choose to ignore that value (by not storing it).

Consider the following increment expressions which both ignore the evaluation:

int x = 1, int y = 1;

++x; // prefix increment.

y++; // postfix increment.

Both x and y are incremented, as you would rightly expect, so they are both 2 when the expressions are evaluated. But by ignoring the result of those evaluations it's impossible to actually see any difference between these two expressions. So let's re-evaluate these expressions:

int x = 1, int y = 1;

int eval_x = ++x;

int eval_y = y++;

Now the evaluations cannot be ignored because we've immediately assigned them to the variables eval_x and eval_y. Both x and y are still incremented, just as before, but while eval_x is assigned the value 2 (the new value of x), eval_y is assigned the value 1 (the old value of y).

Clearly the two expressions perform exactly the same operation upon x and y (incrementing them), but they evaluate quite differently.

The prefix increment operator is the easiest of the two to understand. The evaluation of any prefix increment upon any operand is always a reference to the operand itself. Since the operand is incremented by the operator, assigning the evaluation of the operator to another variable (by reference) effectively assigns the incremented value to that variable.

This is really no different to saying eval_x = ( x = x + 1 ). It looks complicated but its really simple if we evaluate it as the computer evaluates it, from right to left. If x is 1 then x + 1 evaluates to 1 + 1 which evaluates to 2. Thus the equation reduces to eval_x = ( x = 2 ). x = 2 is an assignment not only gives x the value 2, it evaluates as a reference to x. Thus the final expression is eval_x = x, thus eval_x is assigned the value of x, which is 2. We can safely ignore the fact eval_x = x evaluates as a reference to eval_x, as the expression is now fully evaluated. The upshot is that eval_x 2.

Still with me?

The postfix increment operator is more complex. It cannot be evaluated in the same way because the evaluation of y++ requires that y be incremented (y = y + 1), but the evaluation of y++ cannot be a reference to y because we are not remotely interested in the incremented value of y, we're interested in what it was before being incremented. To achieve this we need to imagine there's a temporary variable involved:

int y=1;

int temp = y;

y = y + 1;

int eval_y = temp;

Although this is a fair representation, it is not an accurate one. It would be too easy to assume that postfix increment incurs a penalty by virtue of the temporary variable. But while it is certainly true that postfix operators on an object (an instance of a class) will incur a performance penalty, the same is not true of primitive data types such as int. This is simply because primitive data types do not require temporary variables to perform a postfix operation.

To understand why, remember that the CPU has several registers available for processing and for storing return values (which could be an address or an actual value). The compiler can generate code that will store the current value of y in a return register and operate upon a reference to y in another register. The evaluation is already pre-empted because it already exists in the return register, so no temporary is actually required, and therefore no penalty incurred.

In other words, y++ is no slower or faster than ++x when x and y are both primitives.

Classes are a different matter altogether. We cannot use CPU trickery to optimise a postfix operator upon an object (we'd need a hugely dynamic register model which is impossible to implement in hardware -- but I live in hope :-), so we are forced to make a copy. And since classes are generally larger than primitive data types, that can be an expensive hobby if we're in the habit of using postfix operators upon primitives. If the return value (the evaluation) is of no consequence, it's far too easy to use postfix operators in for() loops and such like. But it's a hard habit to break and you end up using postfix for just about everything, whether you want the previous value or not, resulting in objects being copied but never actually used. If you can get into the habit of using prefix operators at all times, even for primitives, and use postfix only when you actually need the previous value, your code will be that little bit more spritely. If you can't break the habit, or choose not to, put simple traces in your copy constructors. I guarantee you'll be prefixing by default in no time.

To finish off, here's a simple class that implements prefix and postfix increment operators. Note how both implementations actually employ prefix operators but, more importantly, that the postfix operator must make a temporary copy of the current instance (with a tell-tale tracer in the copy constructor). Finally, note that both operators return references, not values. Despite the advice given elsewhere (you know who you are!), never return an object by value unless you absolutely must. Many sites will advise that postfix operators should "always" return by value. But if you do this, you will not only pay a penalty for making a temporary copy (which is unavoidable in non-primitives), you pay the penalty twice over by copying the temporary copy!

#include

class cNum

{

public:

cNum(): m_data( 0 ){}

cNum( int data ): m_data( data ){}

cNum( const cNum & num ): m_data( num.m_data ){ printf( "Copying!\n" ); }

public:

cNum & operator++ (){ ++m_data; return( *this ); } // prefix

cNum & operator++ ( int ){ cNum * tmp = new cNum( *this ); ++m_data; return( *tmp ); } // postfix

cNum & operator= ( const cNum & num ){ m_data = num.m_data; return( *this ); }

public:

int GetData() const { return( m_data ); }

private:

int m_data;

};

int main()

{

cNum num( 25 ), eval;

printf( "Initial values:\n" );

printf( "num: %d, eval: %d\n\n", num.GetData(), eval.GetData() );

printf( "Calling prefix increment:\n" );

eval = ++num;

printf( "num: %d, eval: %d\n\n", num.GetData(), eval.GetData() );

printf( "Calling postfix increment:\n" );

eval = num++;

printf( "num: %d, eval: %d\n\n", num.GetData(), eval.GetData() );

printf( "Calling prefix increment (ignoring evaluation):\n" );

++num;

printf( "num: %d, eval: %d\n\n", num.GetData(), eval.GetData() );

printf( "Calling postfix increment (ignoring evaluation):\n" );

num++;

printf( "num: %d, eval: %d\n\n", num.GetData(), eval.GetData() );

return( 0 );

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is pre increment and post increment in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
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.


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 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 */


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


For loop in c language?

for(assigning initial value;condition;increment/decrement) { statement; }


What do the two plus stand for in C plus plus?

The ++ in C++ refers to the postfix increment operator (operator++()). It's literal meaning is "the successor to C", in reference to the C language upon which the C++ language is based.


Can you increment the value of a variable by 2 using any increment operator?

There is no such increment operator in C language to increment the value of a variable by 2.An increment operator only increments the value by 1. however you can apply the increment operator twice to get an increment of 3. No: you cannot: ++(++a) won't compile. Yes. Example: a += 2; but += is not an increment operator, it's a shorthand of a=a+2; just like a++ is a shorthand for a= a+1


Why is programming language named C and not simply C?

I am guessing you typed the question wrong, the way I understand your question is "Why is the programming language named C++ and not C ? " The answer to this is that there is a programming language called C, and in that programming language the ++ means increment by one. So C++ is the language C improved, as such it can read and compile all C programs in addition to having other features that C does not have.


Why are increment and decrement used in c?

To increment or decrement a value


What do you mean by c plus plus?

C++ is a programming language, but, in the same time, it's a valid expression. Example:A= C++is equivalent with:A= C, C= C+1It's a bit of a joke. In the programming language C, ++ is the increment operator, so C++ can be interpreted as "C, except one better."


How do you increment strings in c?

Not possible. If you think otherwise, then please increment manually this string: "/"