c标准 [英] c standard

查看:69
本文介绍了c标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gcc和g ++可以进行以下操作:


#include< math.h>


double sin(double)

{

返回1;

}


int main()

{

sin(1);

返回1;

}


为什么我不喜欢不会收到任何警告:


在math.h中定义的罪行...


当我用-Wall -pedantic编译时-ansi。


为什么可以覆盖罪的定义,

是标准的这一部分?


其次是双重罪的定义(不是声明)(双重)

错过一个变量!

如果代码中没有引用变量,这可以吗?


在此先感谢,

Florian

解决方案

theoderich写道:< blockquote class =post_quotes>使用gcc和g ++可以进行以下操作:

< math.h>

双罪(dou) ble)
{
返回1;
}
int main()
{
sin(1);
返回1;
}

为什么我没有收到任何警告:

在math.h中经常定义的罪行...


因为它只在那里声明了。
当我用-Wall -pedantic -ansi编译时。

为什么有可能覆盖sin的定义,<这是标准的一部分吗?


你还没有。
其次双罪(双重)的定义(不是声明)
错过了一个变量!
这样可以吗? ,当代码中没有引用变量时?


您显然已将此编译为C ++,其中省略了

未使用参数的名称是合法的。
在此先感谢,
Florian




HTH,

--g

-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot .com (新帖子12/5)
http:// www .cafepress.com / goldsays





theoderich写道:

以下是可能与gcc和g ++:

#include< math.h>

双罪(双)
{
返回1; }

int main()
{
sin(1);
返回1;
}

为什么我我没有得到任何警告:

在math.h中定义的罪行...

当我使用-Wall -pedantic -ansi进行编译时。


这种消息可能来自链接器,

不是来自编译器。当链接器看到你的

定义sin()已经满足

main()中的引用时,它不会尝试导入
$的另一个定义b $ b来自图书馆的sin()。 (顺便说一句,这只是

方式,许多实现工作;这不是一种行为

由C标准保证。)

为什么有可能覆盖罪的定义,
是标准的这一部分?


提供自己版本的库函数

会导致未定义的行为。编译器不需要

来发出诊断,但不需要程序

按照你的意图行事。

