Ways to define C constants

M

Malcolm McLean

What *is* the address or number of port A in your example, or doesn't it
mention it? Because your code is very confusing (the OP was talking about a
constant defining a port number).
0x74.

But that's set at one point in the program, in main. Then it is passed down. So at all places,
the address of port a is held in a variable, except the one place where it is set.
 
J

James Harris

Richard Bos said:
If they're stdint definitions, they may _not_ differ.

I suppose I am at fault for trying to summarise.... I should have known
better. Anyway, I was *not* referring to differences from the standards. We
discussed this topic about a year ago and I have some useful code as a
result. This is not the time to rehash that thread.

James
 
R

Richard Bos

Keith Thompson said:
In fact, if this hasn't changed in C11[*], you can even
[*] Which I don't have - is there a link anywhere?
The latest pre-C11 draft (which is very close to the published standard)
is N1570:

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

Thank you.

Is there any place which accurately documents the differences between
this and the official standard? And those between C11 and C99?
I could do a websearch, but if there's somewhere official I'd rather
rely on that.

Richard
 
K

Keith Thompson

Keith Thompson said:
In fact, if this hasn't changed in C11[*], you can even
[*] Which I don't have - is there a link anywhere?
The latest pre-C11 draft (which is very close to the published standard)
is N1570:

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

Thank you.

Is there any place which accurately documents the differences between
this and the official standard? And those between C11 and C99?
I could do a websearch, but if there's somewhere official I'd rather
rely on that.

The foreword of N1570 and C11 includes a summary of the major changes
between C99 and C11.

I don't know of an *official* summary of differences between N1570 and
C11, but I asked Larry Jones (a committee member) about it in comp.std.c
last year:

From: (e-mail address removed)
Newsgroups: comp.std.c
Subject: Re: Array operand of _Alignof
Date: Fri, 5 Apr 2013 14:07:23 -0400
Message-ID: <[email protected]>

Keith Thompson said:
So it was. I hadn't known there were any differences between N1570
and
the final standard.

There are a number of them, but most are just minor editorial tweaks,
changes to boilerplate text, and shuffling things around to keep the
powers that be happy. The biggest change was removing _Alignof from a
bunch of places it shouldn't have been added (based on the erroneous
notion that it takes either a type or an expression like sizeof does
when it really only takes a type): 6.3.2.1p2, p3, p4, fn. 65; and 6.7.1
fn. 121.
--
Larry Jones

There has also been one Technical Corrigendum. Both N1570
and C11 failed to define proper values for __STDC_VERSION__ and
__STDC_LIB_EXT1__; the TC specifies both as 201112L. (The latter is
defined only by implementations that support Annex K (Bounds-checking
interfaces).)
 
R

Richard Bos

Keith Thompson said:
The foreword of N1570 and C11 includes a summary of the major changes
between C99 and C11.

Hm? Oh, it doesn't open at the first page!

I see they've finally removed gets(). That's one good change to begin
with.
I don't know of an *official* summary of differences between N1570 and
C11, but I asked Larry Jones (a committee member) about it in comp.std.c
last year:

There are a number of them, but most are just minor editorial tweaks,
changes to boilerplate text, and shuffling things around to keep the
powers that be happy. The biggest change was removing _Alignof from a
bunch of places it shouldn't have been added (based on the erroneous
notion that it takes either a type or an expression like sizeof does
when it really only takes a type): 6.3.2.1p2, p3, p4, fn. 65; and 6.7.1
fn. 121.

There has also been one Technical Corrigendum. Both N1570
and C11 failed to define proper values for __STDC_VERSION__ and
__STDC_LIB_EXT1__; the TC specifies both as 201112L. (The latter is
defined only by implementations that support Annex K (Bounds-checking
interfaces).)

*grin*

Thanks again.

Richard
 
J

Jorgen Grahn

I'm a bit confused over how 'best' to define constants - especially small
integer constants - in C. There seem to be some options so I wondred if you
guys would recommend one over the others.

I'll use a 16-bit port number in each example.

That doesn't tell me anything, I'm afraid. In my world a "port" is an
UDP, TCP or SCTP port, and that's clearly not what you have in mind.
The ui16 in the last example
is the typedef of a 16-bit unsigned int.

First option, with #define.
/* Define address port */
#define PORT_A 0x70

Second option, using enum
enum {
port_a = 0x70 /* Address port */
};

Third option, using const
static const ui16 port_a = 0x0070; /* Address port */

Fourth option: isolate its use to one function and use the literal
there. Assuming it's a BSD socket port:

int my_specific_listening_socket(const char* host)
{
// ...
getaddrinfo(host, "112", ...);
// ...
return fd;
}

IME, a surprising number of these constants can be wrapped into
code: the constant itself isn't important but what it's used for.

It's easier to document and understand a function, than a constant.

/Jorgen
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top