odd compiler behaviour

Y

yakir22

hi all,
can anyone explain me please why this line of code doesn't generate
any compiler error ( both on vs2005 and kdevelop ).

printf("%s") , "aaa";

this look totaly wrong ( or am i mistaken ).

Thanks in advance.
 
A

Alex

hi all,
can anyone explain me please why this line of code doesn't generate
any compiler error ( both on vs2005 and kdevelop ).

printf("%s") , "aaa";

this look totaly wrong ( or am i mistaken ).

Thanks in advance.

It's correct, 'Cause compiler view "," as operator.
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
hi all,
can anyone explain me please why this line of code doesn't generate
any compiler error ( both on vs2005 and kdevelop ).

printf("%s") , "aaa";

this look totaly wrong ( or am i mistaken ).

It is syntactically acceptable.
This line executes two instructions:
printf("%s");
"aaa";

Those two lines are valid instructions although the printf call is buggy
and "aaa" doesn't do anything.

If you compile with all warning activated ,you will certainly get some
warning:
//(with g++ -Wall)
In function `int main()':
5: warning: too few arguments for format
5: warning: right-hand operand of comma has no effect
 
P

Pascal J. Bourguignon

hi all,
can anyone explain me please why this line of code doesn't generate
any compiler error ( both on vs2005 and kdevelop ).

printf("%s") , "aaa";

this look totaly wrong ( or am i mistaken ).

You are "mistaken".

A statement may be an expression:

void f(){ 1+2; } // is valid C or C++.

An expression may be a list of expressions separated by commas:

1,2,3 // is a valid expression, whose value is 3.

void f(){ 1,2,3; } // is valid C or C++.

If any subexpression in such a sequence has a side effect, so much
good for you :)


int f(int x){ return x++,x*=2,printf("%d",x),x-1; } // is valid C or C++.

(And contrarily to:
g(x++,x*=2,printf("%d",x),x-1)
the expression:
(x++,x*=2,printf("%d",x),x-1)
has a totally well specified order of evaluation).


Therefore printf("%s"),"aaa" is a valid expression,
therefore printf("%s"),"aaa"; is a valid statement;
it calls the function printf with as argument a pointer to a vector of
three characters '%', 's', 0. and then it results in a pointer to a
vector of four characters, 'a', 'a', 'a' and 0, which it promptly ignores.
 
J

Juha Nieminen

Michael said:
It is syntactically acceptable.
This line executes two instructions:
printf("%s");
"aaa";

Note, however, that the original and those two instructions are not
the same thing. The original is one single expression which value is a
const char*.
 
D

Default User

Therefore printf("%s"),"aaa" is a valid expression,

It is syntactically valid, but not semantically.
therefore printf("%s"),"aaa"; is a valid statement;

Depending on your definition of "valid". I would not call it valid,
although there is no requirement for a diagnostic.
it calls the function printf with as argument a pointer to a vector of
three characters '%', 's', 0.

You mean an array. As vector is a standard library construct in C++,
that distinction is important.
and then it results in a pointer to a
vector of four characters, 'a', 'a', 'a' and 0, which it promptly
ignores.

Again, array.

As calling printf() with insufficient arguments for the format results
in undefined behavior, there's no requirement for anything specific to
happen after the function call. So it need not evaluate the second
expression at all.




Brian
 
P

peter koch

hi all,
can anyone explain me please why this line of code doesn't generate
any compiler error ( both on vs2005 and kdevelop ).

printf("%s") , "aaa";

this look totaly wrong ( or am i mistaken ).

Thanks in advance.

As others have pointed out, the syntax is fine. But I would expect a
compiler warning here, and recommend that you play with your compilers
settings: most likely you will be able to get the compiler to produce
a warning.

/Peter
 
J

Juha Nieminen

Default said:
It is syntactically valid, but not semantically.

Are you referring to the printf() calling syntax being incorrect (ie.
too few parameters)?

Actually, technically speaking, we don't know if it's semantically
incorrect or not. It may well be that 'printf' is a user-defined
function which takes just a const char*, in which case that expression
is completely valid, even semantically. (Moreover 'printf' might be also
an instance of a class which has an operator() member function defined,
ie. a functor.)
 
D

Default User

Juha said:
Are you referring to the printf() calling syntax being incorrect
(ie. too few parameters)?

Actually, technically speaking, we don't know if it's semantically
incorrect or not. It may well be that 'printf' is a user-defined
function which takes just a const char*, in which case that expression
is completely valid, even semantically. (Moreover 'printf' might be
also an instance of a class which has an operator() member function
defined, ie. a functor.)

I think that in all cases where a function is used that has the name of
a standard library function, it must be assumed that it is being
referenced unless there has been a definite statement to the contrary.



Brian
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top