Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Scopes of identifiers Date: Thu, 11 Apr 2019 14:00:00 -0700 Organization: None to speak of Lines: 65 Message-ID: References: <098da5fe-31d3-47b7-b695-d4faea10b366@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="87362217ce2e2b0a1d2135c8fdf71e43"; logging-data="12002"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NVNHpKfW5ePAhsEg40ke6" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:0PH+YFG0zsx9tNOQQxyrGLSNWbI= sha1:JDFIE4kuo+ni6RIhQHFpVZELhOs= Xref: csiph.com comp.lang.c:143035 Andrey Tarasevich writes: > On 4/10/2019 8:58 PM, Keith Thompson wrote: >> For example, an old "hello, world" program might look something like >> this: >> >> main() { >> int printf(); >> printf("hello, world\n"); >> } >> >> but in modern C you'd just #include . (Actually in ancient C >> you probably wouldn't have bothered to declare printf, since it returns >> an int and the call would be resolved correctly.) > > This is not entirely accurate. A variadic function (like `printf`) has > to be declared *with prototype*. Calling a variadic function without a > declaration or with a non-prototype declaration results in undefined > behavior. This requirement is present in the language since C89. > > The correct version would be > > int main() > { > int printf(const char *, ...); > printf("hello, world\n"); > } Of course I'm aware of that (and I should have mentioned it). It was meant to be an example of a pre-ANSI C program, written when the language didn't have prototypes or explicitly variadic functions. In modern C, you could write: int main(void) { int printf(const char *restrict format, ...); printf("hello, world\n"); } but it's a lot easier just to write: #include int main(void) { printf("hello, world\n"); } (and not have to worry about whether "restrict" is supported and/or needed). But in pre-ANSI C, this: printf(); or this: int printf(); would have been the only available way to declare printf. As someone pointed out, a floating-point function might have made for a better example: /* This is pre-ANSI */ main() { double sqrt(); printf("%f\n", sqrt(2.0)); } -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */