8
Shoaib
5y

This is the basic program of C Language. What will be the output?

Comments
  • 10
    How about compile it, run it and see it for yourself?
  • 1
  • 3
    Bullshit interview question.
  • 4
    @irene I don't think that's undefined? Terrible code yea, but I'm pretty sure that's defined behavior
  • 1
    Dumb question because you would never write code that looks like this unless you're an absolute fucking idiot.

    Edit: Sorry not sorry if I offended someone.
  • 0
    Looks like from an interview all right, or some such. Anyway… output is:

    10 11 20 10

    Variable i gets initialized to 9, then increased, so it prints 10.

    New scope is declared with a variable i that shadows the i from the outer scope; the inner i is set to 11, so it prints 11.

    Variable i is decremented, making it 10, then added to itself, making it 20, so it prints 20.

    Inner scope is closed, next access to i is the original outer one, so it prints 10.

    He/She/It who/which understands scopes and basic C operators should get this right without even having to type, compile, and run this program.
  • 1
    I am confused. I thought I would learn some new WTF thing about programming but I was wrong.

    I failed ya all guys..

    I failed to understand the point...
    ¯\_(ツ)_/¯
  • 1
    @SomeNone still a bullshit interview question.
  • 2
    I support that line 14 is undefined behaviour because there's no sequence point between two accesses to the same scalar that is subject to a side effect.
  • 1
    @Fast-Nop Maybe, maybe not, I don't have time to read the specs right now… and now that I think about it, it is not quite clear whether the final addition is to the value of i before or after evaluating the right-hand side. If the specs don't have an aswer (or explicitly state that the behaviour is undefined), then it is indeed undefined.
  • 0
    Well, this one is a good c# code. And actually VERY easy to use, even if definition is scary af
  • 1
    @irene That's why, at least in C++, it's not uncommon in company coding standards to forbid any use of templates except the ones the STL already provides.
  • 0
    @irene That is what I thought at first.
    But usage is SOOOO simple. Basically this allows me to simplify Entity framework code soso much !
  • 0
    @irene True. And I try to avoid it as much as possible. But this one, saves around 5000 lines of code (Whole app combined)
  • 0
    Why the fuck did you tag a question about C with Python and C++
  • 1
    Not sure if it's a quiz or your homework 🙄
Add Comment