一般问题 [英] General question

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

问题描述

小组,


在读完你的

问题比死鹦鹉不那么生动之后,我有点犹豫不决回答!,但是在这里。

(通过常见问题阅读,可能已经错过了这个,在这种情况下会很高兴

被正确引导。)


有人能让我理解为什么

..........


printf(" ; foobar)

........


没有指令#include< stdio>仍然打印,但是这样做(在

Xcode 2.2中)带有警告。很明显,我正在试图获得一个概述

C,并且显然缺少有关编译/链接等的内容。


谢谢

Hi Group,

I am a little hesitant to ask this, after having read the "your
question is less lively than a dead parrot answer!", but here goes.
(Read thru the Faq, may have missed this, in which case would be happy
to be steered correctly.)

Could someone give me an understanding why
..........

printf("foobar")
........

without the directive #include<stdio> still prints, but does so (in
Xcode 2.2) with a warning. Clearly, I am trying to gain an overview of
C, and am obviously missing something about compiling/linking etc.

Thanks

推荐答案

我意识到我应该说,而不是打印,输出到

控制台。

I realize I should have said, instead of "prints", outputs to the
console.


2006年3月26日18:44:50 -0800,drM < MD ** @ comcast.net>写在

comp.lang.c:
On 26 Mar 2006 18:44:50 -0800, "drM" <md**@comcast.net> wrote in
comp.lang.c:
嗨小组,

我有点犹豫要问这个,阅读你的
问题不如死鹦鹉的回答那么生动!,但是这里有。
(通过常见问题阅读,可能错过了这个,在这种情况下会很高兴正确驾驶。)


我不记得这是否在常见问题解答中,并且不能特别是打扰b $ b现在看,我们将讨论它。


有人能让我理解为什么

......... <没有指令#include< stdio>的$ f $ b .......
仍然打印,但是这样做(在Xcode 2.2中)并带有警告。很明显,我正在尝试概述
C,并且显然缺少有关编译/链接等的内容。


我完全不知道什么是Xcode 2.2 ;是的,但是不要费心来解释,这与你的问题并不特别相关。


让我们沿着记忆小道漫步并畅谈关于函数声明和

多年来随着语言的发展而在C中的原型。


第一个C标准是第一版The C Programming

Language作者:Kernighan& Ritchie,发表于1979年。没有

这样的功能原型,并且不需要声明

大多数功能。


如果编译器看到一个函数调用表达式,它没有看到

声明,它假定它是一个函数返回一个int

并且它接受了您调用它的参数。


如果函数返回的不是int,则需要

声明,例如:

double sqrt();


printf()函数确实返回一个int,它接受一个或多个
的参数,第一个必须是char的指针。在你的
代码中,你对printf()的调用有一个参数,一个字符串文字,

转换为指向char的指针,这就是传递给

printf()。



第一个真正的C标准,1989年和1990年的ANSI / ISO,改变了一些东西。

首先,原型被添加到语言中。一个函数

声明可以为编译器提供有关

函数的参数的信息,尽管除了
$ b $之外不需要这样做。 b某些特殊情况。


特殊情况是可变的函数,即带有可变数量参数的那些函数。当然,printf()是

这些函数之一。它的原型是:


int printf(const char * fmt_string,...);


...之后逗号表示一个或多个其他参数

未指定类型。


但是,如果一个函数有一个固定的参数列表,并返回一个int,

你不需要为它提供原型或声明。


1999年对C标准进行了重大更新,尽管不是太多/>
目前很多编译器都支持它。此版本的标准

禁止使用隐含整数。该语言的功能,并要求

所有函数至少都有一个声明范围,指定

他们的返回类型。


从1989年起,所有版本的ANSI / ISO C标准都要求

,当你调用一个可变参数

函数时,你有一个适当的原型。在没有原型范围的情况下调用这样的函数会导致

未定义的行为,这意味着语言标准不再是
指定应该发生什么。没有要求编译器确认和诊断未定义的行为。程序失败没有要求

。当你导致未定义的行为时,它可能会做你所期望的



谢谢
Hi Group,

I am a little hesitant to ask this, after having read the "your
question is less lively than a dead parrot answer!", but here goes.
(Read thru the Faq, may have missed this, in which case would be happy
to be steered correctly.)
I can''t remember whether this is in the FAQ or not, and can''t
particularly be bothered to look right now, so we''ll discuss it.

Could someone give me an understanding why
.........

