Converting unsigned long to string in C

B

Bartc

santosh said:
Bartc wrote:
Maybe the standard should have gone a bit further and defined
lengthof() to return the bound of an array. Using sizeof for this is
rather clumsy.

Could have. But it's easy enough to compute with sizeof.

int arr[4];

size_t size_of_arr = sizeof arr / sizeof arr[0];

Clumsy like I said -- sizeof appears twice, you also have the array name
twice (imagine a much longer name, especially one only slightly different
from another), plus you have "/" and "[0]" thrown in for good measure. A
recipe for errors.

You don't need more than - the array name, and the operator.
Can you propose a better way?

That's a dangerous thing to ask an amateur language designer!
 
S

santosh

Bartc said:
santosh said:
Bartc wrote:
Maybe the standard should have gone a bit further and defined
lengthof() to return the bound of an array. Using sizeof for this is
rather clumsy.

Could have. But it's easy enough to compute with sizeof.

int arr[4];

size_t size_of_arr = sizeof arr / sizeof arr[0];

Clumsy like I said -- sizeof appears twice, you also have the array
name twice (imagine a much longer name, especially one only slightly
different from another), plus you have "/" and "[0]" thrown in for
good measure. A recipe for errors.

You don't need more than - the array name, and the operator.

Another good solution (keeping in mind that the context is C) is to
simply make sure that you store the array's length (which you'll have
to supply during it's declaration) somewhere convenient.

#define MY_ARR_SIZE SOME_VALUE
/* ... */
const size_t my_arr_length = MY_ARR_SIZE;
int my_arr[MY_ARR_SIZE];

Of course, you could dispense with the size_t variable and use
MY_ARR_SIZE too, though type safety is lost. Unfortunately C doesn't
permit static array size to be specified by const qualified variables,
since they cannot be assumed to be present at compile time (Except for
VLAs, which is a different matter).

C probably isn't a good choice if you want a feature-rich or elegant
language (for some definitions of elegant and feature-rich).
That's a dangerous thing to ask an amateur language designer!

The purpose, I believe, was to introduce these features without breaking
existing code that might legitimately be using identifiers
like "and", "bitor" etc. That was probably the chief reason why they
are added as macros and furthermore, only defined in <iso646.h>, a
header that only programs that needed it's facilities would include.
Making them keywords would have (I suppose) broken existing code.

There are more complicated alternatives one can think off, but this was
probably the simplest acceptable solution that the committee could come
up with, given their stated mandate to be as "silent" as possible with
changes to the language.
 
R

Richard

William Pursell said:
I think Richard is accurate about the percentage usage
of "sizeof x". Most programmers don't use it. Most
don't know it is valid syntax. Most production code
doesn't use it. Unfortunately it is also the case
that most programmers are ignorant and have no sense
of aesthetics. Richard obviously has a strong aesthetic
sense, which in this case is in disagreement with the
sensibility of many others in this thread. When I
first saw "sizeof x", I was annoyed that I hadn't seen
it long before, and thought it was amazing that I had
not. How did such an aesthetically appealing usage
avoid my eyes for so long? Because I was reading code
written by incompetent boobs with no aesthetic sense
who were probably following some absurd coding standard
written by persons who may or may not have
known that sizeof could be applied to an object and
mandated that it only be used in the form "sizeof(T)".
(Well, really, it's probably because I wasn't coding
very much at all, and what code I was reading was
obfuscated Perl, but that's a different story which
is clearly OT. Here on CLC, we must pretend that
C is the only language.)

In short, it is not because sizeof is an operator
that I use "sizeof x". It is because I find it
more aesthetically appealing. Richard's sense of
aesthetics is different.

How do you find it more pleasing? In what way? Are you sure its not
becauase you know it to be an operator?

Would you like to see

printf "hello%s" "world";

or something equally as contrived for example?
 
R

Richard

Keith Thompson said:
Richard said:
Richard wrote:
it makes no sense to me whatsoever and I dont give a monkeys uncle about
whats in the standard
[...]
I'm not missing your point; I'm simply disagreeing with you.

You find "sizeof n" ugly and difficult to read. That's fine;
I don't expect, or even particularly want, you to change your mind.

I find the "sizeof n" form cleaner and easier to read than the
"sizeof(n)" form. For me, *because* "sizeof" is an operator,

Exactly. But you do NOT have to think of it as one.

So what? I do think of "sizeof" as an operator. You're saying
there's something wrong with that. That's absurd.

[...]
I DO care about whats in the standard.

Glad to hear it. Note that that directly contradicts what you wrote
upthread.

