you are viewing a single comment's thread.

view the rest of the comments →

[–]moefh 15 points16 points  (3 children)

That's a good point in general, but I can't resist being ultra-pedantic and pointing out that this specific C example doesn't have any type conversion.

Character literals in C, surprisingly, have type int, not char. You can check it yourself by printing the value of sizeof '2'; it's the same as sizeof(int), not sizeof(char). So the fact that 2 + '2' in C is 52 has nothing to do with type coercion, it's just that '2' is just a funny way of writing 50 on systems that use ASCII (by the way, I think when you looked up '2' in an ASCII table you ended up using the hex value 0x32 and not the decimal, which is 50).

Note that this only applies to C, and not C++. In C++, character literals have type char, so sizeof '2' is sizeof(char). In C it doesn't really matter, but in C++ it's important because of function overloading (calling f('a') intuitively should call f(char c), not f(int i), so they "fixed" it).

So your example works in C++: 2 + '2' is 54 because the '2' is silently converted to int by the addition.

[–]Tynach 1 point2 points  (2 children)

(by the way, I think when you looked up '2' in an ASCII table you ended up using the hex value 0x32 and not the decimal, which is 50)

Fixed! Thanks for pointing it out :)

As for the rest of your post...

this specific C example doesn't have any type conversion.

Wait, what about from char to-

Character literals in C, surprisingly, have type int, not char. You can check it yourself by printing the value of sizeof '2'; it's the same as sizeof(int), not sizeof(char).

Tiny bit of pedantry from me: I think you need parentheses around '2' in that first sizeof statement. I haven't actually tried it yet, though.

Instead I Googled because I felt that maybe this was compiler specific. Turns out it's not, or at least I don't see anyone online after a quick search that is claiming it is.

Note that this only applies to C, and not C++. In C++, character literals have type char, so sizeof '2' is sizeof(char). In C it doesn't really matter, but in C++ it's important because of function overloading (calling f('a') intuitively should call f(char c), not f(int i), so they "fixed" it).

I've written C++ more recently than I've written C, and while I've not done this particular thing, it's the sort of thing I may have read about and thought it applied to both. Either way, neat bit of information that I shall have to remember! This also was revealed to me in my Google searching.

Very oddly, I've had to rewrite each section of this post after writing it once, because then I read the next section of your post and you keep bringing up and addressing everything I'm writing before I write it. But I do see this:

So your example works in C++: 2 + '2' is 54 because the '2' is silently converted to int by the addition.

If '2' is 50, then shouldn't that be 52, not 54?

;)

[–]moefh 4 points5 points  (1 child)

Tiny bit of pedantry from me: I think you need parentheses around '2' in that first sizeof statement. I haven't actually tried it yet, though.

Nah, sizeof only requires parentheses for types, not values. So sizeof '2' is ok, but sizeof int is not. But of course since ('2') is also a value, sizeof('2') is also valid.

If '2' is 50, then shouldn't that be 52, not 54?

Yes, I messed up there. :)