printf("foobar")
.......

without the directive #include<stdio> still prints, but does so (in
Xcode 2.2) with a warning. Clearly, I am trying to gain an overview of
C, and am obviously missing something about compiling/linking etc.
I have absolutely no idea what "Xcode 2.2" is, but don''t bother to
explain, it is not particularly relevant to your question.

Let''s stroll down memory lane and talk about function declarations and
prototypes in C over the years as the language evolved.

The first C "standard" was the first edition of "The C Programming
Language" by Kernighan & Ritchie, published in 1979. There was no
such thing as a function prototype, and no need of declarations for
most functions.

If the compiler saw a function call expression that it had not seen a
declaration for, it assumed that it was a function returning an int
and it accepted whatever arguments you called it with.

If a function returned something other than an int it required a
declaration, such as:

double sqrt();

The printf() function does indeed return an int, and it accepts one or
more arguments, the first one must be a pointer to char. In your
code, your call to printf() has a single argument, a string literal,
which is converted to pointer to char and that is what is passed to
printf().

So in a "K&R" version of C, your code is perfectly correct.

The first true C standard, ANSI/ISO of 1989 and 1990, changed things.
First of all, prototypes were added to the language. A function
declaration could provide the compiler with information about the
function''s arguments, although it was not required to do so except in
certain special cases.

The special cases are "variadic" functions, that is those that take a
variable number of arguments. And, of course, printf() is one of
those functions. Its prototype is:

int printf(const char *fmt_string, ...);

The "..." after the comma indicates one or more additional arguments
of unspecified types.

Still, if a function had a fixed argument list, and returned an int,
you did not need to provide either a prototype or declaration for it.

There was a major update to the C standard in 1999, although not too
many compilers support it at this time. This version of the standard
banned the "implicit int" feature of the language, and required that
all functions have at least a declaration in scope that specified
their return type.

All versions of the ANSI/ISO C standard, from 1989 on, have required
that you have a proper prototype in scope when you call a variadic
function. Calling such a function without a prototype in scope causes
undefined behavior, which means that the language standard no longer
specifies what should happen. There is no requirement that a compiler
recognize and diagnose undefined behavior. There is no requirement
that the program fail. It might well do what you expected it to do
when you caused the undefined behavior.
Thanks




所以我们在这里有两种可能性。你要么使用

预标准编译器,要么就会发生未定义的行为

" work"如果你已经包含了

正确的标题,那么程序的工作方式也是一样的。


我知道几个实现,其中调用printf()而没有<范围内的
原型将导致程序挂起或崩溃。未定义

的行为就是这样。


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang.c http://c-faq.com/

comp.lang.c ++ http:/ /www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a ... FAQ-acllc.html



So we have two possibilities here. Either you are using a
pre-standard compiler, or the undefined behavior just happens to
"work" the same way the program would work if you had included the
proper header.

I know of several implementations where a call to printf() without a
prototype in scope will cause programs to hang or crash. Undefined
behavior is like that.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


drM写道:
嗨组,

我有点儿在读完你的问题不如死鹦鹉回答的问题之后!之后,犹豫不决地问这个问题,但是这里有了。
(通过常见问题阅读,可能已经错过了这个,其中案件会很开心正确驾驶。)

有人能让我理解为什么

..........

printf (foobar)

........
没有指令#include< stdio>


那是< stdio.h>


仍会打印,但是会在Xcode 2.2中显示警告。显然,我正在尝试概述
C,并且显然缺少有关编译/链接等的内容。
Hi Group,

I am a little hesitant to ask this, after having read the "your
question is less lively than a dead parrot answer!", but here goes.
(Read thru the Faq, may have missed this, in which case would be happy
to be steered correctly.)

Could someone give me an understanding why
..........

printf("foobar")
........

without the directive #include<stdio>
That''s <stdio.h>

still prints, but does so (in Xcode 2.2) with a warning. Clearly, I am trying to gain an overview of
C, and am obviously missing something about compiling/linking etc.



编译器将警告您该函数是隐式声明的

(没有原型),但链接器仍然可以解析

符号。


这可以如果你将错误的参数传递给

未知函数,会产生灾难性的结果....


-

Ian Collins。


The compiler will warn you that the function is implicitly declared
(there isn''t a prototype), but the linker is still able to resolve the
symbol.

This can have disastrous results if you pass the wrong parameters to an
unknown function....

--
Ian Collins.


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

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