打印unsigned char和unsigned int的范围。 [英] Printing the range s of unsigned char and unsigned int.

查看:161
本文介绍了打印unsigned char和unsigned int的范围。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试打印unsigned char和unsigned int的范围。

char的一个工作正常,给我一个正确的输出,

但是另一个为int不,为什么?谢谢


#include< stdio.h>


main(){

unsigned char c,temp ;

c = temp = 0;

printf(无符号字符的范围是从%d到,c);

while(c> = temp){

temp = c;

++ c;

}

printf("%d \ n",temp);

返回0;

}


输出:范围$ unsgot char从0到255

#include< stdio.h>


main(){

unsigned int c,temp;

c = temp = 0;

printf(" unsigned int的范围是从%d到",c);

while(c> = temp){

temp = c;

++ c;

}

printf("%d \ n",temp);

返回0;

}


OUTPUT:unsigned int的范围是从0到-1


-

通过 http://www.teranews.com

Hello, I am trying to print the range of unsigned char and unsigned int.
The one for char is working good, gives me a correct output,
however the other one for int doesnt, why?? Thanks

#include <stdio.h>

main(){
unsigned char c, temp;
c = temp = 0;
printf("the range of unsigned char is from %d to ", c);
while(c >= temp){
temp = c;
++c;
}
printf("%d\n", temp);
return 0;
}

OUTPUT: the range of unsigned char is from 0 to 255

#include <stdio.h>

main(){
unsigned int c, temp;
c = temp = 0;
printf("the range of unsigned int is from %d to ", c);
while(c >= temp){
temp = c;
++c;
}
printf("%d\n", temp);
return 0;
}

OUTPUT: the range of unsigned int is from 0 to -1

--
Posted via a free Usenet account from http://www.teranews.com

推荐答案

" Junmin H." ; < ti ***** @ gmail.comwrites:
"Junmin H." <ti*****@gmail.comwrites:

您好,我正在尝试打印unsigned char和unsigned int的范围。

char的一个工作正常,给我一个正确的输出,

然而另一个为int不,为什么?谢谢
Hello, I am trying to print the range of unsigned char and unsigned int.
The one for char is working good, gives me a correct output,
however the other one for int doesnt, why?? Thanks



[snip]

[snip]


#include< stdio.h>