Only if you want to be an anal retentive fool. I was referring before to
it being called an "operator" in the standard. This does not dictate how
I USE it.
 
R

Richard

Ben Bacarisse said:
I find it amazing that you can't believe this. I can see how you may
have moved in a programming circle where this looks odd and so, if you
ask, the people around you agree. But how can you make the leap to
not believing it of anyone? y + sizeof z + a has only one plausible
meaning in C and only one plausible meaning even if you don't know C.

To anyone exposed to languages with many word-like operators, or to
those where juxtaposition implies a tight binding, it will be
obvious. For it to have any meaning but the right one, the syntax
would have to be absurd.

For starters theres the

"is it sizeof(x)+y" or "sizeof(x+y)" double take. For seconds, well, I
give up.

I do not believe you don't get my point here. sizeof is a one off in C as
far as I can see. And you reckoning this "one off" is easy and as clear as
"sizeof(y)"?

I cant see it myself.

My experience is that almost nowhere other than here have I seen "sizeof
x + y" used in preference to "sizeof(x)+y";
 
R

Richard

santosh said:
Ben said:
Richard said:
I simply do not believe that in the context of other C code that this

x= y + sizeof z + a;

is easier to read than:

x= y + sizeof(z) + a;

for anyone.

I find it amazing that you can't believe this. I can see how you may
have moved in a programming circle where this looks odd and so, if you
ask, the people around you agree. But how can you make the leap to
not believing it of anyone? [...]

Richard has unfortunately gradually developed a "Spinoza" like complex,
wherein he is compelled to suspect as posturing anything that a poster
whom he considers as a part of the hypothetical "clique" says.

No he hasn't. He knows that the clique are very proud of their ability
to use standards C and the ability to use something as fundamentally
"silly" as this alternative syntax is, frankly, rather amusing. I would
hazard a guess that his alternative usage is in less than 1% of code
worldwide. Only in CLC is it "equally as clear".
 
B

Ben Bacarisse

Richard said:
For starters theres the

"is it sizeof(x)+y" or "sizeof(x+y)" double take. For seconds, well, I
give up.

To *you*? Don't you see. I've spent so many years using languages
that have word-like unary "operators" there is no double take. They
all bind very tightly. The first was SASL in 1978. It looks entirely
normal to me. Please try to accept that. I don't want to persuade
you to do anything you don't like, but I can't see how you can access
my internal parser.
I do not believe you don't get my point here.

I get it. My problem is that you don't believe me.
sizeof is a one off in C as
far as I can see. And you reckoning this "one off" is easy and as clear as
"sizeof(y)"?

Yes, it is a one-off, but only in C. It one of the parts of C that
looks quite natural -- to me. If C insisted on 'sizeof(expression)',
I'd go with that but it would strike me as odd that this built-in
operator required function syntax. The 'sizeof(type-name)' I can
accept since it is, in effect:

sizeof <applied to> (some cast)

The brackets are there because of the ambiguous syntax of type names,
not because of a function-like call.
My experience is that almost nowhere other than here have I seen "sizeof
x + y" used in preference to "sizeof(x)+y";

I don't have much code here to scan, but my Linux kernel source has
more than 1200 instances of this form of sizeof. Much less than the
other form, but my point in not about the frequency of use, but about
your insistence than anyone who thinks differently about is being silly
(that is from another post in this thread).
 
D

Doug Miller

For starters theres the

"is it sizeof(x)+y" or "sizeof(x+y)" double take.

Don't be ridiculous; the meaning is just as obvious as in
x = y * a + b;

Or do you look at that statement and have trouble deciphering whether the
right side means ((y * a) + b) or (y * (a + b)) ?
 
R

Richard Tobin

Richard said:
I would
hazard a guess that his alternative usage is in less than 1% of code
worldwide. Only in CLC is it "equally as clear".

Not as little as that, but the Google n-gram corpus shows that
2,703,650 of 2,871,816 occurrences of sizeof are followed by an open
parenthesis. Of course we don't know how many of them are followed
by types.

-- Richard
 
C

CBFalconer

Richard said:
CBFalconer said:
And they would have been thoroughly criticized. '$' is not
available on many keyboards. Think UK, France, Germany, for
example. It is not a portable character.

$ is in the ASCII character set (code point 36) and the EBCDIC
character set (code point 91). It is present on French keyboards
(between ^ and *), Hungarian keyboards (sharing a key with
e-acute), Polish keyboards (sharing a key with that funny
w-sounded L with the line through it) and UK, German, Croatian,
Slovak, Czech, Italian, Spanish, Latin American, Portuguese, and
Japanese keyboards (shift-4). It is rather more portable than,
say, the two square bracket characters [], which caused me one or
two (minor) portability problems when I was working on mainframes.

