C 中 char、signed char 和 unsigned char 的区别
C 中 char、signed char 和 unsigned char 的区别
来源:http://bbs.chinaunix.net/thread-889260-1-1.html
参考:https://publications.gbdirect.co.uk//c_book/chapter2/integral_types.html
ANSI C 提供了3种字符类型,分别是char、signed char、unsigned char
char相当于signed char或者unsigned char,但是这取决于编译器!
这三种字符类型都是按照1个字节存储的,可以保存256个不同的值。
signed char取值范围是 -128 到 127
unsigned char 取值范围是 0 到 255
但是char究竟相当于signed char呢还是相当于unsigned char呢??
这就是char和int的不同之处!
int == signed int,但是char不能简单以为 == signed char
要确定char究竟等同什么要基于不同的编译器做测试
大多数机器使用补码来存储整数,在这些机器中按照整数类型存储的-1的所有位均是1
假设我的机器也是如此存储,就能据此判断char究竟是等于signed char还是unsigned char
编译器提供以下参数来对char类型进行设置,
-funsigned-char : 设置为 unsigned char
-fno-signed-char : 设置为 非 signed char
-fsigned-char : 设置为 signed char
-fno-unsigned-char : 设置为 非 unsigned char
limits.h
/* Number of bits in a `char'. */
# define CHAR_BIT /* Minimum and maximum values a `signed char' can hold. */
# define SCHAR_MIN (-)
# define SCHAR_MAX /* Maximum value an `unsigned char' can hold. (Minimum is 0.) */
# define UCHAR_MAX /* Minimum and maximum values a `char' can hold. */
# ifdef __CHAR_UNSIGNED__
# define CHAR_MIN
# define CHAR_MAX UCHAR_MAX
# else
# define CHAR_MIN SCHAR_MIN
# define CHAR_MAX SCHAR_MAX
# endif
/* The character type that char matches (i.e., signed or unsigned)
*/
#if CHAR_MIN < 0
typedef signed char underlying_char_type;
#else
typedef unsigned char underlying_char_type;
#endif
test_char.c
#include <stdio.h> int main(void)
{
char a = -;
signed char b = -;
unsigned char c = -; printf("a=%d, b=%d, c=%d\n", a, b, c);
return ;
}
output
# ./test_char.elf
a=-, b=-, c=255 // 从这里可以看出 默认的 char 类型就是 signed char 类型
============ End
C 中 char、signed char 和 unsigned char 的区别的更多相关文章
- 图像处理中像素点的问题:unsigned char 和 char
以前在做图像处理的时候,一直不太在意这个问题,对图像每个像素点的灰度值,总是认为char也可,unsigned char也可.尽管它们都是8位,但是表示的数的范围却不相同:char: -128~127 ...
- C语言char*字符串数组和unsigned char[]数组的相互转换
#include <iostream> #include <string> using namespace std; void convertUnCharToStr(char* ...
- signed char、unsigned char
什么是无符号char类型?与常见的char类型有何不同? 在c++中有三种不同的字符类型:char,signed char,unsigned char.如果要应用与文本字符,就使用不加限制的char类 ...
- char、signed char、unsigned char的区别总结。
转载地址:http://hi.baidu.com/thewillreigns/blog/item/67e665c4296e69c038db492d.html char 和 unsigned char是 ...
- 关于 char 和 unsigned char 的区别
首先卖个关子: 为什么网络编程中的字符定义一般都为无符号的字符? char buf[16] = {0}; unsigned char ubuf[16] = { 0 }; 上面两个定义的区别是: ...
- QString unsigned char* 的转换
QString -> unsigned char* : QString str = "ABCD"; int length = str.length(); unsigned ...
- Integral Promotions整数提升和符号扩展(char,unsigned char,signed char)
以下来自msdn: Objects of an integral type can be converted to another wider integral type (that is, a ty ...
- char、signed char 和 unsigned char 的区别
ANSI C 提供了3种字符类型,分别是char.signed char.unsigned char.而不是像short.int一样只有两种(int默认就是signed int). 三者都占1个字节( ...
- char , unsigned char 和 signed char 区别
ANSI C 提供了3种字符类型,分别是char.signed char.unsigned char.char相当于signed char或者unsigned char,但是这取决于编译器!这三种字符 ...
随机推荐
- 提高组刷题班 DAY 1 上午
低仿机器人(robo,1s,64M) 题解 大模拟 代码 #include <cstdio> #include <cstring> #include <iostream& ...
- bpi English
一.Marketing and Management Dashboard 营销管理 1.non-stackable voucher 不可累计的券 2.Campaign engine 活动引擎 3.i ...
- buildscript和allprojects的作用和区别是什么?
在Android Studio的Project的build.gradle中, // Top-level build file where you can add configuration optio ...
- Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例
Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和WeUI的组件库 ...
- js对象和jQuery对象的区别
(1)js对象的三种基本定位方式 (A)通过ID属性:document.getElementById() (B)通过NAME属性:document.getElementsByName() (C)通过标 ...
- manage.py migrate 报错
第一个提示,setting里面的 STATICFILES_DIRS = ( os.path.join(BASE_DIR,'static')) 第二行的后面加','解决,这样可以被识别是tuple. ...
- Linux安装配置JDK1.8
JDK1.8 链接:http://pan.baidu.com/s/1nvGBzdR 密码:ziqb 1 在/usr/local 文件夹下新建一个文件夹software ,将JDK放到此文件夹中 ...
- 【OSS】工具类
package com.aliyun.oss.demo; import com.aliyun.oss.ClientException; import com.aliyun.oss.OSSClient; ...
- 如何创建一个img文件并且mount 它
https://ubuntuhak.blogspot.com/2012/10/how-to-create-format-and-mount-img-files.html
- Kubernetes-kubectl The connection to the server localhost:8080 was refused -did you specify
今天在Kubernetes的从节点上运行命令[kubectl]出现了如下错误: [root@k8snode1 kubernetes]# kubectl get pod The connection t ...