answersLogoWhite

0


Best Answer

C++ uses the postfix increment operator whereas ++C uses the prefix increment operator. Both do exactly the same thing; they increment C (the same as C=C+1 increments C). The difference is only in the return value. ++C returns a reference to C, whereas C++ returns the original value of C.

User Avatar

Wiki User

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

Wiki User

11y ago

++ is the increment operator. When we say c++ we are really saying c = c + 1. However, there are two variants of the increment operator; prefix and postfix:

++c is an example of the prefix increment operator (the ++ operator comes before the operand c).

c++ is an example of the postfix increment operator (the ++ operator comes after the operand c).

Both will increment c (just as c = c + 1 does), but they differ in the way they are evaluated.

Consider the following:

int c = 1;

int c = c + 1;

c + 1 is an expression that must be evaluated before it can be assigned to c. The evaluation is c + 1 = 1 + 1 = 2. Thus the value 2 is assigned to c. Note that c = 2 is an expression which also evaluates to 2 (the value of c).

The prefix increment operator is evaluated in a similar way:

int c = 1;

int x = ++c;

Here, the evaluation of ++c is assigned to the variable x. ++c evaluates to 2, thus x = 2 (which also evaluates to 2).

int c = 1;

int x = c++;

The postfix increment operator is evaluated differently. Here, x = 1, which is the value of c BEFORE it was incremented. This is essentially the same as if we'd said the following instead:

int c = 1;

int x = c;

c = c + 1;

It's important to note that the evaluation of c++ does not occur before c is incremented (that's impossible). c is incremented first, THEN the evaluation is assigned to x. In other words you cannot evaluate an operation before the operation is carried out. It may seem to be the other way around, but it is not.

It is important to know this because when dealing with objects that implement the postfix increment operator, the implementation must first copy the operand (the object being incremented), then increment the operand, and finally return the copy of the operand. The copy is not incremented thus it represents the original value of the operand, thus obeying the rules of the postfix increment evaluation.

Primitive types such as int do not need to make copies of themselves when postfix incremented. The original value is placed in a CPU return register while the variable is incremented in another register. In effect, the evaluation has actually occurred before the operation. But it is delayed by the operation, so the evaluation still occurs after the operation. However, since no temporary variable was created, there is no loss of performance when postfix incrementing primitive types. Thus if the evaluation is not essential to the operation, then c++ is just as quick as ++c. However the same is not true when dealing with objects, because a temporary copy is required. Therefore ++o would be quicker than o++, where o is an object that implements both of the ++ operators.

Although there's no performance loss when dealing with primitives, if you use the prefix increment on primitives by default, then you'll get into the habit of doing it with objects as well. The only time you should use postfix increment is when the original value is meaningful to the evaluation of a complex expression, such as x = c++. If you only want to increment the operand and don't care for the evaluation, then use ++c instead.

A typical example where the evaluation can be ignored is when using for() loops, such as:

for( int i=0; i<10; i++ ){...}

There's nothing wrong with this (i is a primitive so there's no performance loss), but if you use postfix increment in loops like this, you're far more likely to use them with objects, which can only be detrimental to performance. So get into the habit of using the following instead:

for( int i=0; i<10; ++i ){...}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you write c plus plus not plus plus c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you write own constructor in c plus plus?

Yes.


Can you write own consructor in C plus plus?

Yes.


What you can do in C that connot be done by C plus plus?

Nothing. In C++ you could write a C compiler. So, everything that can be done with C, can be also done in C++.


How do you write a macro to find the biggest of 3 numbers in c plus plus?

#define biggest (a) &gt; (b) &amp;&amp; (a) &gt; (c) ? (a) : (b) &gt; (c) ? (b) : (c)


Write a C plus plus function that print a triangle of stars?

Write a function that print a triangle of stars.


Is g plus plus similar to c plus plus?

G++ is the Gnu compiler's extension for C++. It is not a different language. It simply allows you to use the GCC compiler to write C++ code.


Write a program in c plus plus to implement macro processor?

Don't write, it is already written, google for 'cpp'.


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


Do I need to write a program to find a substring in a given string in c plus plus?

No.


How do you write program to convert meter to kilometer in c plus plus?

Divide it by 1000.


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


How can write the name in c plus plus language without using cout statement?

I believe, you can use C-function - printf().