main(){

unsigned int c,temp;

c = temp = 0;

printf(" unsigned int的范围是从%d到,c);

while(c> = temp){

temp = c;

++ c;

}

printf("%d \ n",temp);

返回0;

}


OUTPUT:unsigned int的范围是0到-1
#include <stdio.h>

main(){
unsigned int c, temp;
c = temp = 0;
printf("the range of unsigned int is from %d to ", c);
while(c >= temp){
temp = c;
++c;
}
printf("%d\n", temp);
return 0;
}

OUTPUT: the range of unsigned int is from 0 to -1



" ;%d" format用于signed int。使用%u。


顺便提一句,你应该使用''int main(void)''而不是''main()''。


如果你的目标只是打印unsigned int的范围,你可以使用UINT_MAX,在< limits.h>中定义。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http ://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

The "%d" format is for signed int. Use "%u".

Incidentally, you should use ''int main(void)'' rather than ''main()''.

And if your goal is merely to print the range of unsigned int, you can
use UINT_MAX, defined in <limits.h>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Junmin H.写道:
Junmin H. wrote:

您好,我正在尝试打印unsigned char和unsigned int的范围。

char的一个工作正常,给我一个正确的输出,

然而另一个为int不,为什么?谢谢
Hello, I am trying to print the range of unsigned char and unsigned int.
The one for char is working good, gives me a correct output,
however the other one for int doesnt, why?? Thanks



因为你错误地使用了%d,一个有符号的int的说明符,要打印一个unsigned int。实际上,在您的实现中,UINT_MAX的位模式被解释为已签名的

int -1的位模式。有关使用正确说明符的示例,请参阅以下内容:


#include< stdio.h>

#include< limits.h>


int main(无效)

{

printf(" [output] \\\
"

unsigned char的范围是从0到%u \ n 

" unsigned int的范围是从0到%u \ n,UCHAR_MAX,

UINT_MAX);

返回0;

}


[输出]

unsigned char的范围是0到255

unsigned int的范围是0到4294967295

Because you incorrectly used "%d", the specifier for a signed int, to
print an unsigned int. As it happens, on your implementation the bit
pattern for UINT_MAX is interpreted as the bit pattern for the signed
int -1. For examples of using the correct specifier, see below:

#include <stdio.h>
#include <limits.h>

int main(void)
{
printf("[output]\n"
"the range of unsigned char is from 0 to %u\n"
"the range of unsigned int is from 0 to %u\n", UCHAR_MAX,
UINT_MAX);
return 0;
}

[output]
the range of unsigned char is from 0 to 255
the range of unsigned int is from 0 to 4294967295


>

#include< stdio.h>


main(){

unsigned char c,temp;

c = temp = 0;

printf(无符号字符的范围是从%d到,c);

while(c> = temp){

temp = c;

++ c;

}

printf("%d \ n" ,临时);

re转0;

}


输出:无符号字符的范围是0到255



#include< stdio.h>


main(){

unsigned int c,temp;

c = temp = 0;

printf(unsigned int的范围是从%d到,c);

while(c> = temp){

temp = c;

++ c;

}

printf("%d \ n", temp);

返回0;

}


输出:unsigned int的范围是0到-1
>
#include <stdio.h>

main(){
unsigned char c, temp;
c = temp = 0;
printf("the range of unsigned char is from %d to ", c);
while(c >= temp){
temp = c;
++c;
}
printf("%d\n", temp);
return 0;
}

OUTPUT: the range of unsigned char is from 0 to 255

#include <stdio.h>

main(){
unsigned int c, temp;
c = temp = 0;
printf("the range of unsigned int is from %d to ", c);
while(c >= temp){
temp = c;
++c;
}
printf("%d\n", temp);
return 0;
}

OUTPUT: the range of unsigned int is from 0 to -1


" Junmin H."写道:
"Junmin H." wrote:

>

您好,我正在尝试打印unsigned char范围和未签名的

INT。 char的一个工作正常,给我一个正确的输出,

然而另一个为int不,为什么?谢谢
>
Hello, I am trying to print the range of unsigned char and unsigned
int. The one for char is working good, gives me a correct output,
however the other one for int doesnt, why?? Thanks



.... snip char version ...

.... snip char version ...


>

#include< stdio.h>


main(){

unsigned int c,temp;

c = temp = 0;

printf(" unsigned int的范围是从%d到,c);

while(c> = temp){

temp = c;

++ c;

}

printf("%d \ n" ,temp);

返回0;

}


OUTPUT:unsigned int的范围是0到-1
>
#include <stdio.h>

main(){
unsigned int c, temp;
c = temp = 0;
printf("the range of unsigned int is from %d to ", c);
while(c >= temp){
temp = c;
++c;
}
printf("%d\n", temp);
return 0;
}

OUTPUT: the range of unsigned int is from 0 to -1



您未能正确键入printf参数。

以下工作正常,并且是一个刚刚修改过的版本

您的代码使用简短并避免无休止的延迟。它

输出65535.注意unsigned int的最大值是

通常不能表示为int,并试图这样做

涉及未定义的行为。


#include< stdio.h>


int main(无效){

unsigned short c,temp;


c = temp = 0;

printf(" unsigned int的范围是从%d到",(int )c);

while(c> = temp){

temp = c;

++ c;

}

printf("%d \ n",(int)temp);

返回0;

}


-

Chuck F(cinefalconer at maineline dot net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>

-

通过 http://www.teranews.com

You have failed to correctly type the printf argument. The
following works fine here, and is a just barely modified version of
your code to use a short and avoid an interminable delay. It
outputs 65535. Note that the maximum value of an unsigned int is
not normally expressible as an int, and attempting to do so
involves undefined behaviour.

#include <stdio.h>

int main(void) {
unsigned short c, temp;

c = temp = 0;
printf("the range of unsigned int is from %d to ", (int)c);
while (c >= temp) {
temp = c;
++c;
}
printf("%d\n", (int)temp);
return 0;
}

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com


这篇关于打印unsigned char和unsigned int的范围。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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