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

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    2

    Getting garbages?


    main()
    {
    int i=5;
    printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
    }

    Answer required: 400..300

    but i get some garbages.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    What do your warning messages say?

    Because of the format string, printf is expecting six arguments, but you are only giving it five. It is bound and determined to print out six numbers, so it goes to the next location where a number should be and grabs whatever garbage is sitting there.

    Originally Posted by Henry Spencer
    If you lie to the compiler, it will get its revenge.
    You lied to the compiler. It got its revenge.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    2
    ok.
    I jus cpoied the problem.
    let the %d should be five.
    what then?

    Revenge, that is a nice one.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    "what then?" what? The program is a bad one to start with, since it depends on undefined behavior that will be different depending on the compiler and platform you compile and run it on. You need to realize that that is not the way to write code and to avoid it.

    When you use printf and scanf, you must always verify that the number of arguments matches the number of flags in the format string. And when you get something like what you got, that should be the first thing that you check.

    I've gotten warnings from my compiler when that happened, so you should have gotten warnings too. Never ignore warnings. Always turn warnings on and up! And never ignore warnings.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    2
    Thank you.
    Now onwards I won't repeat this.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    That is not actually what I wanted you to learn.

    In the signature of one of the forum members, it says something like this (quoting from memory):
    It's not the case that experienced programmers are that much better than beginners. Rather, it's that we have made the same mistakes so many times that we just correct them automatically.
    You see, you will repeat that same mistake again. Several times throughout your career, in fact. We all make that same mistake on occasion, even the most experienced programmers. And lots of other mistakes as well.

    But what distinguishes us experienced programmers from beginners is that we immediately work to identify and to correct those mistakes. Part of our ability to do that is the practical experience of having made those mistakes before. We learn to identify the symptoms. If we get a particular error or warning message, then we already have an idea of what we had done wrong this time. In fact, in the past I have informed a beginner that the reason why we could spot his error so quickly was because we had made the same mistake before so we knew what to look for. It may appear that we can read your mind, but in reality we're just drawing on our own personal experience.

    What I want you to learn from that mistake is how to identify it, what's happening when you make that mistake, and how to correct it. So that the next time you encounter that same mistake, you will be able to correct it immediately instead of having to post it to a forum and wait for a few days before you can be given a solution. The time savings is very subtantial, especially considering how much time we need to devote to developing software.

    Ultimately, even though the ideal goal is to learn to avoid mistakes, the most valuable lesson is to develop the ability to immediately correct and recover from mistakes.

IMN logo majestic logo threadwatch logo seochat tools logo