#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    1

    Why the o/p is like this?


    #include<stdio.h>
    int main()
    {
    static int arr[]={97,98,99,100,101,102,103,104};
    printf("\nbase address:%u",arr);
    int *ptr=arr+1;
    print(++ptr,ptr--,ptr,ptr++,++ptr);
    printf("\n");
    return 0;
    }

    print(int *a,int *b,int *c,int *d,int *e)
    {
    printf("%d %d %d %d %d",*a,*b,*c,*d,*e);
    }

    o/p:100 100 100 99 100

    And this works without declaring function print.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Read Sequence point.

    Also read my page, Evaluating (x + x * ++x).

    Bottom line: You are doing something very, very stupid, depending on undefined behavior.

    Originally Posted by aswinas
    And this works without declaring function print.
    No, it does not work! Read the warnings!

    {Added by Edit:
    After having removed the C99 dependencies that won't let it compile otherwise (ie, changed the order of declarations and executable statements to normal):
    C:TEST>gcc -Wall aswinas1.c
    aswinas1.c: In function `main':
    aswinas1.c:7: warning: unsigned int format, pointer arg (arg 2)
    aswinas1.c:8: warning: implicit declaration of function `print'
    aswinas1.c: At top level:
    aswinas1.c:14: warning: return-type defaults to `int'
    aswinas1.c: In function `print':
    aswinas1.c:16: warning: control reaches end of non-void function

    C:TEST>a
    Then after modifying the printing of the base address so that it will print the address in proper format (pointer values use %p, not %u) and then do a newline; otherwise the output becomes an unreadable mess):
    C:TEST>a

    base address:00402008
    100 100 100 99 99

    C:TEST>
    You claim that the ouput is this:
    100 100 100 99 100

    We get different results. I used MinGW gcc under WinXP. What compiler and OS did you use? Have you followed salem's link yet to see why that would be important?
    }

    Ignoring warnings is another very, very stupid thing.

    And not using code tags after having posted 26 messages here is purely asinine.
    Last edited by dwise1_aol; September 19th, 2013 at 01:39 PM.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Are you at the Bangalore Institute of Technology? Their name comes up when I Google on the arguments for that print call, along with several posts on other forums asking about this homework assignment. The prevailing opinion on this forum is that it is a stupid assignment which does not teach anything useful except what happens when you do something really stupid.

    You asked:
    Originally Posted by aswinas
    Why the o/p is like this?

    . . .

    o/p:100 100 100 99 100
    Why do you ask that question? Except for just simply repeating what you were asked, thus implicitly requesting us to do your homework for you.

    Did you work through by hand the output that you had expected? If not, then why not? Because unless the output you actually got was different from what you had expected, then why question that output? Excluding the reason being that that is what your homework assignment told you to do.

    So then, answer these questions:

    1. What is the output that you would expect from that program?

    2. Why did you expect that output? In other words, what are the assumptions that you made. I think that the best way to answer this question would be for you to give a step-by-step account of passing those arguments to that function and what the value of ptr is at every step (expressed as which element of the array it's pointing ).

    3. When you generate an assembly listing of your program, what does it tell you that the program is really doing? If you bothered to follow the link to my page on a similar problem, you would have seen that referring to the assembly code that the compiler generates is absolutely essential to answering your original question.

    If you are really serious about your question, then answer my questions.

    And use code tags! And never ignore warnings!

    PS
    Also, since you are merely re-posting an assignment that has been handed out many times before and has caused many of those other students to have run to the various fora looking for someone to do their homework for them, why haven't you thought of doing a search in devshed?

    There is no excuse for not making at least a minimal effort to research your problem.

    PPS

    A fourth question: where did you get this question from? The exact same question has shown up before, so where is it from?
    Last edited by dwise1_aol; September 19th, 2013 at 12:28 PM.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    Another chaos example
    Point your 'tutor' at that, then get them to explain how you get different results out of different compilers using the same code.

    If they try to "blame" the compilers (and not realise that the code has UB), then you seriously need another tutor.
    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. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    1
    Dude, There wasn't any warning or errors.
    And I used fedora 19 64bit and gcc compiler.
    I got the result as I told you.
    And this was a problem I found in Pointers in C by Yashwant Kanetkar.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    > Dude, There wasn't any warning or errors.
    Well try using some warning enabling options then.

    > I got the result as I told you.
    Nobody cares what results you got.
    The point is, there's NO WAY that everyone will get the same results as you.

    Here, think about this for a while.
    Someone asks you to write some code (for money), but you don't have access to their compiler.
    You write the code and it 'works' on your compiler.
    You send the code to your customer, but because you relied on UB, it doesn't work for them.
    Now you're out of pocket because they refuse to pay you for providing rubbish work.

    > And this was a problem I found in Pointers in C by Yashwant Kanetkar.
    A case of the stupid leading the blind.
    Get a better book.

    Code:
    $ cat foo.c
    #include<stdio.h>
    int main()
    {
    static int arr[]={97,98,99,100,101,102,103,104};
    printf("\nbase address:%u",arr);
    int *ptr=arr+1;
    print(++ptr,ptr--,ptr,ptr++,++ptr);
    printf("\n");
    return 0;
    }
    
    print(int *a,int *b,int *c,int *d,int *e)
    {
    printf("%d %d %d %d %d",*a,*b,*c,*d,*e);
    }
    $ gcc -W -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:5:1: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat]
    foo.c:7:1: warning: implicit declaration of function ‘print’ [-Wimplicit-function-declaration]
    foo.c:7:29: warning: operation on ‘ptr’ may be undefined [-Wsequence-point]
    foo.c:7:29: warning: operation on ‘ptr’ may be undefined [-Wsequence-point]
    foo.c:7:29: warning: operation on ‘ptr’ may be undefined [-Wsequence-point]
    foo.c:7:29: warning: operation on ‘ptr’ may be undefined [-Wsequence-point]
    foo.c: At top level:
    foo.c:12:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    foo.c: In function ‘print’:
    foo.c:15:1: warning: control reaches end of non-void function [-Wreturn-type]
    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
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by aswinas
    Dude, There wasn't any warning or errors.
    And I used fedora 19 64bit and gcc compiler.
    I got the result as I told you.
    Did you use the -Wall option with gcc? Please refer to my message and you will see that that is what I did.

    In our culture, we talk about the ostrich and how it "hides its head in the sand". I understand that the reality is that the bird will place its head atop the ground, not buried under it, but the popular image still remains of the the ostrich burying its head in the ground so that it can avoid seeing the dangers around it and thus stand there in a false sense of security. In his classic work, The Hitchhiker's Guide to the Galaxy, Douglas Adams invented the Joo Janta 200 Super-Chromatic Peril Sensitive Sunglasses (follow that link):
    Originally Posted by The Hitchhiker's Guide to the Galaxy
    Designed to help the wearer develop a relaxed attitude to danger. The lenses turn completely black at the first hint of trouble, thus preventing the wearer from seeing anything that might alarm him/her.
    Of course, those peril-sensitive sunglasses also prevent the wearer from ever realizing that there is a situation that he needs to handle and in fact leaves him totally defenseless in the face of danger.

    In that vein, if you never tell your compiler to display warnings, then you will have a false sense of security. However, if you tell your compiler to display all warnings (which is what -Wall does with gcc), then you can be aware of what's actually happening and what you need to attend to.

    So then it's all up to you. Which do you want to be? Fat, dumb, and happy? Or alert and aware? Alert and aware has a far greater chance of survival. Though as Deming noted, "Survival is not a requirement." So if you wish to get culled out as completely unfit, do please step right in the line for extinction.

    Originally Posted by aswinas
    And this was a problem I found in Pointers in C by Yashwant Kanetkar.
    I am admittedly at a loss here. I do know that there is an Indian author who is roundly condemned here on this forum as being horrible. But I do not know if the author of your book is that person.

    A little more than one inch from the top of this page on the far right you should see a button that says, "Search". Immediately to the right of that button, you will see a text box that says in grayed-out letters "Google Custom Search". If you place the author's name in that text box and hit search, it should search through all the forum messages that contain that name. You can read their opinions there.

    At the very least, thank you for telling us where this particular question comes from. As I said, it has surfaced here at least once before.

    BTW, I have a further question on this. What is the point that Yashwant Kanetkar was trying to make with this example?

    Here is the general situation. Committees have met and discussed and published standards for the programming languages. Those standards specify what a given expression or statement, etc, written in the language will mean and how it is to be handled by the compiler. The writers of compilers for that language use that published standard as specification for how their compiler is supposed to translate the source code that it is given. For a compiler to be considered compliant, it must do what the standard tells it to do.

    There are some things that the standard does not specify. Such things are called "undefined behavior". Multiple pre/post-increment/decrement notation within the same sequence point is one such instance of "undefined behavior". Whether the arguments of a function call are evaluated from left to right or from right to left is another instance of "undefined behavior". "Undefined behavior" means that the compiler is absolutely free to do whatever it fracking well wants to do. Usually, the compiler designer will try to do something that makes sense (eg, fflush is only defined for output buffers, so fflush(stdin) is not defined, and yet some compilers will empty out the standard input buffer, stdin), but the important point is that you cannot depend on all compilers doing the exact same things for you. The only things that you can really count on all compilers doing for you is that which is specified in the language standard.

    As stated, multiple pre-/post increment/decrement within the same sequence point (refer to the first reference to this in the preceding message for the link to the Wikipedia page) are not defined. The link that I provided to my own page demonstrates the gross difference between two different methods of evaluating an expression.

    When you call a function, there are two possible ways to approach its arguments: from left to right, or from right to left. Think about that, because all that pre-/post-increment/decrement notation depends absolutely on whether you are working from left to right, or from right to left.

    As it turns out, the standard requires neither. That means that some compilers will work from left to right while others will work from right to left. And both ways are perfectly acceptable. Which means that your job is to write code that does not depend on which is used. Which means that the code you posted is totally hosed (meaning that it is completely wrong).

    Now, it turns out that for Intel processors, there is a convention for evaluating function arguments from right to left, but that doesn't mean that compilers based on other processors would do the same (any MacIntosh users out there willing to check it out?).

    But you still have not answered the questions that I had put to you.

    BTW, the output I got is exactly what I had expected. But then I knew to evaluate from right to left on an Intel box. What did you base your expectations on? You really do need to explore that in order to answer the question properly.
    Last edited by dwise1_aol; September 20th, 2013 at 02:26 AM.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    aswinas. In Message #4, salem pointed you to a very good message he had posted on another forum in which he demonstrated somewhat rigorously how entirely different compilers and even the same compiler with different settings will generate code for the exact same source code that yet acts completely differently.

    Please take the lessons he humbly offers to heart.

    Always turn warnings on and up! Never ignore warnings. Warnings are so much more important than error messages could ever hope to be. Never ignore warnings.

IMN logo majestic logo threadwatch logo seochat tools logo