urgent

This is a discussion on urgent within the C Programming forums, part of the General Programming Boards category; Can anyone help me on this one plz I canít understand how to get the result #Define product(x) (x*x) main() ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    30

    urgent

    Can anyone help me on this one plz
    I canít understand how to get the result


    #Define product(x) (x*x)
    main()
    {
    int i=3, j, k;
    j = product (i++);
    k= product (++i);
    printf (ď%d %dĒ, j, k);
    }



    #Define prod(x) (x*x)
    main()
    {
    int i=3, j, k;
    j = prod (++i);
    k= prod (i++);
    printf (ď%d %dĒ, j, k);
    }


    results: for product- j=9, k=49

    for prod, j=k=25


    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Well, i don't think that it is undefined although i am not so sure. The results you got are those which you should get and in every compiler these will be the same.

    The first one is
    j =i++ * i++
    That means j gets the value of i*i and then add 2 to i ( not to k ).

    Then you do
    ( i == 5 )
    k = ++i * ++i

    so you add 2 to i ( now i is 7 ) and multiple i*i ( 7*7 = 49 ).

    The secondexample:
    j = ++i * ++i
    which means add 2 to i and then multiple it by its self ( i = 3 +2, k =i*i = 5*5 =25

    then you do
    k = i++ * i++
    which means multiple i*i ( i == 5 ) and assign this value to k ( k==25 ) and then add 2 to i.
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    As i told you, these results will always behave the same for whatever value i ( i =our variable )has, and the seperate results of each value of i will be the same for every compiler.
    Since undefined means that absolutely anything might happen, this behaviour is not undefined.
    Loading.....
    ( Trying to be a good C Programmer )

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    30
    Money? n Salem, thanks a lot. Salem, sry for using "urgent". it was urgent for me...

    if anyone else got any suggestion on this topic, plz don't hesitate to post your reply. thanks

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>>
    Since undefined means that absolutely anything might happen, this behaviour is not undefined.
    <<<

    If the result of a statement is "undefined" by the standard, then it is undefined full stop. How various compilers react when presented with such a statement may also vary. There is no way you can say your observed case is the general case for all compilers, since I guarantee you have not used all of them. There is also no obligation for the compiler writers to continue this behaviour in future versions.

    The bottom line is that if you build software that relies on the results of "undefined" operations, sooner or later you will hit a, probably very hard to find problem.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >An accidentally correct result does not prove that its defined
    Ok, but follow for a little my thinking and give to i the values 1, 2, 3, 4, 5, 6,7, .....100000 and see that the results will be as i described them.

    About your example, Salem, all happened as i expected. The first part gives 15 and the second one 16.
    About the second one ( which is the oddest ) you have an ++i. That will increase the value of i by one, you'll do your multiplication with 4*4 and then the i++ will increase the i by one.

    I agree with you that it is very odd but this is how it works. So, because i know in advance what the output will be it is defined.

    >If the result of a statement is "undefined" by the standard, then it is undefined full stop.
    .....
    Loading.....
    ( Trying to be a good C Programmer )

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>So, because i know in advance what the output will be it is defined.
    You *really* need to open them ears a little wider, eh?! For the last time, undefined means the results given will not always be as expected. Just because it did what you want to this time, doesn't mean to say that it will when compiled with another compiler or with different optimization settings. But of course, you already know this, because you've been told twice Pay attention please And please don't attempt to promote bad coding practices on here, the newbies will get all confused.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Hammer,
    I have nere used that by my own

    Salem,
    >Well my example has sure got you puzzled
    Not at all! .I really expected that.

    But, ok, i give up - but i still claim that since i know for every value of i the result it's not undefinied
    Loading.....
    ( Trying to be a good C Programmer )

  9. #9
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >>but i still claim that since i know for every value of i the result it's not undefinied
    What is every value? Can you prove it? On every possible combination of machine, operating system, and compiler that supports C? It's easier to read the standard and see what's undefined than try to prove otherwise. What were you planning on doing? Use a huge switch statement and do something different for each value of i? Sounds like a lot of effort for no gain to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 09:38 PM
  3. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM

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