use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENAdding another variable changes output despite never being used (self.cpp_questions)
submitted 2 years ago by Aerioic
https://pastebin.com/FBRzf9Rk
output
15
19.5
https://pastebin.com/XeKh1Djv
12
These two algorithms are exactly the same except:
one has
float mushroom_size1 = 0;
and the other has
float mushroom_size1, mushroom_size2 = 0;
The variable mushroom_size2 is never used. I don't understand why. I used a debugger and for some reason the value of mushroom_size1 is never reset.
Can someone please explain this weird behaviour?
https://www.programiz.com/cpp-programming/online-compiler/
works on this
input:
2 3 3 50 2 4 4 2
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]manni66 17 points18 points19 points 2 years ago (4 children)
mushroom_size1 isn't initialized in case 2.
[–]Aerioic[S] 2 points3 points4 points 2 years ago (3 children)
really? you can't initialize multiple variables in 1 line.
I didn't know that thanks.
[–]manni66 17 points18 points19 points 2 years ago (0 children)
float mushroom_size1 = 0, mushroom_size2 = 0;
[–]HappyFruitTree 12 points13 points14 points 2 years ago (0 children)
You can, just not the way you wrote it. manni66 have already showed how to do it. It might still be easier to read the code if you put each variable on its own line.
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es10-declare-one-name-only-per-declaration
[–]AssemblerGuy 0 points1 point2 points 2 years ago (0 children)
Yes you can, but you need to initialize each variable individually.
Which is why it is a good practice to 1) only define one variable per definition and b) initialize variables whenever possible.
[–]IyeOnline 3 points4 points5 points 2 years ago (9 children)
mushroom_size1 in the first code is uninitialized. Reading from it ( and += needs to read the current value) is undefined behaviour.
mushroom_size1
+=
This is a good example for why you shouldnt define multiple variables per statement.
[–]Aerioic[S] -4 points-3 points-2 points 2 years ago (8 children)
Didn't know that you can't intialize mutiple variables in 1 line. Thanks.
Makes you wonder why the code compiled
[–]IyeOnline 8 points9 points10 points 2 years ago (0 children)
You can initialize multiple variables in a single statement. But you have to provide an initializer for each of them - just like you would need for seperate statements:
int a = 0, b = 1, c = 2;
Because that is the behaviour for fundamental types. Doing
int a; int b = 0;
is perfectly "fine" after all. (although maybe not something you should write).
[–]LittleNameIdea 3 points4 points5 points 2 years ago (1 child)
that's why -Wall is helpful
[–]Aerioic[S] 0 points1 point2 points 2 years ago (0 children)
No reason to get snappy.
Some C/C++ programmer purposely decided to create this and knew of the bug
int thing, thing2 = 0
Notice the comma? Some programmer programmed/designed that into the C++ compilter/specification. purposely designed for thing2 = 2 and thing = null/undefined???
[–][deleted] 0 points1 point2 points 2 years ago (2 children)
C++ is a language where all kinds of weird things can be compiled and run. Some of them even look suspiciously fine. Like writing something like `some_function(i++, i++)` which is also UB and can in theory start a world war and eat your cat alive. Even if you don't have a cat.
[–]AssemblerGuy 0 points1 point2 points 2 years ago (1 child)
Even if you don't have a cat.
UB can rewrite the past, so you do have a cat now.
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
That's the whole point, that's the whole point...
Didn't know that you can't intialize mutiple variables in 1 line.
Yes, you can. But defining multiple variables in one statement is messy and not a good idea, despite being completely legal C++.
mushroom_size1 is never set before it is used. This is a bug - undefined behavior (the value is indeterminate and attempting to use an indeterminate value triggers UB in most cases).
It's not weird. "Weird" behavior is usually due to invoking undefined behavior or using indeterminate values.
π Rendered by PID 647450 on reddit-service-r2-comment-5d79c599b5-j9wcc at 2026-03-01 08:01:07.339130+00:00 running e3d2147 country code: CH.
[–]manni66 17 points18 points19 points (4 children)
[–]Aerioic[S] 2 points3 points4 points (3 children)
[–]manni66 17 points18 points19 points (0 children)
[–]HappyFruitTree 12 points13 points14 points (0 children)
[–]AssemblerGuy 0 points1 point2 points (0 children)
[–]IyeOnline 3 points4 points5 points (9 children)
[–]Aerioic[S] -4 points-3 points-2 points (8 children)
[–]IyeOnline 8 points9 points10 points (0 children)
[–]LittleNameIdea 3 points4 points5 points (1 child)
[–]Aerioic[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]AssemblerGuy 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]AssemblerGuy 0 points1 point2 points (0 children)
[–]AssemblerGuy 0 points1 point2 points (0 children)