-1

i didnt find an answer. what happens if I print a variable that has not been assigned a value? for exmple: int a; printf("%d" a);

nigatoni
  • 13
  • 3
  • It's called "undefined behavior". In theory anything can happen, in practice you will *probably* get an arbitrary value. – HolyBlackCat Nov 15 '20 at 17:22
  • @HolyBlackCat Hover your mouse over the "add a comment" link. – Asteroids With Wings Nov 15 '20 at 17:24
  • 4
    @AsteroidsWithWings this question has already been asked many times and there are many duplicates so writing it as a comment while searching for the duplicate is perfectly fine and desired over writing another answer. – t.niese Nov 15 '20 at 17:27
  • 1
    @AsteroidsWithWings I agree with the "comments not being answers" pov. On the other hand, do you feel this question title serves as a useful signpost to the canonical? Or does your answer add any value over the answers on the target? – cigien Nov 15 '20 at 17:38
  • 1
    I'm of the "Prefer to not answer obvious duplicates" persuasion. As soon as answers start popping up, it becomes harder to curate. Upvoted answers impede the Roomba if I remember correctly. – user4581301 Nov 15 '20 at 17:44
  • 3
    @AsteroidsWithWings I agree with t.niese, and I believe that I help the OP by providing a brief summary of the (future) dupe target. If you think that's harmful for the site or OP in some way, can you explain why? I'm not the one to blindly follow rules to the letter if I don't see any practical value in that. – HolyBlackCat Nov 15 '20 at 17:45
  • @cigien No, I answered before the dupe-closure, and have no strong feeling about my answer staying or going. But answering in the comments is now, was always, and will remain, _double bad_. We have a peer-reviewed answer section for a reason. It's mind boggling that so many people don't understand this. SO is the only site on the network where mods don't routinely delete these model-busting violations. I mean, it even literally says, right there in the tooltip, _don't do this_. But these people are proud of violating it. Just.... what. ‍♂️‍♂️ – Asteroids With Wings Nov 15 '20 at 17:50
  • 3
    @AsteroidsWithWings I see your point about comments, though I'm not entirely convinced either way. However, regarding your answer, it does make it harder to get rid of unneeded dupes (which I think this post qualifies as). Especially if the answer is accepted, the post will never roomba without user intervention. If you do agree that this is an unneeded dupe, could you delete your answer? There's absolutely no obligation to do that, of course, but it does help with curation. – cigien Nov 15 '20 at 17:58

1 Answers1

2

Since you did not give the variable a value, its value is what's called indeterminate.

A program that attempts to access/read/print an indeterminate value has undefined behaviour.

That means, according to the standard, anything could happen.

Don't do it.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35