nalzok
nalzok

Reputation: 16137

Why does 'u0'=30000 in C?

Here is the code:

#include <stdio.h>

int main()
{
    printf("%d %c",'u0','u0');
    return 0;
}

When I run it on my machine, the output is "30000 0", and I have never seen 'u0' used to represent a character (or maybe an integer in fact). Perhaps this implies that the character/integer is unsigned?

Upvotes: 1

Views: 89

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263627

That's a multi-character constant. It has type int and an implementation-defined value.

Quoting the latest draft of the C standard (N1570), section 6.4.4.4 paragraph 10:

The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.

As for why it has the specific value 30000, apparently your compiler gives the constant 'u0' the value 'u' << 8 + '0'. Since 'u'==117 and '0'==48 (in an ASCII-based character set), the result is 30000. But don't count on that specific value; it could vary for other compilers.

I advise not using such constants.

Upvotes: 5

Related Questions