sizeof operator on string(charcter array)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aftabpasha
    New Member
    • Oct 2008
    • 32

    sizeof operator on string(charcter array)

    I have seen the following program.

    Code:
    #include<stdio.h>
    int main()
    {
            char str[]="S\065AB";
            printf("\n%d", sizeof(str));
            return(0);
    }
    It's output is 5.
    Please, someone explain why it is giving o/p 5.

    Thank You.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Because you have supplied a string that is 5 characters long. Try printing out the string as well as its size.

    Comment

    • Aftabpasha
      New Member
      • Oct 2008
      • 32

      #3
      Thanks for your reply.
      I have tried to print its output. It is "S5AB". Why it is neglecting "\06"? I was thinking it should print only "S" considering '\0' as end of string. Please explain this.
      Thank You.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Use of numeric escape sequences has such potential for confusion that it may be helpful take advantage of implicit concatenation to isolate the escape sequence:
        Code:
        char str[] = "S" "\065" "AB";
        You should review the action of the escape character in string literals to see why I chose to break the string at these points.

        Compare the following strings. What is the size (returned by sizeof), length (returned by strlen), and content of each?
        Code:
        char str1[] = "S\065AB";
        char str2[] = "S" "\065" "AB";
        char str3[] = "S" "\06" "5AB";
        char str4[] = "S" "\0" "65AB";

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Has a hint you may also want to look up the ASCII code 065, remembering that a digit sequence starting with 0 is in octal (base 8).

          Or try reading Escape Sequences (C)

          or try both of course :-)

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Octal numbers ... what a pain! Why is the C language defined such that it is so easy to bump into that archaic radix?

            35-40 years ago octal was much more commonly used than hexadecimal. In that milieu nobody questioned the decision to make octal the default radix for character constants.

            This is another example of how some programs outlast their implicit assumptions. Let those who have eyes, see.

            -------

            Many years ago, when I was a callow youth, I wrote a static initializer for a large integer array. I thought it would be nice if the initial values lined up nicely in rows in columns so I padded the initial values with leading zeroes so they were all the same width. The initializer was beautiful, but truly evil. That day I learned initial values should only be padded with leading whitespace.

            Comment

            • Aftabpasha
              New Member
              • Oct 2008
              • 32

              #7
              Thank you both donbock and Banfa for your replies. I got the concept.
              Thank You.

              Comment

              Working...