X = x++

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Syntax Error

Coder
Silenced
Meme Tier VIP
Nov 6, 2014
239
2,928
6
C++:
#include <iostream>

int main(void)
{
	int x = 0;
	
	while ( x < 3 ) {
		x = x++;
		std::cout << x << std::endl;
	}
	
	return 0;
}
Question: What will be this programs output? Post your thoughts and then compile it and see what happens. :]

Credits: stackoverflow
 

Syntax Error

Coder
Silenced
Meme Tier VIP
Nov 6, 2014
239
2,928
6
OUTPUT:
X
X
X
X


Is that right or too simple lol
Good guess. Although it wouldn't print x's because in order for that to happen, x would need to have been inside double quotes...

C++:
std::cout << "x"<< std::endl;
:)
 
Last edited:

squeenie

Hacker
Meme Tier VIP
Dank Tier Donator
Mar 6, 2013
677
5,478
37
Straight off the top of my head it will output
1
2
3
 

keto

Dank Tier Donator
Nobleman
May 25, 2013
151
1,648
1
1,2,3 is my guess
 
Last edited:

till0sch

Respected Hacker
Dank Tier VIP
Dank Tier Donator
Oct 14, 2012
1,104
12,593
51
I guess this is because x++ and ++x exist
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
Looks like I'm the only one who guessed incorrectly! :D
 

Syntax Error

Coder
Silenced
Meme Tier VIP
Nov 6, 2014
239
2,928
6
I guess it depends on what compiler you use which was also mentioned on stack overflow but it's supposed to result in an infinite loop printing 0 nonstop.
Here's the break down of why it happens. So as we no x++ is the post increment operator as opposed to the pre increment operator. Meaning x is incremented but it's old value which is 0 in our case is stored back into x resulting in x staying as 0...

Originally when I found this post it was talking about good c++ interview questions but this morning when i wa trying to find it I was unable to... Here's a different post which is more or less the same thing.

https://stackoverflow.com/questions/3831341/why-does-this-go-into-an-infinite-loop
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods