473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

odd compiler behaviour

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.
Sep 17 '08 #1
6 1079
On 9ÔÂ17ÈÕ, ÏÂÎç4ʱ04·Ö, yaki...@gmail.com wrote:
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.
Sep 17 '08 #2
Michael DOUBEZ wrote:
>printf("%s") , "aaa";

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*.
Sep 17 '08 #3
Pascal J. Bourguignon wrote:
ya*****@gmail.com writes:
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";
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
Sep 17 '08 #4
On 17 Sep., 10:04, yaki...@gmail.com wrote:
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
Sep 17 '08 #5
Default User wrote:
>Therefore printf("%s"),"aaa" is a valid expression,

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.)
Sep 17 '08 #6
Juha Nieminen wrote:
Default User wrote:
Therefore printf("%s"),"aaa" is a valid expression,
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.)
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
Sep 17 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jon morgan | last post by:
Hi, I get random errors raised when compiling a multi-project application. For example the compiler will claim that a class has no constructor when it does. If I replace the first line of the...
10
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class...
7
by: duckfreezone | last post by:
Hi, I've got a small test program which behaves correctly (IMHO) on all compilers that I have acccess to, with the exception of the gcc on Macintosh OSX "gcc version 4.0.0 (Apple Computer, Inc....
6
by: Keith Vetter | last post by:
Hi, After a long debugging session I tracked down a bug to the following line of code: np = &nl; /* nl & np are NODE * */ The problem is that nl is volatile--alloc_memory() may change it...
9
by: gvarndell | last post by:
Hi, In the following code fragment, gcc seems to ignore the initial value assigned to pData. (compiled -fvolatile -O2 with gcc) void test(void) { void *pData = (void*)0x3400; pData =...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
5
by: Ondrej Spanel | last post by:
I though that inline functions should be always visible only in the compilation unit where they are defined - meaning if compiler cannot inline them, they should be handled as if declared static....
4
by: ksukhonosenko | last post by:
This message was originally posted to comp.lang.c++.moderated ---------------------------------------------------------------------------------------------- Hi! I face a problem in my...
29
by: Ark | last post by:
A function int foo(struct T *x) { return (x+1)-x; } should always return a 1, no matter how T is defined. (And int could be replaced with ptrdiff_t for you pedants.) For one thing, it was...
6
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I found a really strange quirk in the C# compiler, which I can't beleive is proper behaviour. If I define a class thus: public class MyClass { public override string ToString() { return...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.