用char计数 [英] counting with char

查看:88
本文介绍了用char计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想为我的系统打印一个字符表及其值,如

65:A

66:B

aso。

从0到2​​55开始。


我是否应该使用unsigned char为此,

每次增加它?使用签名的字符,或只是

普通字符(在我的系统上签名)只会使事情变得困难,因为我需要计算负值。


但问题是for循环。

这两个:


unsigned char c;

for(c = 0; c <256; c ++)




for(c = 0; c< = 255; c ++)


将导致无限循环,因为c不能大于255.


我该如何解决这个?我是否必须为计数器使用除

char之外的其他数据类型?或者我能以某种方式将其投射到比较中吗?


哪种方式最好去?

Hello,

I want to print a table of characters and their values for my system like
65: A
66: B
aso.
starting from 0 to 255.

Am I rigth that I should use an unsigned char for this,
incrementing it each time? Using an signed char, or just
plain char (which is signed on my system) would just make
things difficult since I need to count with negative values.

But then the problem is the for loop.
Both this:

unsigned char c;
for (c = 0; c < 256; c++)

and

for (c = 0; c <= 255; c++)

will result in an infinite loop since c can''t be larger than 255.

How can I tackle this? Do I have to use an other datatype than
char for the counter? Or can I cast it at the comparision somehow?

Which way is the best to go?

推荐答案

eiaks说:
eiaks said:
你好,

我想为我的系统打印一个字符表及其值,如
65:A
66:B
aso。
从0到255开始。

我是否应该使用unsigned char这个,
每次递增它?使用签名的char,或只是简单的char(在我的系统上签名)只会使事情变得困难,因为我需要计算负值。
Hello,

I want to print a table of characters and their values for my system like
65: A
66: B
aso.
starting from 0 to 255.

Am I rigth that I should use an unsigned char for this,
incrementing it each time? Using an signed char, or just
plain char (which is signed on my system) would just make
things difficult since I need to count with negative values.



只需使用int。所有字符都可以表示为整数。


建议:在决定是否将字符写入

标准输出流之前,将其推送到isprint( ),像这样:


/ * ch是一个int * /

if(isprint((unsigned char)ch))

{

无论如何都要将角色显示为角色

}

其他

{

显示某些字符不可打印的指示

}


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



Just use int. All characters are representable as ints.

A word of advice: before deciding whether to write a character to your
standard output stream, shove it through isprint(), like this:

/* ch is an int */
if(isprint((unsigned char)ch))
{
display the character as a character by all means
}
else
{
display some kind of indication that the character is not printable
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)




" ; eiaks" < JA ** @ hotmail.com>在消息中写道

news:xG ******************* @ newsb.telia.net ...

"eiaks" <ja**@hotmail.com> wrote in message
news:xG*******************@newsb.telia.net...
你好,

我想为我的系统打印一个字符表及其值,如
65:A
66:B
aso。
启动从0到255.

我是否应该为此使用unsigned char,
每次递增?使用签名的char,或只是简单的char(在我的系统上签名)只会使事情变得困难,因为我需要用负值来计算。

但是问题是for循环。
这两个:

unsigned char c;
for(c = 0; c <256; c ++)

并且

for(c = 0; c <= 255; c ++)
将导致无限循环,因为c不能大于255.


您可以倒数:


#include< ctype.h>

#include< limits.h> ;

#include< stdio.h>


int main(无效)

{

unsigned char c = UCHAR_MAX;

const char unprintable =''#'';

unsigned char tmp = 0;


而(c)

{

tmp = UCHAR_MAX - c;

printf("%d:%c\ n",

tmp,

isprint(tmp)?(char)tmp:unprintable);

--c;

}


retu 0;

}

我该如何解决这个问题?我是否必须为计数器使用除
char之外的其他数据类型?


如果您需要使用超出其范围的值,您必须:


int main(无效)

{

unsigned int c = 0;

const char unprintable =''#'';


for(c = 0 ; c< UCHAR_MAX; ++ c)

{

printf("%3d:%c \ n",

c, isprint(c)?(char)c:unprintable);

}


返回0;

}


或者我能以某种方式将它投射到比较中吗?


施法不能改变类型的范围。

总是看到怀疑施放的欲望。

哪种方式是最好去?
Hello,

I want to print a table of characters and their values for my system like
65: A
66: B
aso.
starting from 0 to 255.

Am I rigth that I should use an unsigned char for this,
incrementing it each time? Using an signed char, or just
plain char (which is signed on my system) would just make
things difficult since I need to count with negative values.

But then the problem is the for loop.
Both this:

unsigned char c;
for (c = 0; c < 256; c++)

and

for (c = 0; c <= 255; c++)

will result in an infinite loop since c can''t be larger than 255.
You could count backwards:

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

int main(void)
{
unsigned char c = UCHAR_MAX;
const char unprintable = ''#'';
unsigned char tmp = 0;

while(c)
{
tmp = UCHAR_MAX - c;
printf("%d: %c\n",
tmp,
isprint(tmp) ? (char)tmp : unprintable);
--c;
}

return 0;
}
How can I tackle this? Do I have to use an other datatype than
char for the counter?
You must if you need to use a value outside its range:

int main(void)
{
unsigned int c = 0;
const char unprintable = ''#'';

for(c = 0; c < UCHAR_MAX; ++c)
{
printf("%3d: %c\n",
c, isprint(c) ? (char)c : unprintable);
}

return 0;
}

Or can I cast it at the comparision somehow?
Casting cannot change the range of a type.
Always view a desire to cast with suspicion.
Which way is the best to go?




我会选择第二个例子; imo it's more

''直截了当'。


-Mike



I''d go with my second example; imo it''s more
''straightforward''.

-Mike




" Mike Wahler" < MK ****** @ mkwahler.net>在消息中写道

新闻:做***************** @ newsread2.news.pas.earthl ink.net ...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:do*****************@newsread2.news.pas.earthl ink.net...

int main(void)
{
unsigned int c = 0;
const char unprintable =''#'';

for (c = 0; c

int main(void)
{
unsigned int c = 0;
const char unprintable = ''#'';

for(c = 0; c < UCHAR_MAX; ++c)




for(c = 0; c< = UCHAR_MAX; ++ c)


-Mike



for(c = 0; c <= UCHAR_MAX; ++c)

-Mike


这篇关于用char计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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