printf() function

Discussion in 'C Programming' started by Ceriousmall, Feb 29, 2012.

  1. Ceriousmall

    Ceriousmall Guest

    I've wrote some code containing the statement printf("%s %d %d\n",
    some_str, some_funct(), some_var)
    where some_var is an extern variable that's assigned its value within
    some_funct().
    However, the value of some_var is not printed unless the call to this
    variable is placed within a separate printf() statement.. i.e.
    printf("%s %d", some_str, some_funct())
    printf(" %d\n", some_var)

    So I'm wondering, what would this behavior?
    If need be I'll post the code aswell.
    Ceriousmall, Feb 29, 2012
    #1
    1. Advertising

  2. Ceriousmall

    Ceriousmall Guest

    The statement printf("%s %d %d\n", some_str, some_funct(), some_var)
    is on a single line but it seems to have been split during my post,
    so this is just for the record.
    Ceriousmall, Feb 29, 2012
    #2
    1. Advertising

  3. Ceriousmall

    Ian Collins Guest

    On 02/29/12 08:41 PM, Ceriousmall wrote:
    > I've wrote some code containing the statement printf("%s %d %d\n",
    > some_str, some_funct(), some_var)
    > where some_var is an extern variable that's assigned its value within
    > some_funct().
    > However, the value of some_var is not printed unless the call to this
    > variable is placed within a separate printf() statement.. i.e.
    > printf("%s %d", some_str, some_funct())
    > printf(" %d\n", some_var)
    >
    > So I'm wondering, what would this behavior?


    Undefined.

    --
    Ian Collins
    Ian Collins, Feb 29, 2012
    #3
  4. Ceriousmall

    gwowen Guest

    On Feb 29, 7:41 am, Ceriousmall <> wrote:

    > printf("%s %d %d\n", some_str, some_funct(), some_var)
    > However, the value of some_var is not printed unless the call to this
    >
    > So I'm wondering, what would this behavior?
    > If need be I'll post the code aswell.


    There's no guarantee that some_funct() gets called before the variable
    some_var is evaluated (i.e. accessed, read) for use in the function
    call. Putting the reference to some_var in a separate statement
    introduces a sequence point point between the call to some_funct() and
    the evaluation of some_var, thereby guaranteeing the function call
    happens first.
    gwowen, Feb 29, 2012
    #4
  5. Ceriousmall

    Ike Naar Guest

    On 2012-02-29, Ceriousmall <> wrote:
    > I've wrote some code containing the statement printf("%s %d %d\n",
    > some_str, some_funct(), some_var)
    > where some_var is an extern variable that's assigned its value within
    > some_funct().
    > However, the value of some_var is not printed unless the call to this
    > variable is placed within a separate printf() statement.. i.e.
    > printf("%s %d", some_str, some_funct())
    > printf(" %d\n", some_var)
    >
    > So I'm wondering, what would this behavior?
    > If need be I'll post the code aswell.


    The arguments of a function call are evaluated in
    unspecified order.
    So in printf("%s %d %d\n", some_str, some_funct(), some_var)
    the evaluation of some_funct() may happen before or after the
    evaluation of some_var.

    Hint: questions like these are addressed in the comp.lang.c
    frequently asked questions list,

    http://c-faq.com

    Your question is item 3.7
    Ike Naar, Feb 29, 2012
    #5
  6. Ceriousmall

    James Kuyper Guest

    On 02/29/2012 03:24 AM, Ian Collins wrote:
    > On 02/29/12 08:41 PM, Ceriousmall wrote:
    >> I've wrote some code containing the statement printf("%s %d %d\n",
    >> some_str, some_funct(), some_var)
    >> where some_var is an extern variable that's assigned its value within
    >> some_funct().
    >> However, the value of some_var is not printed unless the call to this
    >> variable is placed within a separate printf() statement.. i.e.
    >> printf("%s %d", some_str, some_funct())
    >> printf(" %d\n", some_var)
    >>
    >> So I'm wondering, what would this behavior?

    >
    > Undefined.


    Citation? Reason?

    It is unspecified whether the value of some_var is extracted before or
    after the evaluation of some_funct(), and it's therefore not guaranteed
    whether the value printed will be the one before or after the assignment
    that occurs inside of some_funct(). However, the sequence points inside
    of some_funct() serve to adequately separate the assignment to some_var
    from the reading of some_var, regardless of which of the two occurs first.
    --
    James Kuyper
    James Kuyper, Feb 29, 2012
    #6
  7. Ceriousmall

    Ceriousmall Guest

    Ok thanks for the replies guys and I've bookmarked that faq......
    Ceriousmall, Feb 29, 2012
    #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. Prakru

    Function pointers to printf

    Prakru, Apr 20, 2004, in forum: C++
    Replies:
    4
    Views:
    405
    Michiel Salters
    Apr 20, 2004
  2. ben
    Replies:
    4
    Views:
    599
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,222
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    423
  5. guru
    Replies:
    8
    Views:
    264
Loading...

Share This Page