PDA

View Full Version : A little thinking exercise


Mynick
August 13th, 2016, 01:02 PM
This is a quite simple problem but fun nonetheless.
Basic C knowledge and how data is stored in memory is required.

http://i.imgur.com/JnL1mGX.png

The correct output should be:
The number 65 corresponds to the following char: A
The char 'A' corresponds to the following number: 65

I know where the problem is but what I want you to do is explain WHY it happens.
No, it isn't homework. I know why it happens. lol

PS: I know it only works with the letter 'A'. I'm lazy, hardcoded it.

dxcxdzv
August 13th, 2016, 03:58 PM
My iPad crashed just when I was writing so I'll make this quick.

You're initiating first the value lol = 1000.
You then use the function scanf, which I assume is used to make you enter a random variable/constant, %c seems then to be the type of data, a character here. And lol seems to be address where it is stored.
You're then displaying the number 833 by using %d, which I assume is another type of data. And then you are displaying A as a %c/n type retrieved at the address 1000. I'm not really sure what the second lol is here for.
You're then displaying the type of data %d/n (which seems to be a number) retrieved at the address A.

Please note that I have no knowledge in C.

Mynick
August 13th, 2016, 04:32 PM
My iPad crashed just when I was writing so I'll make this quick.

You're initiating first the value lol = 1000.
You then use the function scanf, which I assume is used to make you enter a random variable/constant, %c seems then to be the type of data, a character here. And lol seems to be address where it is stored.
You're then displaying the number 833 by using %d, which I assume is another type of data. And then you are displaying A as a %c/n type retrieved at the address 1000. I'm not really sure what the second lol is here for.
You're then displaying the type of data %d/n (which seems to be a number) retrieved at the address A.

Please note that I have no knowledge in C.
Pretty well reasoned for someone who doesn't know C. Kudos.
I'll explain the code line by line if you want to try even you don't know C.

int lol = 1000;
Creates a variable "lol" which holds an integer. This case the value is 1000.
printf("Insert a char: ");
scanf("%c", &lol);
Outputs "Insert a char: ".
Reads a char from the keyboard and 'saves' the value in the variable "lol".
Even though I'm asking for a char and saving into a variable which holds an integer 'it's fine' (actually it's a bad programming practice and it's the reason of this error). Since C converts the char to its ASCII code (integer) and then stores the correspondent binary. (hint ;))
printf("The number %d corresponds to the following char: %c\n", lol, lol);
It'll print the sentence you see. The '%d' indicates I want to print whatever is 'inside' the 'lol' variable but I want it's the ASCII code (it' a format printer).
The '%c' means I want to print whatever is 'inside' the 'lol' variable but I want its correspondent char (compares the number with the ASCII table and then prints it).
\n I'ts newline.
printf("The char 'A' corresponds to the following number: %d\n", 'A');
This line just prints A's ASCII code, which is 65.

The rest of the code is irrelevant for the problem.

Since A's ASCII code is 65 and in the begining our input is A this line printf("The number %d corresponds to the following char: %c\n", lol, lol); Should be: The char 'A' corresponds to the following number: 65

TheMatrix
August 13th, 2016, 08:29 PM
Try initializng the value to 0 instead of 1000. What happens is that scanf only replaces the least significant byte of the integer (the byte that's looked at).

Observe this debugger screenshot of your code:
7201
Here, the "lol" variable is set to 0x03E8 (decimal 1000).

After doing scanf:
7202
scanf has only replaced the last byte (note that Intel is little endian, which means that the bytes are "backwards").

Apparently printf is supposed to consider the entire integer for %c. It probably says that somewhere in the spec.

edit: in the second screenshot 'A' is supposed to be 'a'

Mynick
August 14th, 2016, 06:47 AM
Try initializng the value to 0 instead of 1000. What happens is that scanf only replaces the least significant byte of the integer (the byte that's looked at).

Observe this debugger screenshot of your code:
7201
Here, the "lol" variable is set to 0x03E8 (decimal 1000).

After doing scanf:
7202
scanf has only replaced the last byte (note that Intel is little endian, which means that the bytes are "backwards").

Apparently printf is supposed to consider the entire integer for %c. It probably says that somewhere in the spec.

edit: in the second screenshot 'A' is supposed to be 'a'
You are correct :cool:
Nice job.

My explanation (it's in layman terms):
1. The memory location with the label 'lol' is assigned the value 1000 in binary which is 0000001111101000
2. The program asks for the user input which will be 'A'.
'A' ascii's value is 65 (01000001 in binary).
3. The programs alters the value stored at 'lol'. It starts by the least significant bit:
0000001111101000
01000001
0000001101000001
The value stored becomes 0000001101000001 which is (833) in decimal.