char, unsigned char, signed char functions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: char, unsigned char, signed char functions

  1. #1
    Join Date
    Dec 2003
    Location
    Okla, US
    Posts
    126

    char, unsigned char, signed char functions

    I am wondering why the following code compiles and runs? I have ran into this at work, and I am stumped.

    Code:
    #include <stdio.h>
    
    void testFunc( char value )
    {
      printf( "testFunc( char )\n" );
    }
    
    void testFunc( signed char value )
    {
      printf( "testFunc( signed char )\n" );
    }
    
    void testFunc( unsigned char value )
    {
      printf( "testFunc( unsigned char )\n" );
    }
    
    int main()
    {
      char c1 = '1';
      signed char c2 = '2';
      unsigned char c3 = '3';
    
      testFunc( c1 );
      testFunc( c2 );
      testFunc( c3 );
    
      return 0;
    }
    Also the below code, calls the int function instead of the signed char function.

    Code:
    #include <stdio.h>
    
    void testFunc( int value )
    {
      printf( "testFunc( int )\n" );
    }
    
    void testFunc( signed char value )
    {
      printf( "testFunc( signed char )\n" );
    }
    
    int main()
    {
      char c1 = '1';
    
      testFunc( c1 );
    
      return 0;
    }
    The signedness of char is signed on the compilers that I have tested the above code. I have used GCC on Knoppix and Gentoo, as well as Microsoft's Visual Studio .NET 2003.

    Any insight will be much appreciated.

    Thanks!
    gorshing
    newb

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    char is signed always.
    unsigned char is the alternative.

    But the language does not care on most things -- there are some issues with logical operations on signed values (some platforms will allow these to modify sign bit, some do not). For example you can set
    unsigned char duh = -1;
    cout << duh; //prints 255
    not exactly great coding, but its *legal*

    The code is legal (well, except stdio.h is going away) and as far as I can see does not have any 'undefined' parts.

    It is odd that it takes signed and char not to be the same thing (allows overload), is that the question?
    Last edited by jonnin; 07-27-2005 at 10:08 AM.

  3. #3
    Join Date
    Dec 2003
    Location
    Okla, US
    Posts
    126
    jonnin,

    I understand the difference between signed and unsigned, and yes that is my questions as of why it allows all three.

    I believe the following answers my questions. I just found it a couple minutes ago.

    Evidently, C++ treats them as three different types.

    Really Long Google Groups Link
    gorshing
    newb

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by jonnin
    char is signed always.
    unsigned char is the alternative.
    Not quite so. char is char, which means that its signedness is platform specific. It could well be mapped to 'unsigned char' by default.

    However, as far as types are concerned (not the underlying binary layout, which is a different story), these are three distinct types. That's why the first program compiles and runs as it does.

    The problem with the second function is that none of the parameters match the argument exactly. The compiler therefore has to promote it, and the first level promotion is to int. In other words, there's no reason to assume that just because 'char' and 'unsigned char' have 'char' in them, they are better candidate for overalod resolution that is char to int. Notice that if the argument and the parameter match exactly, the compiler chooses the "right" function.
    Last edited by Danny; 07-27-2005 at 06:14 PM.
    Danny Kalev

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    So by messing around with this you could create an array of ints instead of char by mistake?

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    How is it possible? You can't pass an array to a function.
    Danny Kalev

  7. #7
    Join Date
    Dec 2003
    Location
    Okla, US
    Posts
    126
    which means that its signedness is platform specific
    I thought that signedness was compiler specific, in which a compiler will choose which instructions to generate for an operation. Such as signed/unsigned division/multiplication (idiv/div and mul/imul).
    gorshing
    newb

  8. #8
    Join Date
    Dec 2003
    Location
    Okla, US
    Posts
    126
    Another note, this (char, signed/unsigned char are three distinct types) was explictly stated in The Annotated C++ Reference Manual by Stroustrup and Ellis. Just found out today.
    gorshing
    newb

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by gorshing
    I thought that signedness was compiler specific, in which a compiler will choose which instructions to generate for an operation. Such as signed/unsigned division/multiplication (idiv/div and mul/imul).
    I meant "implementation dependent", which roughly amounts to the same thing: a compiler isn't free to choose signedness randomly, it depends on the assembly instructions and the CPU registers of the target machine. In some cases, however, it's a programming language specific decision. The default type in PL/1 for instance is
    FIXED BINARY (31) which implies a 32-bit signed datatype.
    Danny Kalev

  10. #10
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by gorshing
    Another note, this (char, signed/unsigned char are three distinct types) was explictly stated in The Annotated C++ Reference Manual by Stroustrup and Ellis. Just found out today.
    It is stated there but I wouldn't count on the ARM as the final arbitrator on these matters since the ISO standard has changed many ARM specifications. BTW, the roots of this decision date back to ANSI C, when "signed char" was added to the language (it didn't exist in K&R).
    Danny Kalev

Similar Threads

  1. Runtime error w/ Variable Type Part II
    By jamestmfbong in forum C++
    Replies: 2
    Last Post: 05-04-2009, 06:03 PM
  2. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 2
    Last Post: 09-01-2000, 11:36 AM
  3. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 0
    Last Post: 09-01-2000, 08:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center