Thread: increment question

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    increment question

    Code:
    #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);
    }
    Output:12
    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

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    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 ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ranjit89
    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.
    Extrapolate the reasoning from the examples and you will see that the code in this question has the same problem.

    Quote Originally Posted by ranjit89
    It gives same output on GCC and Borland C.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Looks like you guys are right. This program gave an output of 10 on a different compiler. So behavior is unexpected...Thanks again

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ranjit89 View Post
    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 ?
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    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.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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:
    Code:
    int main(void)
    {
        // whatever code you want, as long as it's not undefined behavior :P
        return 0;
    }
    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:
    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 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:
    $ 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]
    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.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Nice to have the extra info..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment and Decrement
    By Johnathan1707 in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 02:20 PM
  2. Increment simple question
    By SurferIX in forum C Programming
    Replies: 6
    Last Post: 06-01-2008, 02:49 PM
  3. Question on increment operator
    By noodles in forum C Programming
    Replies: 1
    Last Post: 09-05-2006, 11:40 PM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:23 AM