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 14 1350
I realize I should have said, instead of "prints", outputs to the
console.
On 26 Mar 2006 18:44:50 -0800, "drM" <md**@comcast.net> wrote in
comp.lang.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
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 wrote: 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.
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.
I just wish to thank both for your thoughful answers.
drM wrote: 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.
The warning is because using printf() without a prototype makes it have
an implicit declaration (before C99) with a fixed number of arguments
and a return type of int. Clearly, printf takes a variable number of
arguments and its declaration, provided by a prototype in <dtdio.h>,
must end in an ellipsis (, ...).
Many compilers "know" about printf. This allows them, for example, to
issue diagnostics for incorrect specifiers. But knowing about printf
also means that they know what the prototype looks like, and so they can
produce what one might call working executables. Other implementations
may just pass any arguments with the default conversions and rely on the
called function to sort them out.
It is a bad practice to rely on any such behavior, and with a C99
compiler not an allowed one. I think you knew that already, though.
Even though I have given an explanation of why your code might be
working, the real position to taken in comp.lang.c is that there is *no*
explanation of why incorrent code "works" other than plain (bad) luck.
On 2006-03-27, Martin Ambuhl <ma*****@earthlink.net> wrote: drM wrote:
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.
The warning is because using printf() without a prototype makes it have an implicit declaration (before C99) with a fixed number of arguments
Not quite. It's actually a declaration with no particular number of
arguments, such that it can be called with any arguments with no
required diagnostic.
Jordan Abel wrote On 03/27/06 10:45,: On 2006-03-27, Martin Ambuhl <ma*****@earthlink.net> wrote:
drM wrote:
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.
The warning is because using printf() without a prototype makes it have an implicit declaration (before C99) with a fixed number of arguments
Not quite. It's actually a declaration with no particular number of arguments, such that it can be called with any arguments with no required diagnostic.
Martin's right: The implicit declaration is for a
function taking a fixed number of arguments. The value
of the fixed number is unknown, but the number is fixed
nonetheless. That is, the implicit declaration is never
of a variadic function, hence an implicit declaration of
printf() is always wrong.
Jordan's right: No diagnostic is required if a call
provides the wrong number of arguments, or even if different
calls provided different numbers of arguments. Nor is a
diagnostic required if the arguments' promoted types fail
to match those of the function's formal parameters.
"And I am right, and you are right, and everything
is quite correct." -- Pish-Tush
-- Er*********@sun.com
Jordan Abel wrote: On 2006-03-27, Martin Ambuhl <ma*****@earthlink.net> wrote: drM wrote:
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. The warning is because using printf() without a prototype makes it have an implicit declaration (before C99) with a fixed number of arguments
Not quite. It's actually a declaration with no particular number of arguments, such that it can be called with any arguments with no required diagnostic.
As well as not requiring a diagnostic, it is also not required to work
if it is a varidac function or if the number and type (after promotion)
of parameters is not correct.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc
Jordan Abel wrote: On 2006-03-27, Martin Ambuhl <ma*****@earthlink.net> wrote:
drM wrote:
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.
The warning is because using printf() without a prototype makes it have an implicit declaration (before C99) with a fixed number of arguments
Not quite. It's actually a declaration with no particular number of arguments, such that it can be called with any arguments with no required diagnostic.
I did not claim it was a declaration with a particular number of
arguments. I claimed that it was a declaration with a fixed number of
arguments. Just as it can be called "with no required diagnostics", it
call be called with no reason to expect it to work.
On 2006-03-27, Eric Sosman <Er*********@sun.com> wrote: Jordan's right: No diagnostic is required if a call provides the wrong number of arguments, or even if different calls provided different numbers of arguments. Nor is a diagnostic required if the arguments' promoted types fail to match those of the function's formal parameters.
And of course, in the older [K&R] dialects of C that this convention
comes from, there were several functions that were actually well-defined
on different numbers or types of parameters. printf is the obvious one
one. also open() and, i believe, signal handlers.
On 2006-03-27, Flash Gordon <sp**@flash-gordon.me.uk> wrote: Jordan Abel wrote: On 2006-03-27, Martin Ambuhl <ma*****@earthlink.net> wrote: drM wrote:
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. The warning is because using printf() without a prototype makes it have an implicit declaration (before C99) with a fixed number of arguments
Not quite. It's actually a declaration with no particular number of arguments, such that it can be called with any arguments with no required diagnostic.
As well as not requiring a diagnostic, it is also not required to work if it is a varidac function or if the number and type (after promotion) of parameters is not correct.
I didn't say that it was _right_, just that it didn't require
a diagnostic. Which is in some ways worse.
On 2006-03-27, Martin Ambuhl <ma*****@earthlink.net> wrote: Jordan Abel wrote: On 2006-03-27, Martin Ambuhl <ma*****@earthlink.net> wrote:
drM wrote:
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.
The warning is because using printf() without a prototype makes it have an implicit declaration (before C99) with a fixed number of arguments
Not quite. It's actually a declaration with no particular number of arguments, such that it can be called with any arguments with no required diagnostic.
I did not claim it was a declaration with a particular number of arguments. I claimed that it was a declaration with a fixed number of arguments.
But it's not. It is a declaration with empty parentheses, and you can
subsequently call the function with a different number of arguments,
almost certainly invoking undefined behavior.
Just as it can be called "with no required diagnostics", it call be called with no reason to expect it to work.
Jack Klein wrote: On 26 Mar 2006 18:44:50 -0800, "drM" <md**@comcast.net> wrote in comp.lang.c:
[...] 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.
Xcode is the Mac OS X C/C++/Objective-C/etc./etc. development system.
Essentially a hacked GCC.
On Sun, 26 Mar 2006 22:03:21 -0600, Jack Klein <ja*******@spamcop.net>
wrote: On 26 Mar 2006 18:44:50 -0800, "drM" <md**@comcast.net> wrote in comp.lang.c:
(calling printf without inlcuding stdio.h) 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.
After promotion(s) of small integers to int and float to double.
If a function returned something other than an int it required a declaration, such as: <snip> So in a "K&R" version of C, your code is perfectly correct.
Right.
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 <snip> 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.
A prototype is also required when calling a function defined with one
containing narrow type parameters. If defined K&R1-style parameter
passing uses the promoted type(s) and it may be called with either a
K&R1 declaration or none, or a prototype using promoted types.
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.
Although quite a few C89 compilers implement(ed) at least an optional
warning for implicit function declaration. (I don't even say as an
extension, since warning for anything was already permitted.)
<snip rest>
- David.Thompson1 at worldnet.att.net This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Salerno |
last post by:
After my last post, I thought of another question as a result of the
following:
------------------------------
Mike Meyer wrote:
> John Salerno <johnjsal@NOSPAMgmail.com> writes:
>
>
>>So...
|
by: JezB |
last post by:
What's the generally accepted approach for using Styles and Stylesheets in a
web application based on .aspx files, Web Controls, User Controls, and
code-behind modules (c# in my case)? Most style...
|
by: ZorpiedoMan |
last post by:
I'm new to the world of sockets, and this question is not VB specific:
If multiple clients access the same server on the same port, and the server
is set up to do some async communication, does...
|
by: Christoph Zwerschke |
last post by:
Sometimes I find myself stumbling over Python issues which have to do
with what I perceive as a lack of orthogonality.
For instance, I just wanted to use the index() method on a tuple which
does...
|
by: Mark Fink |
last post by:
Hi there,
unfortunately I am new to Jython and my "Jython Essentials" book is
still in the mail. I looked into the Jython API Doc but could not find
the information.
I am porting a Python...
|
by: Yong |
last post by:
I'm not getting any reply to my previous thread so I'm stating a new one.
I get a General Network Error due to my SqlCommand object not having a big
enough CommandTimeout to complete very long...
|
by: Viviana Vc |
last post by:
Hi all,
I've read the WindowsVistaUACDevReqs.doc documentation and I have done
different small tests on Vista to understand the bahaviour and now I
have a few questions.
1) If I create a...
|
by: sharan |
last post by:
Hello Friends I have a problem in Data Structure (In C) i want to
emplement a General tree having three child nodes of each parent
node ...please send me any simple Example( code) by which i can...
|
by: SpotNet |
last post by:
Scott M. Have your experiences shown you that programmers may give the best
computer advice, analysis, and solutions even at the worst of times, for a
PC user? ;~D
Haven't crossed many Network...
|
by: =?Utf-8?B?Ymxi?= |
last post by:
I am posting to the general discussion group - but I cannot find my
postings... or replies...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |