Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Mystry of C

Mystry of C

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pandit84
    wrote on last edited by
    #1

    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...

    G S 2 Replies Last reply
    0
    • P pandit84

      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...

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • P pandit84

        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...

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        There's a couple of factors at play here:

        1. Array accessing - array[index] can be rewritten as index[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)
        2. 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

        M M 2 Replies Last reply
        0
        • S Stuart Dootson

          There's a couple of factors at play here:

          1. Array accessing - array[index] can be rewritten as index[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)
          2. 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

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Stuart Dootson

            There's a couple of factors at play here:

            1. Array accessing - array[index] can be rewritten as index[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)
            2. 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

            M Offline
            M Offline
            Mustafa Ismail Mustafa
            wrote on last edited by
            #5

            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[^]?

            S 1 Reply Last reply
            0
            • M Mustafa Ismail Mustafa

              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[^]?

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • S Stuart Dootson

                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

                M Offline
                M Offline
                Mustafa Ismail Mustafa
                wrote on last edited by
                #7

                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[^]?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups