Home > Archive > C > April 2004 > declaring errno
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Is it legal to declare errno after you've included errno.h?
For example:
#include<errno.h>
....
int main (void)
{
extern int errno;
....
}
I see in the standard that errno may be a macro, and I see that defining
errno is illegal, but I don't think the declaration above counts as a
definition since it doesn't reserve storage. On the other hand, if errno
IS a macro, the declaration above could easily be a syntax error after
pre-processing.
Please enlighten me.
--Mac
| |
| P.J. Plauger 2004-04-22, 9:30 am |
| "Mac" <foo@bar.net> wrote in message
news:pan.2004.04.22.04.06.05.969827@bar.net...
> Is it legal to declare errno after you've included errno.h?
>
> For example:
>
> #include<errno.h>
>
> ...
>
> int main (void)
> {
> extern int errno;
>
> ...
>
> }
>
> I see in the standard that errno may be a macro, and I see that defining
> errno is illegal, but I don't think the declaration above counts as a
> definition since it doesn't reserve storage. On the other hand, if errno
> IS a macro, the declaration above could easily be a syntax error after
> pre-processing.
>
> Please enlighten me.
You've already enlightened yourself. The declaration is indeed unsafe.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
| |
| Dan Pop 2004-04-22, 9:30 am |
| In <pan.2004.04.22.04.06.05.969827@bar.net> "Mac" <foo@bar.net> writes:
>Is it legal to declare errno after you've included errno.h?
>
>For example:
>
>#include<errno.h>
>
>...
>
>int main (void)
>{
> extern int errno;
>
>...
>
>}
>
>I see in the standard that errno may be a macro, and I see that defining
>errno is illegal, but I don't think the declaration above counts as a
>definition since it doesn't reserve storage. On the other hand, if errno
>IS a macro, the declaration above could easily be a syntax error after
>pre-processing.
>
>Please enlighten me.
Your analysis is correct. You cannot even declare errno, because it can
be (and quite often is) defined as a macro in <errno.h>. The common
reason for this is allowing multithreaded applications to have a per
thread errno, rather than sharing a global errno. This is one of the
few places where the C standard cares about multithreading.
The common macro definition for errno is along the lines:
#define errno (*__errno())
So, if you need to access errno, include <errno.h> and use whatever
definition/declaration it provides.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
| |
| Dave Thompson 2004-04-27, 12:12 am |
| On Wed, 21 Apr 2004 21:06:06 -0700, "Mac" <foo@bar.net> wrote:
> Is it legal to declare errno after you've included errno.h?
<snip>
> I see in the standard that errno may be a macro, and I see that defining
> errno is illegal, but I don't think the declaration above counts as a
> definition since it doesn't reserve storage. On the other hand, if errno
> IS a macro, the declaration above could easily be a syntax error after
> pre-processing.
>
7.1.3p1 list items 3 and 5:
Each macro name in any of the following subclauses (including the
future library
directions) is reserved for use as specified if any of its associated
headers is included;
unless explicitly stated otherwise (see 7.1.4).
Each identifier with file scope listed in any of the following
subclauses (including the
future library directions) is reserved for use as a macro name and as
an identifier with
file scope in the same name space if any of its associated headers is
included.
errno is either a macro or an identifier with external linkage, per
7.5p2.
7.1.3p2 <snip> If the program declares or defines an identifier in a
context in which it is reserved (other than as allowed by 7.1.4), or
defines a reserved
identifier as a macro name, the behavior is undefined.
and none of the exceptions in 7.1.4 covers errno, so declaring it,
even without defining, is UB; for exactly the reasons others have
answered and you have discovered.
- David.Thompson1 at worldnet.att.net
|
|
|
|
|