Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf() function

Reply
Thread Tools

printf() function

 
 
Ceriousmall
Guest
Posts: n/a
 
      02-29-2012
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.
 
Reply With Quote
 
 
 
 
Ceriousmall
Guest
Posts: n/a
 
      02-29-2012
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.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-29-2012
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
 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      02-29-2012
On Feb 29, 7:41*am, Ceriousmall <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      02-29-2012
On 2012-02-29, Ceriousmall <(E-Mail Removed)> 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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-29-2012
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
 
Reply With Quote
 
Ceriousmall
Guest
Posts: n/a
 
      02-29-2012
Ok thanks for the replies guys and I've bookmarked that faq......
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments