declaring errno

M

Mac

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
 
D

Dan Pop

In said:
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
 
P

P.J. Plauger

Mac said:
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
 
D

Dave Thompson

Is it legal to declare errno after you've included errno.h?
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top