1

I use sizeof() method to get the memory size of char variable. i.e.,I write such a code in Xcode:

    char a = '1';
size_t size = sizeof(a);

I find the size is 1.So what is this character's encoding type? UTF-8 or another?

huixing
  • 321
  • 2
  • 11

3 Answers3

4

You'd have to know this before using the character, just as you are expected to know the encoding of a string before you can interpret it correctly.

You cannot determine the encoding without prior knowledge.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
2

The sizeof method is not enough to determine what the encoding of a character is.

In general, you must be told what encoding is being used before being able to interpret characters.

As stated in a comment, look here for some information on C string encoding: What is the default encoding for C strings?

Community
  • 1
  • 1
Almo
  • 15,538
  • 13
  • 67
  • 95
0

For some character literals the C Standard defines a fixed value (the digits if I remembers correctly) for others (letters) the C Standard leaves it to the implementation which char-set/encoding to use.

alk
  • 69,737
  • 10
  • 105
  • 255