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

 
 
Jean-Marc Bourguet
Guest
Posts: n/a
 
      02-25-2008
Richard <(E-Mail Removed)> writes:

> Operator? Get out of it.


I can write

x = sizeof (ptr+1)->field;

which is valid, but would be meaningless if sizeof was a function returning
an int.

I can't write

x = (sizeof)(ptr);

which would be valid if sizeof was a function.

Luckily, I don't think there is a case where interpreting sizeof as a
function lead to a valid but different parse as using its true definition
excepted variations on the obfuscated contest candidate:

x = sizeof(i)[ptr];


You may want to force additional parenthesis in your coding standard if you
find them more readable. I've no strong opinion about them. But don't let
your cosmetic preference (as cosmetic as prefering the absence of them)
induce you into error, sizeof is not a function and doesn't behave like
one.

A+

--
Jean-Marc
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      02-25-2008
Richard Heathfield <(E-Mail Removed)> writes:
> Richard Tobin said:
>> In article <(E-Mail Removed)>,
>> Richard Heathfield <(E-Mail Removed)> wrote:
>>>> I have no objection to operators that are words, but C doesn't have
>>>> any apart from sizeof.

>>
>>>...except for:
>>>
>>>* and
>>>[...]

>>
>> 10/10 for pedantry.

>
> Strictly speaking, it should be 11/11.


Strictly speaking, 10/10 == 11/11.

--
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
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      02-25-2008
Eric Sosman <(E-Mail Removed)> writes:
> Richard wrote:
>> it makes no sense to me whatsoever and I dont give a monkeys uncle about
>> whats in the standard

>
> From the horse's mouth, folks.


Indeed.

