printf() function

C

Ceriousmall

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.
 
C

Ceriousmall

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.
 
I

Ian Collins

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.
 
G

gwowen

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.
 
I

Ike Naar

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
 
J

James Kuyper

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top