
Newbie - scanf and while loop - correction
Quote:
>: >
>: >: You can't use printf() without a prototype.
>: >
>: >You can use printf without a prototype because it returns an int, and
>: >because compilers still must support K&R C.
>: Please don't post nonsense. You can't use _any_ variadic function
>: without a prototype in scope. printf is no exception. Compilers are
>: NOT required to support K&R C code which is invoking undefined behaviour
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quote:
>: according to the ANSI C standard.
>: "But it works on any compiler I've used" is not a valid argument in this
>: newsgroup.
>I won't use that argument. In fact what I am about to say is not fact but
>my personal view on this.
>The prototype for printf in this case, without a prototype is
>int printf()
This should read: "The default declaration in this case, without a
prototype in scope, is
int printf();"
Not every declaration is a prototype.
Quote:
>That means that nothing should be assumed about the arguments to printf
>by the compiler.
Wrong. The compiler assumes that printf is a function with an unspecified,
but _constant_ number of arguments.
Quote:
>Now this should work unless you do something really
>boneheaded on a system where ints are 16 bit and longs are 32 bit and
>pointers are 32 bit.
This won't work on _any_ system where the compiler uses different argument
passing conventions for variadic functions vs ordinary functions. This is
precisely why variadic functions CANNOT be called without a prototype in
scope.
Quote:
>eg. printf("%p",0); could cause problems.
>or printf("%ld",10); could also cause problems, I agree on that.
Both of these could ALWAYS cause problems, no matter which headers are
included or what are the sizes of various types, because the types of
the arguments don't match the format strings. Which means UNDEFINED
BEHAVIOUR.
Quote:
>But scanf is variadic and should work unless you do not pass the address
>of a variable which is wrong no matter how you look at it.
Once you invoke undefined behaviour, the expression "should work" is plain
stupid. And using scanf without a prototype in scope invokes undefined
behaviour. What is so difficult to understand?
Quote:
>Of course I am not saying that you should not prototype or that you do
>not include the appropriate headers. Also you would use varargs.h for
>variadic functions in K&R C.
<varargs.h> is NOT a K&R C feature.
Quote:
>Handling varargs the way they are now was
>actually borrowed from C++. It was handled quite differently in K&R C.
It wasn't handled at all in K&R C. K&R1 explicitly states that there is
no mechanism for user-defined variadic functions. <varargs.h> is an
extension to K&R C, which was not universally supported.
Dan
--
Dan Pop
CERN, CN Division
Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland