$ cat ./charff.cpp 
#include <limits.h>
#include <iostream>
 
int main(void){
  char a = 0xFF;
  signed char b = 0xFF;
  unsigned char c = 0xFF;
  std::cout << "char a=" << (int)a << std::endl;
  std::cout << "signed char b=" << (int)b << std::endl;
  std::cout << "unsigned char c=" << (int)c << std::endl;
  std::cout << "CHAR_BIT=" << CHAR_BIT << std::endl;
  std::cout << "CHAR_MIN=" << CHAR_MIN << std::endl;
  std::cout << "CHAR_MAX=" << CHAR_MAX << std::endl;
  std::cout << "SCHAR_MIN=" << SCHAR_MIN << std::endl;
  std::cout << "SCHAR_MAX=" << SCHAR_MAX << std::endl;
  std::cout << "UCHAR_MAX=" << UCHAR_MAX << std::endl;
  return 0;
}

コンパイルして実行。


$ g++ ./charff.cpp 
$ ./a.out 
char a=-1
signed char b=-1
unsigned char c=255
CHAR_BIT=8
CHAR_MIN=-128
CHAR_MAX=127
SCHAR_MIN=-128
SCHAR_MAX=127
UCHAR_MAX=255

環境。
OS: Mac OS X Snow Leopard


$ uname -mrsv
Darwin 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386
 
$ g++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Ref. climits (limits.h) - C++ Reference

tags: c c++

Posted by NI-Lab. (@nilab)