Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>One aspect that confuses me is the actual representation, or should I say existence, of variables in memory. What exactly does a data type specify for a variable?</p> </blockquote> <p>At the machine level, the difference between <code>int</code> and <code>char</code> is only the size, or number of bytes, of the memory allocated for it by the programming language. In C, IIRC, a <code>char</code> is one byte while an <code>int</code> is 4 bytes. If you were to "look" at these inside the machine itself, you would see a sequence of bits for each. Being able to treat them as <code>int</code> or <code>char</code> depends on how the language decides to interpret them (this is also why its possible to convert back and forth between the two types).</p> <blockquote> <p>When using getchar(), I was told that it is better to use type "int" than type "char" due to the fact that "int" can hold more values while "char" can hold only 256 values.</p> </blockquote> <p>This is because there are 2^8, or 256 combinations of 8 bits (because a bit can have two possible values), whereas there are 2^32 combinations of 32 bits. The EOF constant (as defined by C) is a negative value, not falling within the range of 0 and 255. If you try to assign this negative value to a char (this squeezing its 4 bytes into 1), the higher-order bits will be lost and you will end up with a valid char value that is NOT the same as EOF. This is why you need to store it into an int and check before casting to a char.</p> <blockquote> <p>Also, does this mean that the data type "char" is only fine to use when we are simply assigning a value to a variable manually, such as 0char c = 'a', when we definitely know that we will only have 256 possible ASCII characters?</p> </blockquote> <p>Yes, especially since in that case you are assigning a character literal.</p> <blockquote> <p>Also, what is the actual important difference between "char" and "int"? If we can use "int" type instead of "char" type, why do we decide to use one over the other at certain times?</p> </blockquote> <p>Most importantly, you would pick <code>int</code> or <code>char</code> at the language level depending on whether you wanted to treat the variable as a number or a letter (to switch, you would need to cast to the other type). If you wanted an integer value that took up less space, you could use a <code>short int</code> (which I believe is 2 bytes), or if you were REALLY concerned with memory usage you could use a <code>char</code>, though mostly this is not necessary.</p> <p><strong>Edit</strong>: here's a <a href="http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/CONCEPT/data_types.html" rel="nofollow noreferrer">link</a> describing the different data types in C and modifiers that can be applied to them. See the table at the end for sizes and value ranges.</p>
 

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