K& R答案书中的问题 [英] problem in K&R answer book

查看:78
本文介绍了K& R答案书中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

这是K& R给答案书中给出的程序

============= =======================

#include< stdio.h>


int main()

{

printf(" signed char =%d", - (char)((unsigned char)~0>> 1) );

printf(" signed char =%d",(char)((unsigned char)~0>> 1));

返回0;

}


============================= =====

这是打印我们可以存储在签名字符中的最大值..

这个输出是

-127

127

但是我们可以将-128作为签名字符中的值存储在< limits.h>


#define SCHAR_MIN(-128)


为什么在最着名的K& R答案书中以这种方式给出

我是错的还是程序错了..

PLZ帮帮我

问候

prem

Hi everyone
this is the program given in answer book to K&R

====================================
#include <stdio.h>

int main()
{
printf (" signed char = %d", -(char) ((unsigned char) ~0 >> 1));
printf (" signed char = %d", (char) ((unsigned char) ~0 >> 1));
return 0;
}

==================================
This is to print the maximum value we can store in a signed char..
the output of this is
-127
127
but we can store -128 as the value in a signed char and also in <limits.h>

# define SCHAR_MIN (-128)

why is it given in that way in the most famous K&R answerbook
am i wrong or the program is wrong..
plz help me
regards
prem

推荐答案

Prem Mallappa写道:
Prem Mallappa wrote:

大家好
这是K& R给答案书中给出的程序

= ===================================
#include< stdio.h>

int main()
{
printf(" signed char =%d", - (char)((unsigned char)~0>> 1));
printf(" signed char =%d",(char)((unsigned char)〜 0>> 1));
返回0;
}

===================== =============

这是打印我们可以存储在签名字符中的最大值..
这个输出是
-127
127
但是我们可以将-128作为值存储在一个有符号的char中,也可以存储在< limits.h>

#define SCHAR_MIN(-128)

为什么在最着名的K& R答案书中以这种方式给出
我错了或程序错了..
PLZ帮帮我

Hi everyone
this is the program given in answer book to K&R

====================================
#include <stdio.h>

int main()
{
printf (" signed char = %d", -(char) ((unsigned char) ~0 >> 1));
printf (" signed char = %d", (char) ((unsigned char) ~0 >> 1));
return 0;
}

==================================

This is to print the maximum value we can store in a signed char..
the output of this is
-127
127
but we can store -128 as the value in a signed char and also in <limits.h>

# define SCHAR_MIN (-128)

why is it given in that way in the most famous K&R answerbook
am i wrong or the program is wrong..
plz help me




-127是SCHAR_MIN的允许值。

我不知道你指的是哪个K& R问题。


-

pete



-127 is an allowable value for SCHAR_MIN.
I don''t know which K&R problem you are refering to.

--
pete


pete写道:
pete wrote:
Prem Mallappa写道:
Prem Mallappa wrote:

大家好
这是程序在给K& R的答案书中给出了

================================== ==
#include< stdio.h>

int main()
{
printf(" signed char =%d", - (char)((unsigned char)~0>> 1));
printf(" signed char =%d",(char)((unsigned char)〜 0>> 1));
返回0;
}

===================== =============

这是打印我们可以存储在签名字符中的最大值..
这个输出是
-127
127
但是我们可以将-128作为值存储在签名字符中,也可以存储在
< limits.h>

#define SCHAR_MIN( - 128)

为什么在最着名的K& R答案书中以这种方式给出
我错了或程序错了..
PLZ帮帮我

Hi everyone
this is the program given in answer book to K&R

====================================
#include <stdio.h>

int main()
{
printf (" signed char = %d", -(char) ((unsigned char) ~0 >> 1));
printf (" signed char = %d", (char) ((unsigned char) ~0 >> 1));
return 0;
}

==================================

This is to print the maximum value we can store in a signed char..
the output of this is
-127
127
but we can store -128 as the value in a signed char and also in
<limits.h>

# define SCHAR_MIN (-128)

why is it given in that way in the most famous K&R answerbook
am i wrong or the program is wrong..
plz help me



-127是SCHAR_MIN允许的值。
我不知道你指的是哪个K& R问题。



-127 is an allowable value for SCHAR_MIN.
I don''t know which K&R problem you are refering to.



运动C编程语言2-1:由Kernighan和Ritchie ...

- 写一个程序来确定Char,short,int的范围......两者都签了

和未签名的,通过打印批准来自标准标题的priate值和

直接计算..


Excercise 2-1 of C programming Language : by Kernighan and Ritchie...
-- Write a program to determine the ranges of Char, short, int.. both signed
and unsigned, by printing appropriate values from standard headers and by
direct computation..


Prem Mallappa写道:
Prem Mallappa wrote:
大家好。
这是给K& R答案书中给出的程序

======================== ============
#include< stdio.h>

int main()
{
printf(" signed char =%d", - (char)((unsigned char)~0>> 1));
printf(" signed char =%d",(char)((unsigned char)〜 0>> 1));
返回0;
}

===================== =============

这是打印我们可以存储在签名字符中的最大值..
这个输出是
-127
127
但是我们可以将-128作为值存储在一个有符号的char中,也可以存储在< limits.h>

#define SCHAR_MIN(-128)

为什么在最着名的K& R答案书中以这种方式给出
我错了或程序错了..
Hi everyone
this is the program given in answer book to K&R

====================================
#include <stdio.h>

int main()
{
printf (" signed char = %d", -(char) ((unsigned char) ~0 >> 1));
printf (" signed char = %d", (char) ((unsigned char) ~0 >> 1));
return 0;
}

==================================
This is to print the maximum value we can store in a signed char..
the output of this is
-127
127
but we can store -128 as the value in a signed char and also in <limits.h>

# define SCHAR_MIN (-128)

why is it given in that way in the most famous K&R answerbook
am i wrong or the program is wrong..




(1)它适合某些机器;其他人错了

(2)假设(char)与(签名字符)相同是错误的

(3)不终止最后一个是错误的带有

行终止字符(''\ n'')的输出行。


试试这个:

#include< stdio.h>

#include< limits.h>


int main()

{

printf("%d \ n",

(有符号字符)((无符号字符)1<<(CHAR_BIT - 1)));

printf(" signed char =%d",

- (signed char)((unsigned char)~0>> 1));

printf(" signed char =%d \ n",

(signed char)((unsigned char)~0>> 1));

返回0;

}


-

Martin Ambuhl



(1) It''s right for some machines; wrong for others
(2) It is wrong in assuming that (char) is the same as (signed char)
(3) It is wrong in not terminating the last line of output with a
line-termination character (''\n'').

Try this:
#include <stdio.h>
#include <limits.h>

int main()
{
printf(" %d\n",
(signed char) ((unsigned char) 1 << (CHAR_BIT - 1)));
printf(" signed char = %d",
-(signed char) ((unsigned char) ~0 >> 1));
printf(" signed char = %d\n",
(signed char) ((unsigned char) ~0 >> 1));
return 0;
}


--
Martin Ambuhl


这篇关于K&amp; R答案书中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