a silent error

R

RoS

what does it say the standard for below program?
it has to run or not? or it is UB?
it is better know there is a error or the portability?
it compile and run with gcc?

-------------------------------
#include <stdio.h>

int main(void)
{char *p="123";

printf("jijdid\n", p);
return 0;
}
-------------------------------
 
S

santosh

RoS said:
what does it say the standard for below program?
it has to run or not? or it is UB?
it is better know there is a error or the portability?
it compile and run with gcc?

-------------------------------
#include <stdio.h>

int main(void)
{char *p="123";

printf("jijdid\n", p);
return 0;
}

It invokes undefined behaviour.
 
R

RoS

In data Mon, 03 Dec 2007 17:21:48 +0100, RoS scrisse:

--------------------
#include <stdio.h>

int main(void)
{char *p="123";

printf("jijdid\n", p);
return 0;
}
-------------------------------

for me not seem an error
 
S

santosh

Jean-Marc Bourguet said:
Where, why?

You are right sorry. Excess arguments after format specifiers are
processed are evaluated and ignored. So no undefined behaviour.
 
R

RoS

In data Mon, 03 Dec 2007 22:28:39 +0530, santosh scrisse:
Which is one possible outcome of Undefined behaviour.

in the sense i always wrong?

i post that because i did read something like above in my C code

i compile it using the current compiler in my pc but i
fear it could be not compile in some other one compiler
 
B

Barry Schwarz

In data Mon, 03 Dec 2007 17:21:48 +0100, RoS scrisse:

It is not an error from a language point of view. The standard
specifically allows it. However, there are some very popular
compilers which attempt to match the types of the subsequent arguments
against the specifications in the format string and will generate a
diagnostic if a mismatch is detected, as in this case. Furthermore,
when I see code like this I immediately assume the programmer forgot
something and wonder what else is missing from the function.


Remove del for email
 
H

Harald van Dijk

It is not an error from a language point of view. The standard
specifically allows it.

It almost does, but almost means it doesn't. It allows it in va_arg,
which may or may not be how printf is implemented. It would be a strange
implementation where this doesn't work, but it would be a valid one.
 
F

Flash Gordon

Harald van Dijk wrote, On 06/12/07 20:38:
It almost does, but almost means it doesn't. It allows it in va_arg,
which may or may not be how printf is implemented. It would be a strange
implementation where this doesn't work, but it would be a valid one.

Actually, the specification for fprintf explicitly specified that extra
parameters are evaluated but otherwise ignored. It is in the first
paragraph of the description in section 7.19.6.1 of n1256. So Barry was
correct.
 
H

Harald van Dijk

Harald van Dijk wrote, On 06/12/07 20:38:

Actually, the specification for fprintf explicitly specified that extra
parameters are evaluated but otherwise ignored. It is in the first
paragraph of the description in section 7.19.6.1 of n1256. So Barry was
correct.

Sorry, you and Barry are right, of course. I was thinking of something
else.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top