Mystry of C
-
Hi , How does following code works. int s = 0 ; printf("%d" ,s["123456789"]); //Output 49 printf("%d",s["987654321"]); //Output 57 printf("%d",s["9"]); //Output 57 Please provide any comments on this...
The "so-called" code is returning the ASCII base-10 value of the first character in the string.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Hi , How does following code works. int s = 0 ; printf("%d" ,s["123456789"]); //Output 49 printf("%d",s["987654321"]); //Output 57 printf("%d",s["9"]); //Output 57 Please provide any comments on this...
There's a couple of factors at play here:
- Array accessing -
array[index]
can be rewritten asindex[array]
(it's all because of the syntax of PDP-11 assembly language, in which and for which the first C compiler was written). So, with s==0,- s["123456789"] == "123456789"[s] == "123456789"[0] == "1" == 49 (ASCII code)
- s["987654321"] == "987654321"[s] == "987654321"[0] == "9" == 57 (ASCII code)
- s["9"] == "9"[s] == "9"[0] == "9" == 57 (ASCII code)
- When you use the format string "%d", printf interprets the next argument on the stack as an integer. Items on the stack are aligned on 4-byte boundaries, so characters can be read as integers, which means that the ASCII code of the character is printed (so long as the character is zer-extended before it's pushed onto the stack).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
- Array accessing -
-
There's a couple of factors at play here:
- Array accessing -
array[index]
can be rewritten asindex[array]
(it's all because of the syntax of PDP-11 assembly language, in which and for which the first C compiler was written). So, with s==0,- s["123456789"] == "123456789"[s] == "123456789"[0] == "1" == 49 (ASCII code)
- s["987654321"] == "987654321"[s] == "987654321"[0] == "9" == 57 (ASCII code)
- s["9"] == "9"[s] == "9"[0] == "9" == 57 (ASCII code)
- When you use the format string "%d", printf interprets the next argument on the stack as an integer. Items on the stack are aligned on 4-byte boundaries, so characters can be read as integers, which means that the ASCII code of the character is printed (so long as the character is zer-extended before it's pushed onto the stack).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Stuart Dootson wrote:
Array accessing - array[index] can be rewritten as index[array]
Yeah, read that somewhere recently. interesting. Thanks.
This signature was proudly tested on animals.
- Array accessing -
-
There's a couple of factors at play here:
- Array accessing -
array[index]
can be rewritten asindex[array]
(it's all because of the syntax of PDP-11 assembly language, in which and for which the first C compiler was written). So, with s==0,- s["123456789"] == "123456789"[s] == "123456789"[0] == "1" == 49 (ASCII code)
- s["987654321"] == "987654321"[s] == "987654321"[0] == "9" == 57 (ASCII code)
- s["9"] == "9"[s] == "9"[0] == "9" == 57 (ASCII code)
- When you use the format string "%d", printf interprets the next argument on the stack as an integer. Items on the stack are aligned on 4-byte boundaries, so characters can be read as integers, which means that the ASCII code of the character is printed (so long as the character is zer-extended before it's pushed onto the stack).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Holy Moly! How do you know stuff like this?! I'm duly impressed.
If the post was helpful, please vote! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
- Array accessing -
-
Holy Moly! How do you know stuff like this?! I'm duly impressed.
If the post was helpful, please vote! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
Mustafa Ismail Mustafa wrote:
How do you know stuff like this?!
20 years of reading stuff combined with a (very) good memory :-) You may have had an annoying classmate at school who never seemed to have to study for exams? Yeah, that was me - learning stuff just sticks with me. Remembering peoples names or what I was doing last week? Nah, can't do that!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Mustafa Ismail Mustafa wrote:
How do you know stuff like this?!
20 years of reading stuff combined with a (very) good memory :-) You may have had an annoying classmate at school who never seemed to have to study for exams? Yeah, that was me - learning stuff just sticks with me. Remembering peoples names or what I was doing last week? Nah, can't do that!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Stuart Dootson wrote:
You may have had an annoying classmate at school who never seemed to have to study for exams?
Funny, its like you were describing me. I never took that in class though (the OP that is)
If the post was helpful, please vote! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?