关于char越界的简单c

来源:互联网 发布:魔法王座升阶数据图片 编辑:程序博客网 时间:2024/05/24 15:39

结论:

1. char 有符号范围是-128~127;无符号范围是0~255;

2. 在存储媒介中,数字都是补码形式储存的(含字符常量);

3. 正数补码为本身二进制,负数补码:绝对值->取反->+1;

4. 补码的补码为源码:原码求补码是取反加1,补码求原码还是是取反加1(符号位除外) !


例子:

root@ubuntu:/mnt/temp/test_8.31# gdb test<span style="white-space:pre"></span><strong><span style="color:#ff0000;">//gdb调试</span></strong>GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1Copyright (C) 2014 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.  Type "show copying"and "show warranty" for details.This GDB was configured as "i686-linux-gnu".Type "show configuration" for configuration details.For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>.Find the GDB manual and other documentation resources online at:<http://www.gnu.org/software/gdb/documentation/>.For help, type "help".Type "apropos word" to search for commands related to "word"...Reading symbols from test...done.(gdb) list<span style="white-space:pre"></span><strong><span style="color:#ff0000;">//源码</span></strong>1#include <stdio.h>2#include <stdlib.h>34int main()5{6char a,b,c1,c2;7a = 197;8b = 198;910printf("%c,%c\n",a,b);(gdb) list11printf("%d,%d\n",a,b);12exit(EXIT_SUCCESS);13}(gdb) break 10<span style="white-space:pre"></span><span style="color:#ff0000;"><strong>//断点1为第10行</strong></span>Breakpoint 1 at 0x8048460: file test.c, line 10.(gdb) break 11<span style="white-space:pre"></span><strong><span style="color:#ff0000;">//断点2为第11行</span></strong>Breakpoint 2 at 0x804847e: file test.c, line 11.(gdb) r<span style="white-space:pre"></span>//运行Starting program: /mnt/temp/test_8.31/test Breakpoint 1, main () at test.c:1010printf("%c,%c\n",a,b);(gdb) cont<span style="white-space:pre"></span><strong><span style="color:#ff0000;">//断点1运行结果</span></strong><span style="white-space:pre"></span>Continuing.�,Breakpoint 2, main () at test.c:1111printf("%d,%d\n",a,b);(gdb) cont<span style="white-space:pre"></span><strong><span style="color:#ff0000;">//断点2运行结果</span></strong>Continuing.-59,-58<span style="white-space:pre"></span><span style="color:#ff0000;">//197 的补码是11000101,最高位是1,所以是负数,剩1000101(补码),求源码为111011,为59,加符号位"1",就是-59</span>[Inferior 1 (process 19127) exited normally]





0 0