Question c faq

E

Emilio

Hi,
I was reading the faqs from c-faq.com and on the C Preprocesor Section
in question 10.27 there is a piece of code:

printf("DEBUG: \"%s\", line %d: ",
__FILE__,__LINE__),printf("i is %d", i);

That I quite don't understand. Whats the comma for?. I thought it
should be changed for a ;

I tried it in a program and it that prints out

DEBUG: "main.c", line 10: i is 3

giving i the value 3. In this case it gives the same output if the
comma was changed for a ;.

I don't understand what it is used for how how it works.

TIA
 
E

Eric Sosman

Emilio said:
Hi,
I was reading the faqs from c-faq.com and on the C Preprocesor Section
in question 10.27 there is a piece of code:

printf("DEBUG: \"%s\", line %d: ",
__FILE__,__LINE__),printf("i is %d", i);

That I quite don't understand. Whats the comma for?. I thought it
should be changed for a ;

I tried it in a program and it that prints out

DEBUG: "main.c", line 10: i is 3

giving i the value 3. In this case it gives the same output if the
comma was changed for a ;.

I don't understand what it is used for how how it works.

Try it both ways (with comma and with semicolon)
in a context like

if (i < 0)
printf("DEBUG: \"%s\", line %d: ",
__FILE__,__LINE__), printf("i is %d", i);

.... and be sure to try both negative and positive values
of `i'. Ask again if it's still confusing.
 
E

Emilio

what I wanted to know is why use
printf("DEBUG: \"%s\", line %d: ", __FILE__,__LINE__),
printf("i is %d", i);

and not
printf("DEBUG: \"%s\", line %d: ", __FILE__,__LINE__);
printf("i is %d", i);

I've been searching and it's the comma operator that bugs me. I don'
see why to use it with printf expressions in macros( this example is
from the expanded macro from the c faq.

Btw i value does not chand the printed output
 
E

Emilio

Right, I think I've got it.
The porpoises of the macro in the faq was to have several expression
executed as one. One way to do it would be

#define onemacro(p) do { print1(...); print2(...); } while(0)
print1 and print2 are execued in the same block

which is the same as

#define onemacro(p) print1(...), print2(...);
 
C

CBFalconer

Emilio said:
I was reading the faqs from c-faq.com and on the C Preprocesor
Section in question 10.27 there is a piece of code:

printf("DEBUG: \"%s\", line %d: ",
__FILE__,__LINE__),printf("i is %d", i);

That I quite don't understand. Whats the comma for?. I thought
it should be changed for a ;

I assume you are worrying about the comma in "),printf". It is
actually very poor code leading to undefined behaviour, IMNSHO. A
clearer (and more likely correct) version might be:

printf("DEBUG: \"%s\", line %d: i is %d",
__FILE__, __LINE__, i);

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Keith Thompson

Emilio said:
I was reading the faqs from c-faq.com and on the C Preprocesor Section
in question 10.27 there is a piece of code:

printf("DEBUG: \"%s\", line %d: ",
__FILE__,__LINE__),printf("i is %d", i);

That I quite don't understand. Whats the comma for?. I thought it
should be changed for a ;

I tried it in a program and it that prints out

DEBUG: "main.c", line 10: i is 3

giving i the value 3. In this case it gives the same output if the
comma was changed for a ;.

I don't understand what it is used for how how it works.

It's a comma operator. It evaluates its left operand (and discards
the result), then it evaluates the right operand and yields its
result.

It can be a little confusing because the comma is used for other
things as well (such as separating function arguments).
 
K

Keith Thompson

CBFalconer said:
I assume you are worrying about the comma in "),printf". It is
actually very poor code leading to undefined behaviour, IMNSHO. A
clearer (and more likely correct) version might be:

printf("DEBUG: \"%s\", line %d: i is %d",
__FILE__, __LINE__, i);

The code in the FAQ shouldn't lead to undefined behavior if used
properly.

Here's the macro definition from the FAQ:

#define DEBUG printf("DEBUG: \"%s\", line %d: ", \
__FILE__,__LINE__),printf

Then this:

DEBUG("i is %d", i);

expands to:

printf("DEBUG: \"%s\", line %d: ",
__FILE__,__LINE__),printf("i is %d", i);

It's tricksy, but it works.
 
E

Eric Sosman

CBFalconer said:
I assume you are worrying about the comma in "),printf". It is
actually very poor code leading to undefined behaviour, IMNSHO. A
clearer (and more likely correct) version might be:

printf("DEBUG: \"%s\", line %d: i is %d",
__FILE__, __LINE__, i);

Could you explain your NSHO? What is it in the original
that might provoke undefined behavior? (Let's assume that the
fragment appears in an executable context, with an appropriate
declaration of `i' in scope, with <stdio.h> included, and that
the program will eventually output a newline -- what is it about
the fragment itself that troubles you?)
 
B

Ben C

Could you explain your NSHO? What is it in the original
that might provoke undefined behavior? (Let's assume that the
fragment appears in an executable context, with an appropriate
declaration of `i' in scope, with <stdio.h> included, and that
the program will eventually output a newline -- what is it about
the fragment itself that troubles you?)

I wonder if in the former version the second printf expression could be
evaluated before the first one?
 
M

Malcolm

Keith Thompson said:
It's a comma operator. It evaluates its left operand (and discards
the result), then it evaluates the right operand and yields its
result.

It can be a little confusing because the comma is used for other
things as well (such as separating function arguments).
It is also the first time I've seen one used, since as long as I can
remember.
 
E

Eric Sosman

Ben said:
I wonder if in the former version the second printf expression could be
evaluated before the first one?

No, by the properties of the comma operator. The
left operand is guaranteed to be evaluated first, and
there is a sequence point before starting to evaluate
the right operand. That's why I'm perplexed by CBF's
uneasiness.
 
C

Christopher Benson-Manica

Malcolm said:
It is also the first time I've seen one used, since as long as I can
remember.

I haven't looked at the FAQ to see what the rationale for using it
was, but I can say that the only time I've used the comma operator was
to hack around a compiler bug.
 
C

CBFalconer

Eric said:
No, by the properties of the comma operator. The
left operand is guaranteed to be evaluated first, and
there is a sequence point before starting to evaluate
the right operand. That's why I'm perplexed by CBF's
uneasiness.

I misread it, and had the 2nd printf as an argument to the DEBUG.
I still don't like it.

IMNSHO = in my not so humble opinion.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

CBFalconer

Christopher said:
I haven't looked at the FAQ to see what the rationale for using
it was, but I can say that the only time I've used the comma
operator was to hack around a compiler bug.

A common use, at least for me, is in for initialization and
looping:

for (x = 0, y = 1, z = 2; x < MAX; x++, y--) {
/* stuff using x, y, z */
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top