Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Converting unsigned long to string in C

Reply
Thread Tools

Converting unsigned long to string in C

 
 
CBFalconer
Guest
Posts: n/a
 
      02-27-2008
Eric Sosman wrote:
> Richard wrote:
>> Keith Thompson <(E-Mail Removed)> writes:
>>>
>>> [...] *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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      02-27-2008
santosh wrote:
> Bartc wrote:
>> "Richard Heathfield" <(E-Mail Removed)> wrote in message
>>> Richard Tobin said:
>>>> Keith Thompson <(E-Mail Removed)> wrote:
>>>>
>>>>> Note that some languages have a number of operators that use
>>>>> keywords (For example, Ada has "and", "or", "xor", "not", "mod",
>>>>> "rem", "abs", "not", even though the fundamental arithmetic
>>>>> operators use the usual symbols "+", "-", "/", "*".)
>>>>
>>>> I have no objection to operators that are words, but C doesn't
>>>> have any apart from sizeof.
>>>
>>> ...except for:
>>>
>>> * and

.... snip ...
>>> * xor
>>> * xor_eq
>>>

>>
>> 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?

>
> I believe it was to alleviate problems for programmers working
> with non-standard keyboards where some of these symbols were
> hard to generate.
>
>> And macros in lower-case too! (I suppose some attempt to
>> avoid user-namespace pollution.)

>
> 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 ^.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Micah Cowan
Guest
Posts: n/a
 
      02-27-2008
Richard Tobin wrote:
> In article <fq3pv1$dm3$(E-Mail Removed)>,
> Richard <(E-Mail Removed)> wrote:
>
>> 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.


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.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-27-2008
Keith Thompson <(E-Mail Removed)> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      02-27-2008
Richard wrote:
<snip>
> 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      02-27-2008
Bartc wrote:
>> * and
>> * and_eq
>> * bitand
>> * bitor
>> * compl
>> * not
>> * not_eq
>> * or
>> * or_eq
>> * xor
>> * xor_eq
>>

>
> 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? ) in C's
<iso646.h>, except that in C++ they are full-fledged keywords.

> 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.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
 
Reply With Quote
 
Richard Harter
Guest
Posts: n/a
 
      02-27-2008
On Wed, 27 Feb 2008 15:25:06 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) (Doug
Miller) wrote:

>In article <fq3psc$dm3$(E-Mail Removed)>, Richard <(E-Mail Removed)> wrote:
>
>>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)) ?
>

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 Removed)
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-27-2008
John Bode wrote:

> On Feb 26, 1:26 pm, Richard <(E-Mail Removed)> wrote:


> > I'm deadly serious about this use of sizeof.

>
> 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
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      02-27-2008
On Feb 27, 1:43 pm, "Default User" <(E-Mail Removed)> wrote:
> John Bode wrote:
> > On Feb 26, 1:26 pm, Richard <(E-Mail Removed)> wrote:
> > > I'm deadly serious about this use of sizeof.

>
> > 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


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.
 
Reply With Quote
 
William Pursell
Guest
Posts: n/a
 
      02-27-2008
On Feb 27, 1:47 pm, Richard <(E-Mail Removed)> wrote:
> William Pursell <(E-Mail Removed)> writes:


>
> Would you like to see
>
> printf "hello%s" "world";
>
> or something equally as contrived for example?



I would much rather see:

printf "hello %s\n" "world";

Which, BTW, is actually valid in at least one major language:
$ printf "hello %s\n" "world"
hello world


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Question on converting unsigned long to std::string Ramesh C++ 2 10-10-2008 07:58 AM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments