Thread: printf returning values problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    printf returning values problem

    Hey all,
    Just looking through some notes and came accros this code asking what the printout is... I don't understand why n has different values for each.... Is the first value for n a junk value?
    Cheers in advance for any wise words.

    Code:
    #include<stdio.h>
    main() 
    {   
    int n;
    n=printf("n is %d \n",n);
    n=printf("n is %d \n",n);
       
    int k; /*here just to keep the program running*/
    scanf("%d",&k);
    }

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Quote Originally Posted by hayai32
    Hey all,
    Is the first value for n a junk value?
    Yes, you're right.

    And you can use getchar() to keep the program running

  3. #3
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    Yes n will be a junk value. As far as I know c & c++ gives a variable a random value if you dont give it one. This can give some strange output is you are not careful
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    Cheers.

    If you guys got this type of Q in an exam what would you put down?
    Also would the junk variable value be different for different compilers?

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    It could be different everytime you compile it. It is given the last value that was left in that section of memory.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Marlon
    As far as I know c & c++ gives a variable a random value if you dont give it one.
    C and C++ don't give it anything. That's the whole point or problem. It doesn't initialize it, so whatever was in that spot in memory before this variable was pushed there, is the value you get. But C and C++ don't give it anything. If they did, then it would be initialized. This is really telling with pointers, where when you don't initalize them before you use them, they end up just pointing wherever.

    They get a value based on whatever was in memory in that spot before, just like non-pointer variables, which translates into a memory address, which then ends up more often than not pointing to a data segment you don't have any business doing anything with. It's a Segfault waiting to happen.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    .
    Join Date
    Nov 2003
    Posts
    307
    From what I see, printf() is defined as
    Code:
    int printf(const char *,...);
    meaning it has a return value.

    and it returns the number of characters printed, or a negative number when an error occurs.
    So, n in the OP's code on the second printf call will show 8, I believe. Is this not what you see? In the first call to printf, n can be any value.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The first call, the output would be:
    "n is [any random value]"

    The second time, it would be:
    "n is [7 + length of the number 'any random value']"


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by quzah
    As far as I know c & c++ gives a variable a random value if you dont give it one.
    C and C++ don't give it anything. That's the whole point or problem. It doesn't initialize it, so whatever was in that spot in memory before this variable was pushed there, is the value you get.
    Actually, not all variables. Global variables are initialized to zero, and I think static variables are too. But basically quzah's right. And you should initialize varialbes anyway, to get into the habit.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The comment was in regards to the current program. Of course globals are initalized, as are static. This program however uses neither of those, which is what the reply was tailored to.

    You are also misquoting me. I didn't say the "As far as I know..." line.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    Cheers for the comments.
    The second printout value is understood. no worries. But...

    With the first one the value printed out for n (or any other letter) is always constant (2) each time I run the program. and...

    If the value printed for the first one is the last vale stored in that address then why don't they both return the same value after running the program twice in a row?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why should a program be in the same spot in memory on consecutive runs? That's purely an OS issue.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Unintialized local variables are not constrained to have any value, hence typically get whatever crap happens to be on the stack (this is a performance optimization built-in to C/C++). The stack crap typically is quite reproducible (and one of the main reasons why a lot of bugs like this can be hidden for years until a tiny change is made someplace else in the program) for a given compiler with the same flag settings.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  14. #14
    .
    Join Date
    Nov 2003
    Posts
    307
    The stack is dynamically allocated from memory by the OS, with each process having a different starting physical address. Depending on the OS, when you repeatedly run the same code, it may get a different place in memory, so the stack area where the local variables live comes from different places in memory. It's like moving into a new home - you can't really control the street address.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int n;
    > n=printf("n is %d \n",n);
    There's no reason to suppose that the function would ever return. The only type in C which is guaranteed NOT to have any trap values is unsigned char. If you use an uninitialised value of any other type, then you're on your own.
    So whilst your average desktop machine may have constant 'junk' which does not cause an exception, do not make the mistake of assuming that you're going to get the same answer elsewhere.

    > Also would the junk variable value be different for different compilers?
    Varies by operating system
    Varies by compiler
    Varies by compiler option
    May even vary by which program(s) you've run previously - who knows.

    > with each process having a different starting physical address
    Most desktop operating systems use virtual memory - create two "hello world" programs (with a long pause at the end), and run several copies of it (printing out addresses of various objects).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. menu problem!!! need U R G E N T help!!!
    By catcat28 in forum C Programming
    Replies: 16
    Last Post: 11-19-2007, 01:32 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM