Why, oh WHY isn't this allowed?

This is a discussion on Why, oh WHY isn't this allowed? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void){ int x; x = 5; printf("x is %d\n", ++x++); //or... printf("x is %d\n", (++x)++); //or ...

  1. #1
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55

    Thumbs down Why, oh WHY isn't this allowed?

    Code:
    #include <stdio.h>
    
    int main(void){
    
         int x;
     
         x = 5;
    
         printf("x is %d\n", ++x++); //or...
         printf("x is %d\n", (++x)++); //or even
         printf("x is %d\n", ++(x++)); // !!!
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why would it be useful if it was?

    The reason the last one is not allowed is that the return value of postfix ++ is not an lvalue. You can't do that for the same reason that you can't do ++1.
    The second one is just a sequence point issue. Two updates to the same variable without a SP in-between is undefined behaviour.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55
    The reason the last one is not allowed is that the return value of postfix ++ is not an lvalue. You can't do that for the same reason that you can't do ++1.
    The second one is just a sequence point issue. Two updates to the same variable without a SP in-between is undefined behaviour.
    What about the first one???

    WHO on earth is sequence point?? I haven't met this fella before

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. constructors not allowed a return type
    By kotoko in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2009, 11:51 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Java: Why isn't this allowed?
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-14-2005, 04:28 PM
  5. Constructors now allowed a return type: Mystery Error
    By wbeasl in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 02:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21