其次是定义(双重罪(双重)
错过一个变量!
这个没问题,当代码中没有引用变量时?




不,它不正确C并且编译器应该抱怨

。但是,我理解C ++允许这样做,所以一个b ++编译器不会抱怨。你确定你是使用C编译器而不是C ++编译器吗?


-
Er ********* @ sun.com


文章< 24 ************************** @ posting.google.com> ;,

theoderich < E0 ****** @ student.tuwien.ac.at>写道:

gcc和g ++可以使用以下内容:
#include< math.h>

双罪(双) {
返回1;
}
int main()
{
sin(1);
返回1;
}

为什么我没有收到任何警告:

在math.h中经常定义的罪...

当我编译与-Wall -pedantic -ansi。


因为它实际上并没有在< math.h>中定义,所以只声明了;标题

有[1]之类的东西:

--------

double sin(double);

--------

这意味着有一个名为sin的函数,需要一个双倍而

返回一个double,并且它被定义在其他地方。当编译器

看到你的定义时,它假定这是其他地方。

为什么可以覆盖罪的定义,
是这部分标准?


不,等等。

通过定义一个与标准库函数同名的函数,

你''重新调用未定义的行为,这意味着编译器不再受语言定义放在

上的要求的约束(因为你的代码不符合要求语言

定义放在_it_);它允许做任何实现者

希望它在这种情况下做(通常是无论什么'方便'

或不管这个其他什么覆盖这种情况的标准定义了,

但是像使恶魔飞出程序员的鼻子这样的事情并没有被禁止,只是非常难以实施),并且不需要

警告你。


我怀疑GCC实际上做的是没有连接数学库

根本没有抱怨无法找到它因为你给它

a定义你使用的唯一功能。我怀疑如果你告诉它链接数学库就会完成

让你的sin()覆盖

库中的那个并调用你定义的那个无论如何。细节

正是它正在做什么以及为什么超出了comp.lang.c的范围,

但是如果你真的想知道讨论的新闻组GCC,或者可能是讨论unix编程的
(我不确定这是GCC

的东西还是unix的东西),能够回答问题关于那个。


其次,双重罪的定义(不是声明)(双重)
错过一个变量!
如果没有引用变量,这是否正常在代码中?




在C ++中,是的。在C,没有。我怀疑你告诉GCC它是C ++,它是b $ b编译而不是C.(它通常根据文件的

扩展做出这个决定,除非你使用强制它的适当参数

否则 - 你调用的编译器可执行文件的名称不会影响它编译的语言。)

dave


[1]实际上,为了迂腐正确,这应该说#including

标题相当于插入具有的东西;;

标题不一定是实际包含任何内容的真实实体 -

它对编译器解释#include< math.h>完全有效。

表示将表7-12中的类型声明,函数声明和宏

定义复制到实时符号表中并且没有

标题存在于除了预先消化的符号表之外的其他任何东西。

但是如果你在我们喜欢做的迂腐的挑剔之间抓住我们,那么/>
我们大多数人都会承认几乎所有(所有?)真正的实现

实际上有一个文件,他们读取的地方而不是这样做。


-

Dave Vandervies dj******@csclub.uwaterloo.ca

事实上,你不理解它并不意味着它是错的,而且你理解它的事实并非如此。这意味着它是对的。

- 在comp.lang.c中的Joona I Palaste


Following is possible with gcc and g++:

#include <math.h>

double sin(double)
{
return 1;
}

int main()
{
sin(1);
return 1;
}

Why I don''t get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi.

Why is it possible to overwrite the definition of sin,
is this part of the standard?

Secondly the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code?

Thanks in advance,
Florian

解决方案

theoderich wrote:

Following is possible with gcc and g++:

#include <math.h>

double sin(double)
{
return 1;
}

int main()
{
sin(1);
return 1;
}

Why I don''t get any warnings like:

sin prevously defined in math.h ...
Because it''s only declared there.
when I compile with -Wall -pedantic -ansi.

Why is it possible to overwrite the definition of sin,
is this part of the standard?
You haven''t.
Secondly the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code?
You''ve obviously compiled this as C++, where leaving out the name of an
unused argument is legal.
Thanks in advance,
Florian



HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays




theoderich wrote:

Following is possible with gcc and g++:

#include <math.h>

double sin(double)
{
return 1;
}

int main()
{
sin(1);
return 1;
}

Why I don''t get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi.
That kind of message probably comes from the linker,
not from the compiler. When the linker sees that your
definition of sin() already satisfies the reference in
main(), it does not try to import another definition of
sin() from the library. (By the way, this is just "the
way many implementations work;" it is not a behavior
guaranteed by the C Standard.)
Why is it possible to overwrite the definition of sin,
is this part of the standard?
Providing your own version of a library function
causes undefined behavior. The compiler is not required
to issue a diagnostic, but the program is not required
to behave as you intend.
Secondly the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code?



No, it is not correct C and the compiler should have
complained. However, I understand C++ allows this so a
C++ compiler would not complain. Are you sure you are
using a C compiler and not a C++ compiler?

--
Er*********@sun.com


In article <24**************************@posting.google.com >,
theoderich <e0******@student.tuwien.ac.at> wrote:

Following is possible with gcc and g++:

#include <math.h>

double sin(double)
{
return 1;
}

int main()
{
sin(1);
return 1;
}

Why I don''t get any warnings like:

sin prevously defined in math.h ...

when I compile with -Wall -pedantic -ansi.
Because it isn''t actually defined in <math.h>, only declared; the header
has[1] something like:
--------
double sin(double);
--------
This means "There''s a function called sin, that takes a double and
returns a double, and it''s defined somewhere else". When the compiler
sees your definition it assumes that this is the "somewhere else".
Why is it possible to overwrite the definition of sin,
is this part of the standard?
No, sort of.
By defining a function with the same name as a standard library function,
you''re invoking undefined behavior, which means that the compiler is
no longer bound by the requirements the language definition places on
it (because your code doesn''t conform to the requirements the language
definition places on _it_); it''s allowed to do whatever the implementor
wants it to do in this case (usually either "whatever''s convenient"
or "whatever this other standard that does cover this case defines",
but things like "make demons fly out of the programmers nose" aren''t
forbidden, only extremely difficult to implement), and doesn''t need to
warn you about it.

What I suspect GCC is actually doing is not linking with the math library
at all and not whining about not being able to find it because you gave it
a definition of the only function you use. What I suspect it would have
done if you told it to link the math library is let your sin() override
the one in the library and call the one you defined anyways. The details
of exactly what it''s doing and why are beyond the scope of comp.lang.c,
but if you really want to know a newsgroup that discusses GCC, or possibly
one that discusses unix programming (I''m not sure whether this is a GCC
thing or a unix thing), would be able to answer questions about that.

Secondly the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code?



In C++, yes. In C, no. I suspect you told GCC that it was C++ that it
was compiling and not C. (It usually makes this decision based on the
extension of the file, unless you use appropriate arguments to force it
to do otherwise - the name of the compiler executable you invoke doesn''t
affect the language it compiles.)
dave

[1] Actually, to be pedantically correct, this should say "#including
the header is the equivalent of inserting something that has"; the
header need not be a real entity that actually contains anything -
it''s perfectly valid for the compiler to interpret "#include <math.h>"
to mean "Copy the type declarations, function declarations, and macro
definitions from table 7-12 into the live-symbols table" and not have
the header exist as anything other than a pre-digested symbol table.
But if you catch us in between the pedantic nitpicking we like to do,
most of us will admit that almost all (all?) real implementations
actually have a file somewhere that they read instead of doing this.

--
Dave Vandervies dj******@csclub.uwaterloo.ca
The fact that you don''t understand it doesn''t mean it''s wrong, and the
fact that you understand it doesn''t mean it''s right.
--Joona I Palaste in comp.lang.c


这篇关于c标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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