[英]How do you convert an int to it's respective ASCII letter in C?
我挑战自己制作一个需要 int 的程序,它必须打印相应的 ASCII 字母,如下所示:如果数字是 65,它应该打印出A
等等。 但我不知道如何做到这一点。 到目前为止,这是我的代码:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
for (int i = 65; i < 81; i++)
{
printf("%s\n", i);
}
}
我需要在string b = NULL;
之间的位置添加某种函数string b = NULL;
和printf("%s\\n", b);
但我不知道我应该使用哪个功能。 最后,它应该按顺序打印出字母表中的所有字母,每个字母都有自己的一行。 如果您碰巧知道可以帮助我的功能,请告诉我。
聚苯乙烯
如果我按照上面的示例进行操作,则会收到以下错误消息:
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow try.c -lcrypt -lcs50 -lm -o try
try.c:12:18: error: format specifies type 'char *' but the argument has type 'int' [-Werror,-Wformat]
printf("%s\n", i);
~~ ^
%d
1 error generated.
<builtin>: recipe for target 'try' failed
make: *** [try] Error 1
最后,它应该按顺序打印出字母表中的所有字母,每个字母都有自己的一行。
你真的不需要一个函数来做到这一点。 string
也是错误的选择,因为您只想打印字符。 这for
可能做你希望做什么:
for (int i = 0; i < 26; ++i)
{
printf("%c\n", i + 'a');
}
我用过这个:
int integer = // your int ;
char c = integer;
它非常简单,并使其成为一个字符。
此外,您可以使用<ctype.h>
isascii
检查结果
我不认为你需要任何东西。 当您使用 %s 时,它应该自动将整数打印为字符。
简单地做:
for(int i = 0; i < 26; i++)
{
printf("%s\n", i);
}```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.