Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your question is very broad, I'll try to address some specific issues you raised, hopefully it will get you abit more sorted out.</p> <ul> <li><p>The <code>char</code> type can be though of as just another numerical type, just like int, short and long. It is totally ok to write <code>char a=3;</code>. The difference is that with <code>char</code>s the compiler gives you some added value. instead of just numbers you can also assign ASCII characters to a variable like <code>char a='U';</code> and then the variable will get the ASCII value of that character and you can also initialize arrays of character using literal strings like so: <code>char *s="hello";</code>.<br> This doesn't change the fact that after all char is still a numeric type and a string is just an array of numbers. If you'll look at the memory of the string, you'll see the ASCII codes of the string.</p></li> <li><p>The choice of <code>char</code> being 1 byte is arbitrary and is largely kept this way in C due to historical reasons. more modern languages like C# and Java define char to be 2 bytes.</p></li> <li><p>You don't need "another" type for characters. <code>char</code> is just the numeric type that holds a single singed/unsigned byte the same as <code>short</code> is the numeric type that holds a signed 16 bit word. The fact that this data type is used for characters and strings is just syntactic sugar provided by the compiler. <strong>1 byte integers == char</strong>.</p></li> <li><p><code>printf()</code> only works with chars since this is the way C was designed. it it was designed today it would possibly be working with shorts. Indeed in windows you have a version of <code>printf()</code> which works with shorts, it is called <code>wprintf()</code></p></li> <li><p>the type <code>wchar_t</code>, in windows, is just another name for <code>short</code>. somewhere in the windows header files there is a decleration like this: <code>typedef short wchar_t;</code> which makes this happen. You can use them interchangeably. The advantage of using the word <code>wchar_t</code> is that whoever reads your code knows that you now want to use characters rather than numbers. Another reason is that if there's a remote chance that sometime Microsoft will decide that now they want to use UTF32 then all they need to do is redefine the typedef above to be <code>typedef int wchar_t;</code> and that's it (in reality this will be quite abit more complicated to achieve so this change is unlikely in the for seeable future)</p></li> <li><p>If you want to use some 8-bit encoding that is not ASCII, for instance the encoding for hebrew which is called "Windows-1255" you just use chars. There are many such encodings but these days using UNICODE is always preferable. Indeed there is actually a version of Unicode itself which fits in 8-bit strings that is UTF-8. If you're dealing with UTF-8 strings then you should work with the <code>char</code> data type. There is nothing that limits it to working with ASCII since it is just a number, it can mean anything.</p></li> <li><p>Working with such long numbers is usually done using something called "decimal types". C doesn't have this but C# does. The basic idea of these types is that they handle a number similar to a string. Every digit of the decimal representation is saved using 4 bits so an 8 bit variable can save the numbers in the range 0-99, a 3 byte array can save values in the range of 0-999999 and so on. This way you can save numbers of any range.<br> The downside to these is that making calculations on them is alot slower than making calculations on normal binary numbers.<br> I am not sure if there are libraries which do this kind of thing in C. Use google to find out.</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload