Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a silent error

Reply
Thread Tools

a silent error

 
 
RoS
Guest
Posts: n/a
 
      12-03-2007
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;
}
-------------------------------
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      12-03-2007
RoS wrote:

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

 
Reply With Quote
 
 
 
 
RoS
Guest
Posts: n/a
 
      12-03-2007
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
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      12-03-2007
RoS wrote:

> 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


Which is one possible outcome of Undefined behaviour.

 
Reply With Quote
 
Jean-Marc Bourguet
Guest
Posts: n/a
 
      12-03-2007
santosh <(E-Mail Removed)> writes:

> RoS wrote:
>
> > 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.


Where, why?

--
Jean-Marc
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      12-03-2007
Jean-Marc Bourguet wrote:

> santosh <(E-Mail Removed)> writes:
>
>> RoS wrote:
>>
>> > 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.

>
> Where, why?


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

 
Reply With Quote
 
RoS
Guest
Posts: n/a
 
      12-03-2007
In data Mon, 03 Dec 2007 22:28:39 +0530, santosh scrisse:
>RoS wrote:
>
>> 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

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

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-06-2007
On Mon, 03 Dec 2007 17:23:57 +0100, RoS <(E-Mail Removed)> wrote:

>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


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
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      12-06-2007
On Thu, 06 Dec 2007 04:07:36 -0800, Barry Schwarz wrote:
> On Mon, 03 Dec 2007 17:23:57 +0100, RoS <(E-Mail Removed)> wrote:
>>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

>
> 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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-06-2007
Harald van Dijk wrote, On 06/12/07 20:38:
> On Thu, 06 Dec 2007 04:07:36 -0800, Barry Schwarz wrote:
>> On Mon, 03 Dec 2007 17:23:57 +0100, RoS <(E-Mail Removed)> wrote:


<snip>

>>>> printf("jijdid\n", p);
>>>> return 0;
>>>> }
>>>> -------------------------------
>>> for me not seem an error

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


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.
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
silent error in email module rb.online@gmail.com Python 0 11-27-2006 11:36 AM
PAgP - silent and non-silent modes andymacca Cisco 1 03-26-2006 06:28 PM
E-Power Technology Silent Engine Tiger 550W Modular PSU Silverstrand Front Page News 1 09-15-2005 06:30 AM
Review: Sharkoon Silent Eagle 80mm Fans Silverstrand Reviews & How-To's 15 09-14-2005 03:50 AM
NorthQ 500W Ultra Silent Power Supply Review @ PC Modding Ma Silverstrand Front Page News 0 08-09-2005 08:28 PM



Advertisments