0

I've stumbled on this while learning JAVA I've noticed that char takes 16 bit , while I remember that it takes 8 in C. can someone explain why they are not resemble ?

LordTitiKaka
  • 2,087
  • 2
  • 31
  • 51
  • 4
    A `char` is not necessarily 8-bits wide in C. The size of that type is implementation-dependent. – Frédéric Hamidi Feb 27 '14 at 12:46
  • 1
    Is there a reason you cannot simply read the [manual](http://msdn.microsoft.com/en-US/library/x9h8tsay.aspx)? EDIT: changed your tags to Java since you mentioned Java. If C# was right, change it back and include and explanation. – nvoigt Feb 27 '14 at 12:47
  • @FrédéricHamidi It's 8 bits on all platforms where a byte has 8 bits though. So it's never used to represent multi-byte characters, which I think is the point here. – sepp2k Feb 27 '14 at 12:48
  • You stumbled on this while learning "JAVA" then why do your question read "C#" and the tag too? – Abhineet Feb 27 '14 at 12:49
  • @Abhineet you are right , I've never noticed it in C# until I saw it in JAVE :D – LordTitiKaka Feb 27 '14 at 13:07

1 Answers1

1

C# chars are Unicode, which is 16-bit, while C only uses ASCII, which is actually only 7 bits.

azurelogic
  • 801
  • 6
  • 12
  • So, you are saying that there is no implementation of UNICODE chars in C??? – Abhineet Feb 27 '14 at 12:50
  • 3
    To be precise, C# and Java use UTF16 for their `char` definition. – Hot Licks Feb 27 '14 at 12:50
  • @Abhineet - C is about 30 years older than Unicode. When C was written they were still trying to decide whether to use ASCII or USASCII or EBCDIC or FieldData or Baudot. – Hot Licks Feb 27 '14 at 12:52
  • @HotLicks:: But I will still say that stating "C only uses ASCII" would be wrong. – Abhineet Feb 27 '14 at 12:54
  • If you need Unicode, there is library support for it, but it is not native: http://www.cprogramming.com/tutorial/unicode.html – azurelogic Feb 27 '14 at 12:54
  • @Abhineet - Actually, it's mostly correct. The only place that C interfaces with the character set is in the compiler (obviously) and in string literals. There have been versions of C written that supported EBCDIC instead of ASCII, but 99% of compilers are ASCII only. C++ does, I believe, add some features to support Unicode escapes, but still doesn't really "know" what Unicode is. – Hot Licks Feb 27 '14 at 13:03
  • @RobRibeiro thank you for your short answer – LordTitiKaka Feb 27 '14 at 13:10