I believe you. However, I can definitely remember seeing a
definition where that character was called 'local currency symbol',
or similar. Maybe that preceded general tightening of the ASCII
spec. And you can't deny that many keyboards are missing it.
Especially typewriters (remember them?).
 
C

CBFalconer

Eric said:
Richard said:
Keith Thompson said:
[...] *because* "sizeof" is an operator,

Exactly. But you do NOT have to think of it as one.

Right. And Carbon is a chemical element, but you do NOT have to
think of it as one. And 17 is a prime number, but you do NOT
have to think of it as one. And Richard is a bleating troll, but
you do NOT have to think of it as one.

Unfortunately it periodically nymshifts (without reforming) and
temporarily escapes the PLONK mechanism.
 
C

CBFalconer

santosh said:
I believe it was to alleviate problems for programmers working
with non-standard keyboards where some of these symbols were
hard to generate.


Yes. If you don't include <iso646.h> you can use these
identifiers.

I spent a period a few years ago of including iso646.h and using
those, for the added clarity. They do clear some things up for
novice readers, especially the doubled && and ||, and the confusing
xor ^.
 
M

Micah Cowan

Richard said:
Not as little as that, but the Google n-gram corpus shows that
2,703,650 of 2,871,816 occurrences of sizeof are followed by an open
parenthesis. Of course we don't know how many of them are followed
by types.

It'd be interesting, for comparison, to determine how many occurrences
of malloc() do not get their values checked.

Of course, either way, ad populum is a ridiculous form of argument to
make regarding personal aesthetics. But then, so is more-or-less any
form of argument.
 
K

Keith Thompson

Keith Thompson said:
If K&R had chosen to use, say, "$" rather than "sizeof" as the symbol
for this operator, we wouldn't be having this discussion.

Just for the record, this was not a serious suggestion. (It probably
would have caused problems on VMS, for one thing.)

On general, though, either having *all* operators represented by
puncutation symbols, or having several operators represented by
keywords, would avoid a lot of confusion. Having just one exceptional
operator represented by a keyword can, as we've seen, be problematic
for some. I have no problem with it myself, but I can understand that
others do.
 
M

Michael Mair

Richard wrote:
I simply do not believe that in the context of other C code that this

x= y + sizeof z + a;

is easier to read than:

x= y + sizeof(z) + a;

for anyone.

This may be the heart of the problem.
When other people say "It is as easy / easier to read for me", you
just insist that they are wrong or lying.

If we had
x = y + * z + a;
and
x = y + *(z) + a;
I'd probably go for the former (but prefer
x = y + *z + a;
due to my internal parser) and would add parentheses (if pressed)
around the unary operator and its operand,
x = y + (*z) + a;
Just the same holds _in_ _my_ _case_ for sizeof.

This is just a matter of being used to it -- I basically learned
of "sizeof z" quite some time before I ever had the need to use
sizeof (T).

sizeof is a "special" thing IMO and therefore should the "like a
function" usage is much better so its not so special anymore.

"It is like neither"?
I have no preference either way but I am not sure whether having
a function-like compile-time (or C99: mostly ha^H compile-time)
operator really is better than an operator working on expressions
and "casts".


Cheers
Michael
 
M

Micah Cowan

Bartc said:
I thought this was some sort of joke, but yes, these are actually in my C99
document as macros.

But, why? Every C programmer knows and loves && and friends, why did the
standard have a sudden urge to be sensible and turn C into something it
isn't?

Once C had made the decision that they wanted to support variants of the
ISO-646 character set, they pretty much had to stick with it.

ISO-646 specifies a set of 7-bit character encodings which share most,
but not all, of their character set. US-ASCII (which in this context is
known as ISO-646-IRV) is one of them. All the other national variants of
ISO-646 must contain the same set of characters as ASCII, except that
0x23 could be either # or £, 0x24 could be either $ or ¤, and any of the
following characters:

@ [ \ ] ^ ` { | } ~

could be replaced by arbitrary other characters. For instance, the
Spanish-language variant replaces these with

§ ¡ Ñ ¿ ^ ` ° ñ ç ~

(pologies to those using readers lacking Unicode support) and opts to
use £ instead of #. EBCDIC, of course, lacked several of these
characters as well (though, I believe that it may also have lacked !,
which begs the question as to why that character was not given a
trigraph equivalent, and in fact is relied upon for the trigraph
equivalent of |).

