-5

Explain the output . Program has been compiled on gnu (gcc 4.9.2 compiler)

#include <stdio.h>

main()
{
    int i=1;
    int arr[]={2,3,4,5};
    int x=(i++<3);

    printf("%d  %d  %d  %d  %d",i,x,++i[arr],i++[arr],i);
}

output :3 1 6 4 3

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • You have mistakes. That code WON'T compile (even on gcc :-) – Amit May 29 '15 at 05:56
  • What about this do you not understand? How does it differ from what you expect. Be specific. – Iskar Jarak May 29 '15 at 05:56
  • 1
    It is undefined behavior. The output could be anything. – Spikatrix May 29 '15 at 05:56
  • 2
    @Amit , Actually, it does compile. But with warnings... – Spikatrix May 29 '15 at 05:58
  • 1
    @CoolGuy `i[arr]` would compile? didn't know that... what would it translate into? – Amit May 29 '15 at 05:59
  • 7
    @Amit See this question: ["With C arrays, why is it the case that `a[5]` == `5[a]` ?"](https://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a). – WhozCraig May 29 '15 at 06:00
  • @WhozCraig, CoolGuy - good to know :-) – Amit May 29 '15 at 06:02
  • 2
    @AyushNigam there is no explanation for the output. Your parameters instigate unsequenced evaluation and modification to `i`. In fact, clang even warns you about it: "Unsequenced modification and access to 'i'". If gcc doesn't consider turning up your warning levels and options. – WhozCraig May 29 '15 at 06:04
  • int x=(i++<3); is a boolean Operation. so x gets the value 1. ++i[arr] and i++[arr] is an address, but not the address of the Array, this behaviour is undefined and can let the Programm Crash. I think it should be arr[i++] or arr[++i] –  May 29 '15 at 07:37
  • @Miguel13366 The multiple access to `i` in the same expression is what's undefined behavior. The [obscure ordering](http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a) of `i` versus `arr` has nothing to do with it. – Lundin May 29 '15 at 07:55
  • @Lundin: Sure. But I think the address of 'i' is also undefined. –  May 29 '15 at 08:35

1 Answers1

0

What you have here is a "side effect". Depending on the underlying architecture, the parameters to printf() are evaluated from left-to-right or from right-to-left (f.e. HPUX).

So you cannot say, what is "THE" result. You can only specify the result on your operating system with your compiling chain.

This behaviour is called Undefined behavior and sequence points

Community
  • 1
  • 1
Peter Miehle
  • 5,984
  • 2
  • 38
  • 55
  • First of all,in c arr[i] is equivalent to i[arr] NOW coming to the main point the above question does have a solution and is not machine dependent because it has been asked in many reputed company interviews like Adobe etc.So I would be really glad to know the correct answer – Ayush Nigam May 30 '15 at 03:59
  • NOPE: `function(f1(),f2());` depends on the calling strategy (we talk about "C" not about "C++"!), if f1 and f2 have side-effects, the result is different on different architectures. – Peter Miehle Jun 01 '15 at 07:11
  • the correct answer is: "under C, this is machine dependend behaviour". – Peter Miehle Jun 01 '15 at 07:45