C陷阱是标准错误还是实现失误? [英] Are C pitfalls the mistake of standard or that of implementations ?

查看:56
本文介绍了C陷阱是标准错误还是实现失误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们看到C对于非标准语法有点自由,

例如当我尝试

< printf(" string1"," string2%d",v1);>在Dev-C ++中,它打印了string1

并且没有出现错误,这是标准错误还是

实现(Dev-C ++)。

我的印象是,通过实施适当的规则并同时遵守标准(ANSI

C),实施可能更好。例如,在上面的例子中,我假设

标准会指定类似实现

必须提供printf(字符串,其中%是特殊字符。 。,var1,..);。现在

a好的实现者可以实现这个以及错误chek

看到用户代码严格遵守该格式并引发错误

如果不是(我的例如,上面)。

但是,许多实现,甚至是流行的实现似乎都不这样做。

你会选择什么?读过实际标准的人可以对标准中的规格进行一些说明。


Greenhorn。

Hi,
We see that C is a bit liberal towards non-standard syntax,
for e.g. when i tried
<printf("string1", "string2 %d", v1);> in Dev-C++, it printed string1
and no error was raised, is this the mistake of standard or the
implementation (Dev-C++).
I was under the impression that the implementations could have been
better by implementing proper rules and adhering to the standard (ANSI
C) at the same time, for e.g., in the above case i was assuming that
the standard would have specified something like "an implementation
MUST provide printf("string where % is special char..", var1,..);". Now
a good implementor could have implemented this along with an error chek
to see the user code strictly adheres to that format and raise an error
if not (my e.g., above).
But, many implementations, even popular ones don''t seem to do this.
What do u opine? People who have read the actual Standard can throw
some light on the specification in the standard.

Greenhorn.

推荐答案



Greenhorn写道:

Greenhorn wrote:

我们看到C对非 - 标准语法,例如当我尝试
< printf(" string1"," string2%d",v1);>在Dev-C ++中,它打印了string1
并且没有出现错误,这是标准错误或
实现(Dev-C ++)。
我的印象是实现可以通过实施适当的规则并同时遵守标准的
(ANSI C)更好,例如,在上述情况下,我假设标准会指定某些内容喜欢一个实现
必须提供printf(字符串,其中%是特殊字符..,var1,..);。
现在一个好的实现者可以实现这个以及错误
chek以查看用户代码严格遵守该格式并且如果没有(例如,上面),则引发
错误。但是,许多实现,甚至是流行的实现似乎都不会这样做。
你会选择什么?读过实际标准的人可以对标准中的规范有所了解。

Greenhorn。
Hi,
We see that C is a bit liberal towards non-standard syntax,
for e.g. when i tried
<printf("string1", "string2 %d", v1);> in Dev-C++, it printed string1
and no error was raised, is this the mistake of standard or the
implementation (Dev-C++).
I was under the impression that the implementations could have been
better by implementing proper rules and adhering to the standard (ANSI C) at the same time, for e.g., in the above case i was assuming that
the standard would have specified something like "an implementation
MUST provide printf("string where % is special char..", var1,..);". Now a good implementor could have implemented this along with an error chek to see the user code strictly adheres to that format and raise an error if not (my e.g., above).
But, many implementations, even popular ones don''t seem to do this.
What do u opine? People who have read the actual Standard can throw
some light on the specification in the standard.

Greenhorn.



如果你调用未定义的行为,任何实现都可以用你的程序做任何事情

。换句话说,如果你不遵守规则,

任何事情都可能发生,对于任何

实施,这'任何'都可以是不同的


If you invoke undefined behaviour, any implementation can do anything
with your program. In others words "if you don''t follow the rules,
anything can happen, and this ''anything'' can be different for any
implementation"


文章< 11 ********************** @ f14g2000cwb.googlegroups。 com>,

Greenhorn< te ************ @ yahoo.com>写道:

:我们看到C对非标准语法有点自由,

:例如:当我尝试

:< printf(" string1"," string2%d",v1);>在Dev-C ++中,它打印了string1

:并且没有出现错误,这是标准错误还是

:实现(Dev-C ++)。


两者都没有。 printf()

的定义中没有要求所有的值参数都需要消耗。通过printf()。


:我的印象是实施可能是

:通过实施适当的规则并遵守标准更好( ANSI

:C)同时,例如,在上述情况下我假设

:标准会指定类似实现的内容/>
:必须提供printf(字符串,其中%是特殊字符..,var1,..);。


我不是了解最后一点。标准

中没有任何内容要求printf()的第一个参数具有%格式

说明符。


:现在

:一个好的实现者可以实现这个以及错误chek

:看到用户代码严格遵守该格式并引发错误

:如果没有(我的例如,上面)。


这不是一个错误。一些编译器会警告它,lint也会发出警告。

-

如果一个巨魔和一半可以在一个帖子中挂一个读者和一半一半,

有多少读者可以在6个帖子中找到6个巨魔?
In article <11**********************@f14g2000cwb.googlegroups .com>,
Greenhorn <te************@yahoo.com> wrote:
: We see that C is a bit liberal towards non-standard syntax,
:for e.g. when i tried
:<printf("string1", "string2 %d", v1);> in Dev-C++, it printed string1
:and no error was raised, is this the mistake of standard or the
:implementation (Dev-C++).

Neither. There is no requirement in the definition of printf()
that all of the value arguments need to be "consumed" by the printf().

:I was under the impression that the implementations could have been
:better by implementing proper rules and adhering to the standard (ANSI
:C) at the same time, for e.g., in the above case i was assuming that
:the standard would have specified something like "an implementation
:MUST provide printf("string where % is special char..", var1,..);".

I don''t understand that last bit. There is nothing in the standard
that requires that the first argument to printf() have a % format
specifier.

:Now
:a good implementor could have implemented this along with an error chek
:to see the user code strictly adheres to that format and raise an error
:if not (my e.g., above).

It isn''t an error. Some compilers will warn about it, as will ''lint''.
--
If a troll and a half can hook a reader and a half in a posting and a half,
how many readers can six trolls hook in six postings?


" Greenhorn" < TE ************ @ yahoo.com>写道:
"Greenhorn" <te************@yahoo.com> writes:
我们看到C对于非标准语法有点自由,对于例如当我尝试
< printf(" string1"," string2%d",v1);>在Dev-C ++中,它打印了string1
并且没有出现错误,这是标准错误还是
实现(Dev-C ++)。
We see that C is a bit liberal towards non-standard syntax,
for e.g. when i tried
<printf("string1", "string2 %d", v1);> in Dev-C++, it printed string1
and no error was raised, is this the mistake of standard or the
implementation (Dev-C++).




这是正确的行为。这通常是一个错误,所以更好

C实现会警告它,如果你打开那个

功能。 GCC是一个实现的例子。

-

当我不得不依靠不足时,我更喜欢它是我自己的。

- 理查德希思菲尔德



This is the correct behavior. It is usually a mistake, so better
C implementations will warn about it, if you turn on that
feature. GCC is an example of an implementation that will do so.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield


这篇关于C陷阱是标准错误还是实现失误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