Output:12Code:#include<stdio.h> void main() { static int a[][2][3] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; int i = -1; int d; d = a[i++][++i][++i]; printf("%d", d); }
I am confused regarding in which order d is evaluated..Help?
Output:12Code:#include<stdio.h> void main() { static int a[][2][3] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; int i = -1; int d; d = a[i++][++i][++i]; printf("%d", d); }
I am confused regarding in which order d is evaluated..Help?
Last edited by Salem; 10-31-2011 at 11:34 PM. Reason: remove pointless tags
As is everybody. That code exhibits undefined behavior, so there is no way to know. The following explanations of similar code should clarify this:
Question 3.1
Question 3.9
Question 3.10a
The links you have provided discuss cases where i is used both as array index and in increment expression while in this question that is not the case. It gives same output on GCC and Borland C. Any other explanation ?
Extrapolate the reasoning from the examples and you will see that the code in this question has the same problem.Originally Posted by ranjit89
Give me enough time and I can write a standard conforming C compiler that generates the program to give the same output, except on public holidays.Originally Posted by ranjit89
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Looks like you guys are right. This program gave an output of 10 on a different compiler. So behavior is unexpected...Thanks again
Nope, no other explanation. I guess I should have linked you to Question 3.8 as well. That has the better discussion of it, and covers exactly what in the standard guarantees that you are stepping into undefined behavior.
I had enough time.
Programming and Web Development Help | DreamInCode.net
Same UB code, lots of different compilers, and LOTS of different answers!
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Oh, and I should mention a few other things:
void main is only supported in unhosted environments (embedded systems and the like). Hosted environments (i.e. those that run on a normal OS) require main to return an int per the C standard. Thus your program should be:
Also, multi-dimensional array initializers should be put int the proper amount of curly braces, and nested appropriately. You have a Nx2x3 array, so the compiler will figure out N based on your initializer. But adding the correct braces makes it easier to spot errors:Code:int main(void) { // whatever code you want, as long as it's not undefined behavior :P return 0; }
I got warnings for both of those as well as the undefined behavior by enabling warnings on my compiler. Here's what happened when I compiled your initial code (note the strict gcc options):Code:static int a[][2][3] = { { {0, 1, 2}, {3, 4, 5} }, { {6, 7, 8}, {9, 10, 11} }, 12 // who knows what this guy belongs to };
I would also recommend not using Borland anymore, simply because it's so outdated and no longer maintained. GCC, Pelles C, Code::Blocks/MinGW and even Microsoft Visual C++ Express are all free and currently maintained.Code:$ gcc -Wall -Wextra -pedantic --std=c99 foo.c foo.c:2:6: warning: return type of ‘main’ is not ‘int’ [-Wmain] foo.c: In function ‘main’: foo.c:4:5: warning: missing braces around initializer [-Wmissing-braces] foo.c:4:5: warning: (near initialization for ‘a[0]’) [-Wmissing-braces] foo.c:7:16: warning: operation on ‘i’ may be undefined [-Wsequence-point] foo.c:7:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
Nice to have the extra info..