The simple fact is that sizeof *is* an operator, whether anyone likes
it or not. You (that's a generic "you") can use all the parentheses
you like in your own code, but if you can't cope with "sizeof x", then
you can't cope with valid C.

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

(There are C coding styles that I dislike, but I can deal with them,
and even use them if necessary. I don't question the motivation or
integrity of those whose opinions differ from mine.)

--
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
 
Richard Heathfield
Guest
Posts: n/a
 
      02-25-2008
Keith Thompson said:

> Richard Heathfield <(E-Mail Removed)> writes:
>> Richard Tobin said:
>>> In article <(E-Mail Removed)>,
>>> Richard Heathfield <(E-Mail Removed)> wrote:
>>>>> I have no objection to operators that are words, but C doesn't have
>>>>> any apart from sizeof.
>>>
>>>>...except for:
>>>>
>>>>* and
>>>>[...]
>>>
>>> 10/10 for pedantry.

>>
>> Strictly speaking, it should be 11/11.

>
> Strictly speaking, 10/10 == 11/11.


It depends on what you mean by it. In context, 10/10 suggests "10 out of
10", rather as if it were the score on a test, i.e. in the spirit of "10
questions right out of 10 questions asked", rather than "10 divided by
10". Now, "10 right out of 10 asked" is /not/ the same as "11 right out of
11 asked" (the latter being the more difficult achievement), so 10/10
isn't necessarily == 11/11.

(Of course, in a C context they do evaluate to the same result, but we all
knew that we all knew that already, right?)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      02-25-2008
On Mon, 25 Feb 2008 10:36:16 +0000, Richard Heathfield wrote:
> Richard Tobin said:
>> I have no objection to operators that are words, but C doesn't have any
>> apart from sizeof.

>
> ...except for:
>
> * and
> * and_eq
> * bitand
> * bitor
> * compl
> * not
> * not_eq
> * or
> * or_eq
> * xor
> * xor_eq


They're operators in C++. They're macros in C.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-25-2008
Harald van D?k wrote:

> On Mon, 25 Feb 2008 10:36:16 +0000, Richard Heathfield wrote:
>> Richard Tobin said:
>>> I have no objection to operators that are words, but C doesn't have
>>> any apart from sizeof.

>>
>> ...except for:
>>
>> * and
>> * and_eq
>> * bitand
>> * bitor
>> * compl
>> * not
>> * not_eq
>> * or
>> * or_eq
>> * xor
>> * xor_eq

>
> They're operators in C++. They're macros in C.


Any reason to make them macros instead of keywords, as in C++?

 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      02-25-2008
On Tue, 26 Feb 2008 00:26:45 +0530, santosh wrote:
> Harald van D?k wrote:
>> On Mon, 25 Feb 2008 10:36:16 +0000, Richard Heathfield wrote:
>>> Richard Tobin said:
>>>> I have no objection to operators that are words, but C doesn't have
>>>> any apart from sizeof.
>>>
>>> ...except for:
>>>
>>> * and
>>> * and_eq
>>> * bitand
>>> * bitor
>>> * compl
>>> * not
>>> * not_eq
>>> * or
>>> * or_eq
>>> * xor
>>> * xor_eq

>>
>> They're operators in C++. They're macros in C.

>
> Any reason to make them macros instead of keywords, as in C++?


To minimise the modifications required for compilers, I believe. It's only
recently that C++ compilers handle the alternate spellings of the
operators correctly (including such things as the required diagnostic for
the syntax error in #ifdef and), when all that complexity is really not
necessary. For correct programs, the only effective difference between a
macro "and" and an operator "and" is how they are stringised, and this
will affect very few programs.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-25-2008
santosh said:

> Harald van D?k wrote:
>

<snip>

>> They're operators in C++. They're macros in C.

>
> Any reason to make them macros instead of keywords, as in C++?


By making them macros, the Standard attempts to avoid breaking existing
code. After all, these names are in C90 user namespace! So the Standard
stops you from getting access to these operators unless you specifically
request it by #including iso646.h - or at least, that seems to me to be
the most likely rationale.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-25-2008
Harald van D?k wrote:

> On Tue, 26 Feb 2008 00:26:45 +0530, santosh wrote:
>> Harald van D?k wrote:
>>> On Mon, 25 Feb 2008 10:36:16 +0000, Richard Heathfield wrote:
>>>> Richard Tobin said:
>>>>> I have no objection to operators that are words, but C doesn't
>>>>> have any apart from sizeof.
>>>>
>>>> ...except for:
>>>>
>>>> * and
>>>> * and_eq
>>>> * bitand
>>>> * bitor
>>>> * compl
>>>> * not
>>>> * not_eq
>>>> * or
>>>> * or_eq
>>>> * xor
>>>> * xor_eq
>>>
>>> They're operators in C++. They're macros in C.

>>
>> Any reason to make them macros instead of keywords, as in C++?

>
> To minimise the modifications required for compilers, I believe. It's
> only recently that C++ compilers handle the alternate spellings of the
> operators correctly (including such things as the required diagnostic
> for the syntax error in #ifdef and), when all that complexity is
> really not necessary. For correct programs, the only effective
> difference between a macro "and" and an operator "and" is how they are
> stringised, and this will affect very few programs.


Thanks to both Richard and Harald.

 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      02-25-2008
On 25 Feb 2008 at 18:01, Richard Heathfield wrote:
> Keith Thompson said:
>
>> Richard Heathfield <(E-Mail Removed)> writes:
>>> Richard Tobin said:
>>>> In article <(E-Mail Removed)>,
>>>> Richard Heathfield <(E-Mail Removed)> wrote:
>>>>>> I have no objection to operators that are words, but C doesn't have
>>>>>> any apart from sizeof.
>>>>
>>>>>...except for:
>>>>>
>>>>>* and
>>>>>[...]
>>>>
>>>> 10/10 for pedantry.
>>>
>>> Strictly speaking, it should be 11/11.

>>
>> Strictly speaking, 10/10 == 11/11.

>
> It depends on what you mean by it. In context, 10/10 suggests "10 out of
> 10", rather as if it were the score on a test, i.e. in the spirit of "10
> questions right out of 10 questions asked", rather than "10 divided by
> 10". Now, "10 right out of 10 asked" is /not/ the same as "11 right out of
> 11 asked" (the latter being the more difficult achievement), so 10/10
> isn't necessarily == 11/11.
>
> (Of course, in a C context they do evaluate to the same result, but we all
> knew that we all knew that already, right?)


Another insightful discussion, getting right to the heart of a juicy C
problem...

 
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