473,734 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

declaring errno

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

Nov 14 '05 #1
3 2435
"Mac" <fo*@bar.net> wrote in message
news:pa******** *************** *****@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
Nov 14 '05 #2
In <pa************ *************** *@bar.net> "Mac" <fo*@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: Da*****@ifh.de
Nov 14 '05 #3
On Wed, 21 Apr 2004 21:06:06 -0700, "Mac" <fo*@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.ne t
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1555
by: Richard Tobin | last post by:
In a library I am writing, I want to use an errno-like mechanism for error returns. The error would probably be represented as a struct rather than just an integer. I don't have any multi-threaded programs, but others may who want to use my library. Most likely they'll be using a pthreads-compatible threads system. How can I best accommodate them? Presumably they will want the error object to be in per-thread storage, so to set and...
4
9015
by: Paul Emmons | last post by:
If I am writing a function that sets errno according to a possible error condition, should it also set errno to 0 if it executes normally, or should it leave errno alone on success?
11
2528
by: Vijay Kumar R Zanvar | last post by:
> 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> > > > >... > >
18
3432
by: pete | last post by:
On my system, the following five expressions are true: (HUGE_VAL == HUGE_VAL / 2) (1 / HUGE_VAL == 0) (sqrt(HUGE_VAL) == HUGE_VAL) (sqrt(-1) == HUGE_VAL) (sqrt(-HUGE_VAL) == HUGE_VAL) and
5
2364
by: Urs Beeli | last post by:
I have a question regarding errno. If I understand it correctly, including <errno.h> allows me to check "errno" for error values that some standard library functions may set. This brings up some questions: - As I understand it, most functions only set errno in an error case. I take this to mean, that on success, errno is not necessarily set to EOK and I would either need to set errno to EOK before calling the function in question to...
3
3390
by: eyalc1978 | last post by:
Hi Does someone knows what does errno 72 means I got this error when fwriting a structure and still the file is being created. I saw something on the net saying that this is caused when trying to access different file system
24
1888
by: Daniel Rudy | last post by:
Hello Group, Consider the following code: /* form 1 */ int main(void) { int i; char *p;
13
1937
by: Spiros Bousbouras | last post by:
Assume I'm writing a function which is going to set the value of errno if something went wrong but I also want to guarantee that errno will remain unchanged if the function completed its task succesfully. So at the beginning of my code I have something like int errsto = errno ; and just before every successful return I have errno = errsto ; Is this ok ?
22
2499
by: viza | last post by:
Hi all, A quick one - since errno is a lvalue, can I do: fread( & errno, sizeof errno, 1, fp ) ? TIA viza
0
8776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.