Exercise 1
The following is a program code example:
1 int age;
2 int n;
3 char vstring [28]; char char_age[3];
4 n=18;
5 vstring = "How old are you ? ";
6 print vstring;
7 scan char_age;
8 age = atoi(char_age);
9 if (age%26gt;=n){
10 vstring = "You can vote ! ";
11 else
12 vstring = "You are too young to vote !";
13 }
14 print vstring ;
Read this example and explain :
1) show the contents of variable "vstring" as ASCII decimal and binary when the program executes line 6 ;
2)show the contents of variable "vstring" as ASCII decimal and binary when the program executes line 14 for two cases listed above
Exercise 2
Show how the phrase My sister is 17 years old would be stored in the memory.
Hint : use ASCII codes and show the code for each character.
Compare results of Exercise 2 and of Exercise 1, question 2). How 17 is stored as part of the character string and how 17 is stored as a number. Why is it different ?
Im not really asking anyone to do this for me but please explain two things. What is she asking for in questions 1 and 2 in regards to vstring? what would be the difference?
and the secong thing i dont understand is how is 17 stored as a character string and how is it stored as a number? i dont understand what she is asking. i just want explanations not asking you to do it for me please include an example. ..Thanks|||This can be confusing when you are first starting out. Unlike many posts here, you seem to be making an effort so I will try to help if I can.
Every data type in C is really just a collection or series of bytes. An "int" is a series bytes (usually 2 or 4, it depends on the compiler) where the bits of all those bytes are combined to represent a single value. A string is a series of bytes where each byte represents one character.
To store the string "17", the computer would use 2 bytes, the first one set to the ASCII value that means "print a 1" and the second byte would have the ASCII value for "print a 7". (there is also a terminator byte after those two bytes).
To store the value of 17 in an int variable, the computer would take all the bits in that int variable and set them so that mathematically they represented the value 17 (in binary).
The ASCII code for the character "1" is 49. "7" is 55. So to make a 1 in a series of bytes that are part of a string, you need that byte contain the value 49. The next byte needs to be set to 55.
In binary, these two bytes look like: 00110001 00110111
The value 17 stored in an int would look like: 00000000 00010001
(if the int type uses 2 bytes on your system, it may use 4 in which case there would be 2 more bytes in front of these two, all bit set to 0s)
Hope that helps. You might want to find an ascii chart so you look up the values for all the different characters.|||its all 1s and 0s to me.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment