asuerhao's Blog

如果有什么做的不到的地方请尽管留言, 我会改进的 : )

C语言 char型也是分有符号数和无符号数的

asuerhao posted @ 2012年4月18日 01:25 in 积累 with tags c char类型 , 6015 阅读

  unsigned char型表示无符号数, 取值范围是 0~255.
  signed char型表示无符号数, 取值范围是 -128~127.

  在x86平台上, gcc规定不带'unsigned'或'signed'关键字的char是有符号的. 参见以下代码 :

#include<stdio.h>
int main(void)
{
	printf("(signed char)200: %d\n", (signed char)200);
	printf("(unsigned char)200: %d\n", (unsigned char)200);
	printf("(char)200: %d\n", (char)200);	// gcc定义char型是有符号的。
	return 0;
}


打印结果为:

(signed char)200: -56
(unsigned char)200: 200
(char)200: -56

  利用objdump工具对其进行反汇编, 观察向printf函数传递的第二个参数, 节选部分汇编代码:
    printf("(signed char)200: %d\n", (signed char)200);
 80483cd:    b8 e0 84 04 08                mov    $0x80484e0,%eax
 80483d2:    c7 44 24 04 c8 ff ff        movl   $0xffffffc8,0x4(%esp)
 80483d9:    ff
 80483da:    89 04 24                            mov    %eax,(%esp)
 80483dd:    e8 12 ff ff ff                    call   80482f4 <printf@plt>
    printf("(unsigned char)200: %d\n", (unsigned char)200);
 80483e2:    b8 f6 84 04 08                mov    $0x80484f6,%eax
 80483e7:    c7 44 24 04 c8 00 00    movl   $0xc8,0x4(%esp)
 80483ee:    00
 80483ef:    89 04 24                            mov    %eax,(%esp)
 80483f2:    e8 fd fe ff ff                     call   80482f4 <printf@plt>
    printf("(char)200: %d\n", (char)200);    // gcc定义char型是有符号的。
 80483f7:    b8 0e 85 04 08                mov    $0x804850e,%eax
 80483fc:    c7 44 24 04 c8 ff ff         movl   $0xffffffc8,0x4(%esp)
 8048403:    ff
 8048404:    89 04 24                            mov    %eax,(%esp)
 8048407:    e8 e8 fe ff ff                    call   80482f4 <printf@plt>

  还有一个问题需要注意, 从汇编代码可以看到, 实际上用了四个字节存储char类型的参量, 即: 像printf这种形参类型未知的函数, 调用函数时编译器会自动对相应的实参做Integer Promotion.

Avatar_small
cleaning companies i 说:
2019年9月15日 20:03

Competent services belonging to the trained and even experienced team members bestowed them given that the best maintaining company on Dubai. You certainly will agree that many type in cleaning service varies from once more. Therefore, the product demands an experienced person in the fields developing sufficient experience with the relevant domain of product.

Avatar_small
isellpittsburghhomes 说:
2019年11月14日 01:07

Bill financing is just about the most common route to finance your internet business. You get rid of a lending product and will repay over a number of time. The lender charges appeal and will make a profit in which. The most commonly seen business lenders might possibly be the following:

Avatar_small
deep cleaning servic 说:
2020年2月22日 22:02

Our team of cleanup experts perform inside a variety of residential along with commercial spots including condominiums, villas, office buildings, malls, health care centers, hostipal wards, hotels, educational facilities, warehouses, merchants, showrooms plus much more. Each person in our assistance staff is usually trained to look at extra proper care when going around your place or handling your possessions. Your household, workspace along with property will be in good arms.

Avatar_small
maid agency dubai 说:
2020年4月28日 20:03

It may well not get an exaggeration to speak about that Individuals like Bloody halloween. It is certainly clear that many of us love it all! People of the ages, races, economic status, creeds together with temperaments most of enjoy celebrating Halloween of hospitality attire form or simply another. Decorating the place, hosting a good Halloween occasion, going fool or curing, and carving jack-o-lanterns are very proof for this. What the majority adults enjoy stands out as the excuse to build dressed away as something different from us.

Avatar_small
wall painting servic 说:
2020年4月28日 20:03

Utilizing better real estate and gdns picture electrician software, it's possible you'll repaint together with remake all assorted types of surfaces in your own virtual your home. This may be a faster together with easier option to get an overall view of the home projects finalized look. It can save you time together with money while you need possibly not redo any projects many times. In a better way, it will let you view your household plan earlier than pouring in hard earned money just of having it changed afterward.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter