Please explain the o/p

Discussion in 'C Programming' started by Kenny McCormack, Jul 30, 2005.

  1. Program:

    #include <stdio.h>

    int main(void)
    {
    int indiarocks = 2, j = 3;
    char webeingood[80];

    sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
    j = 27;
    puts(webeingood);
    }

    O/P is 5. Why?
     
    Kenny McCormack, Jul 30, 2005
    #1
    1. Advertising

  2. Kenny McCormack wrote:

    > Program:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int indiarocks = 2, j = 3;
    > char webeingood[80];
    >
    > sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
    > j = 27;
    > puts(webeingood);
    > }
    >
    > O/P is 5. Why?


    At the sprintf, indiarocks + j evaluates to 2 + 3, which is 5, so the
    webeingood array is used for storing the string "5".

    The fact that you change j after this is utterly irrelevant. sprintf simply
    builds a string; it doesn't create a dynamic binding between the string and
    the objects whose values were used to build that string.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    mail: rjh at above domain
     
    Richard Heathfield, Jul 30, 2005
    #2
    1. Advertising

  3. Kenny McCormack

    Eric Sosman Guest

    Kenny McCormack wrote:
    > Program:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int indiarocks = 2, j = 3;
    > char webeingood[80];
    >
    > sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
    > j = 27;
    > puts(webeingood);
    > }
    >
    > O/P is 5. Why?


    Because you're using a C implementation that's broken.
    The output should have been "indiarocks + j = 5\n"; since
    that's not what you got, complain to the C vendor.

    Oh, that *is* what you got? Well, why didn't you say
    so? If you suffer a bad fall and fear you've broken your
    right arm, do you tell the doctor it's your left great toe
    that hurts? If you ever expect to get anywhere in writing,
    reading, or using computer programs, Kenny, you need to
    develop the ability to observe in detail and describe with
    accuracy.

    Now to your question -- the one you didn't quite ask, but
    should have. The sprintf() call fills the initial portion
    of webeingood[] with a string of characters determined by the
    values of the sprintf() arguments at the time of the call.
    At that time, indiarocks is equal to 2 and j is equal to 3,
    so the third sprintf() argument is equal to 5 and sprintf()
    places "indiarocks + j = 5" in webeingood[].

    Subsequent changes to the value of j -- or of indiarocks,
    for that matter -- don't change what's already been put into
    webeingood[]. Like any other variable, webingood[] retains
    the last value stored until you store a new one. (If you
    write your weight on a slip of paper and then go on a three-
    week feeding binge and gain twenty kilos, does the number
    on the paper change automatically?) When you arrive at the
    puts() call, webeingood[] still holds the string that sprintf()
    deposited in it, and puts() outputs that string followed by a
    newline.

    Variables in C are not like "variables" in mathematics,
    nor like "formulas" in spreadsheets. They're more like pieces
    of paper: you can write things on them and read them back again,
    you can erase what's written and write something new, but the
    paper doesn't self-erase or self-rewrite.

    (Pedantry warnings: Yes, I know about "volatile." Yes,
    I know about "storage duration." Yes, I know Kenny should
    return a value from main(), lest his implementation not yet
    conform to C99. All such matters -- well, maybe except the
    last -- are beyond Kenny's current knowledge, and anyone who
    carps on them is doing him no favors.)

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 30, 2005
    #3
  4. Eric Sosman wrote:

    > If you ever expect to get anywhere in writing,
    > reading, or using computer programs, Kenny, you need to
    > develop the ability to observe in detail and describe with
    > accuracy.


    I'm stuffed, then. I didn't notice that either.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    mail: rjh at above domain
     
    Richard Heathfield, Jul 30, 2005
    #4
  5. Kenny McCormack

    Joe Wright Guest

    Kenny McCormack wrote:
    > Program:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int indiarocks = 2, j = 3;
    > char webeingood[80];
    >
    > sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
    > j = 27;
    > puts(webeingood);
    > }
    >
    > O/P is 5. Why?
    >

    Because 2 plus 3 equals 5. 'j = 27;' has no effect on webeingood.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jul 30, 2005
    #5
  6. Kenny McCormack wrote on 30/07/05 :
    > int indiarocks = 2, j = 3;

    <...>
    > sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);

    <...>

    > O/P is 5. Why?


    Because 'indiarocks + j' (that is 2 + 3) evaluates to 5. What's your
    point exactly ?

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "It's specified. But anyone who writes code like that should be
    transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
     
    Emmanuel Delahaye, Jul 30, 2005
    #6
  7. 2+3=5


    <posted & mailed>

    Kenny McCormack wrote:

    > Program:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int indiarocks = 2, j = 3;
    > char webeingood[80];
    >
    > sprintf(webeingood,"indiarocks + j = %d",indiarocks+j);
    > j = 27;
    > puts(webeingood);
    > }
    >
    > O/P is 5. Why?


    --
    Remove '.nospam' from e-mail address to reply by e-mail
     
    James McIninch, Jul 30, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. suzy
    Replies:
    3
    Views:
    450
  2. BluDog

    Can Someone Please Explain...

    BluDog, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    477
    BluDog
    Oct 5, 2004
  3. Alan Silver
    Replies:
    1
    Views:
    483
    Alan Silver
    Jun 28, 2005
  4. KK
    Replies:
    2
    Views:
    595
    Big Brian
    Oct 14, 2003
  5. Kaye Ng
    Replies:
    8
    Views:
    178
    Josh Cheek
    Jun 8, 2010
Loading...

Share This Page