If I assume that a character is stored in RAM, and I go to that
address, what will I find there?
First, let us remember how
characters are stored in RAM; when we make the statement in C or C++ char character1 we are, first of all,
asking for 1 byte of storage in RAM or 8 bits. By default characters are
signed, this means that when evaluating the contents of that location the
computer looks at the first bit and analyze if is a negative or nonnegative
value (0 = nonnegative and 1= negative). After this, the computer proceeds to
evaluate the remaining 7 bits to see what numeric value is stored in that
location and then match this value to the ASCII table. A variant to this is if
we declared the following unsigned
character1, This statement will result in the computer taking all 8 bits
and evaluating the numeric value stored in that location and looking for the
matching ASCII character in the extended character set table.
Now let’s see what happen
when we write the statement char character1 in C or C++. When we run the
program, 1 byte of storage is allocated and given the name character1. If we go to that particular address we will notice that
there is something there. How can this
be if we have not initialized the variable?
Contrary to other
programming languages C and C++ do not clear the contents of an address when a
variable is assigned to a particular address. Other programs, like Cobol, when
allocating a variable to an address, get rid of whatever is stored at that
particular address. C and C++ keep whatever
value is stored in that address until we actually set a value to the variable.
Let us look at the following
short program and see what the output is to better understand this concept.
#include <stdio.h>
void main()
{
char
character1;
printf("This
is what is stored in character1 %c\n", character1);
}
As mentioned before, C and
C++ will give us an output of whatever character was stored at that location.
On the other hand, let’s see
what happen when we initialize the variable character1
#include <stdio.h>
void main()
{
char
character1='A';
printf("This
is what is stored in character1 %c\n", character1);
}
Once again C++ gives us the
output of whatever the contents of character1
were, because we initialize it with the character A that is our output.
One more time for reviewing
sake, if we assume that a character is stored in a particular location in RAM
and we go and look at that address we will find 1 byte or 8 bits. The contents
of that particular address are determined by whether if we have initialized the
variable or not. If we did initialize it the contents will be whatever value we
assigned to the variable; if, on the contrary, we did not initialize the
variable we will get whatever value was stored at the location.
Now let’s test your understanding
with the following questions:
If we see the following statement unsigned char how is the computer going
to evaluate the contents of that location?
Answer: The statement unsigned
char will result in the computer evaluating all 8 bits to come out with the
numeric value stored in the location.
Why do we get such strange output when printing out
the contents of a declared variable? And why that this happen?
Answer: Because by declaring a variable we are only
requesting a location in RAM, we are not initializing it. Therefore, when
printing out the contents of that variable, C++ will give us whatever character
was stored there before. Remember that C and C++ do not clear the contents of
the location in RAM when allocating an address.
Characters, by default, are signed.
A- True B-
False
Answer: True, by default characters are signed.
References.
Not sure about something in
C++, send your question