How does an int gets type promoted to a char ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • radha gogia
    New Member
    • Feb 2015
    • 56

    How does an int gets type promoted to a char ?

    Code:
    #include<stdio.h>
    int main()
    {
    char arr[3]={0,2,3,4,5,6,7};
    printf("%d  ",sizeof(arr));
    printf("%c ", arr[0]);
    printf("%c ", arr[1]);
    printf("%c ", arr[2]);
    printf("%c ", arr[3]);
    return 0;
    }
    In this code 2 and 3 would be acting like ASCII values so then these are 4 byte integer values so when we will extract 1 byte from it since its a character array so then how come it is not printing null character for each array index value ?
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    First thing you should do is get the errors out
    Code:
    cc     lala.c   -o lala
    lala.c: In function ‘main’:
    lala.c:5:2: warning: excess elements in array initializer [enabled by default]
      char arr[3]={0,2,3,4,5,6,7};
      ^
    lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
    lala.c:5:2: warning: excess elements in array initializer [enabled by default]
    lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
    lala.c:5:2: warning: excess elements in array initializer [enabled by default]
    lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
    lala.c:5:2: warning: excess elements in array initializer [enabled by default]
    lala.c:5:2: warning: (near initialization for ‘arr’) [enabled by default]
    Last edited by Luuk; Sep 13 '15, 05:21 PM. Reason: oops: they are warnings, but should give a hint why things are happening......

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      There is no such thing as promotion to a char.

      Promotion means to make a smaller type into a bigger type. Like promotion a char (1 byte) to an int (4 bytes).

      The %c and %d are printf format specifiers where you request the data to be displayed as a char or as an int.

      In this case the int of the array is typecast by printf to a char. Most likely the least significant byte (LSB) of the int becomes the char. The remaining bytes of the int are lost.

      Comment

      • radha gogia
        New Member
        • Feb 2015
        • 56

        #4
        So that's what I am trying to say that 0 , 2,3 these numbers are 4 byte value , hence these would be getting converted to 1 byte starting from LSB , so for arr[1]= 2 , op must be some character corresponding to ascii value 2 , but it is not printing anything , although it prints some character for ascii value 3 and 4 , so then ?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Remember, an int value of 2 is not an ASCII value of 2. A char '2' would be an int 50.

          Comment

          • Luuk
            Recognized Expert Top Contributor
            • Mar 2012
            • 1043

            #6
            A char '2' will only be an int 50 if it's specified in decimal notation.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              A '2' is always 50.

              The %c tells printf you have an ASCII value so the 50 will be displayed as 2.

              Code:
              char x = 50;
              printf("%c, %d\n", x, x);
              this will display 2,50.

              Comment

              • radha gogia
                New Member
                • Feb 2015
                • 56

                #8
                If I do like
                printf("%c %c" , 3, 4);
                Then I am getting the character value corresponding to int 3 and 4 , and that is quite obvious also since we are chosing LSB's so why not for 2 I am getting this output , that's the confusion .

                Comment

                • Luuk
                  Recognized Expert Top Contributor
                  • Mar 2012
                  • 1043

                  #9
                  sorry, I don't understand I tried this, and output seemed correct (and logical) to /me

                  Code:
                  ~/tmp> cat test.c
                  #include <stdio.h>
                  
                  void main()
                  {
                          printf("%c %c %c", 2, 3, 4);
                  }
                  ~/tmp> make test
                  cc     test.c   -o test
                  ~/tmp> ./test | hexdump -C
                  00000000  02 20 03 20 04                                    |. . .|
                  00000005
                  ~/tmp>

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Forget LSB, byte alignment, conversion algorithms, etc...

                    %c tells printf to display the ASCII value (what is called the SYMBOL of the argument) if the value is between 32 and 127. Values outside this range are not printable ASCII values and are displayed as is and usually appear as weird characters.

                    It does not matter what the type of the argument is as long as it is an integer. char and int are both integers. Both behave the same with %c.
                    Last edited by weaknessforcats; Sep 15 '15, 09:31 PM. Reason: typo

                    Comment

                    • radha gogia
                      New Member
                      • Feb 2015
                      • 56

                      #11
                      Thankss a lot , that was a great explanation now which actually cleared my confusion , I understood the concept now , thankss once again .

                      Comment

                      Working...