ISO-646 variants were in fairly wide use in the days that limiting
character encodings to 7 bits was important. The above character
replacements obviously make a great deal of sense if you're typing in
Spanish (and note that you still can't get characters like é without a
terminal that supports composition via backspacing and typing a ' over
your e).

Of course, if you're trying to code C on a system that uses this
variant, it's a pain in the butt to code even small programs like:

#include <stdlib.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
int res1=0, res2;
if (argv[ 0 ] != NULL)
res1 = printf("%s: ", argv[ 0 ]);
res2 = printf("Hello, world!\n");
return (res1 < 0 || res2 < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
}

That's the reason trigraphs were invented: to allow people to use the
set of characters that can be counted upon to exist virtually
everywhere, to make up for the lack of characters that C really needs.
It's not pretty, but thanks to trigraphs in the original C Standard, our
Spanish programmer can at least write the program:

??=include <stdlib.h>
??=include <stdio.h>
int
main(int argc, char *argv??(??))
??<
int res1=0, res2;
if (argv??( 0 ??) != NULL)
res1 = printf("%s: ", argv??( 0 ??));
res2 = printf("Hello, world!??/n");
return (res1 < 0 ??!??! res2 < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
??>

(There's a good chance you need to explicitly activate trigraph support
on your implementation, say via a "-trigraphs" switch, to compile the
above. The above example was obviously contrived to use as many of the
variant charactrs as possible: at the very least, it could have been
written without any use of ??( or ??) ([ or ]).)

Great, so now our Spanish coder can write code. It's still butt-ugly,
though, and about as cumbersome to type as it is to read.

With C95, however, digraphs and the iso646.h header were added, which
drastically improved the readability of such programs:

%:include <iso646.h>
%:include <stdlib.h>
%:include <stdio.h>
int
main(int argc, char *argv<::>)
<%
int res1=0, res2;
if (argv<: 0 :> != NULL)
res1 = printf("%s: ", argv<: 0 :>);
res2 = printf("Hello, world!??/n");
return (res1 < 0 or res2 < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
%>

Note that the only place where we still required an ugly trigraph, was
to represent the backslash character for "\n" in "Hello, world\n".

C++ also has all the words that are #defined (%:defined? :D) in C's
And macros in lower-case too! (I suppose some attempt to avoid
user-namespace pollution.)

Namespace pollution is no issue, as anyone #including <iso646.h> is
writing new code (or at least adapting old code), and knows to avoid them.

Note that these are far from the only examples of lower-case macros
within the C standard.
I think though eyebrows would be raised if anyone actually posted code with
these 'operators' in. Especially mixed with the originals.

Mixed with the originals, yeah. And I think it's fair to say that
virtually no one is using these variants today; not when the friendlier
8-bit ISO-8859-* encodings are so pervasive. Japan still uses theirs, I
believe (which merely replaces \ with ¥), but not to the exclusion of
ASCII. It probably shows up mainly in the ISO-2022-JP encoding.
But not sure about the _eq versions: 'and=', 'or=' and so on would have been
adequate.

Well, no (and it would be bitor=, not or=). Consider the fact that ||,
|, ||= and |= are all distinct and complete tokens. A lexical scanner
that would translate | into a BITOR token, = into an ASSIGN token, and
|= into a BITOR_EQ token, would translate "bitor=" as BITOR ASSIGN,
because macro expansion happens after tokenization.
 
R

Richard Harter

Don't be ridiculous; the meaning is just as obvious as in
x = y * a + b;

Or do you look at that statement and have trouble deciphering whether the
right side means ((y * a) + b) or (y * (a + b)) ?
I dunno, I would expect that for most people it is not "just as
obvious". That * binds more closely than + is customary usage in
basic algebra. C (and most other computer languages) uses the
same precedence rules as ordinary usage. "sizeof" is an operator
peculiar to C; its precedence is necessarily idiosyncratic.



Richard Harter, (e-mail address removed)
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
 
D

Default User

John said:
In my 18 years of programming at 6 different companies, I've never
heard anyone rant about sizeof in this manner.

Richard is troll, which is why I killfiled him long ago. He'll "rant"
about anything that he thinks will cause trouble on the newsgroup. It's
best to ignore him if at all possible.




Brian
 
J

John Bode

Richard is troll, which is why I killfiled him long ago. He'll "rant"
about anything that he thinks will cause trouble on the newsgroup. It's
best to ignore him if at all possible.

Brian

I don't see him as a troll like Twink and McCormack; he strikes me as
someone who could contribute meaningfully to the group if he just got
over himself.
 

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

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top