简体   繁体   中英

Is char signed or unsigned on OS X C++

Is char signed or unsigned on OS X.

I put the following snippet together to test, but was wondering how to tell for sure?

char a(0x80);           //fill most sig bit 
unsigned char b(0x80);  //fill most sig bit

cout<<"char ";
(a==b)? cout<<"is not" : cout<<"is";    //compare most sig bits in diff't chars
cout<<" signed\n";

The result was: char is signed

I'd like to know how to find this out without a piece of trick code.

Check the value of std::numeric_limits<char>::is_signed

#include <iostream>
#include <limits>

int main() {
  std::cout << "char "
            << (std::numeric_limits<char>::is_signed ? "is" : "is not")
            << " signed.\n";
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM