what can(++)and(--) do in c++ source code?

Recommended Answers

All 6 Replies

Member Avatar for ArashVenus

I'm glad that I have the knowledge to help you here ,
++ and -- add +1 or -1 to the variable

for example x++ is equal to x=x+1 and x-- is equal to x=x-1

commented: Yes! +14

what can(++)and(--) do in c++ source code?

++ increase the variable value by 1.

int x=0;
cout<<x<<endl;
x++;
cout<<x;

output:

0
1

lets see more about ++.

x++(postfix)
++x(prefix)

there is an important difference between postfix and prefix.

int x,y=10;
x=++y;

output:

x=11
y=11

int x,y=10;
x=y++;

output:

x=10
y=11

now i hope you can adjust with --
for more information read this tutorial

But beware of unconventional constructs like ++x-- or y = ++x - x--; Such constructs have undefined behavior, the compiler is free to produce any results it wants.

the compiler is free to produce any results it wants.

i check these expression in different environment and they produce different result. i think these expression are compiler dependent.

That's what I said.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.