gpt4 book ai didi

c - 在C中获取 "highest"字符

转载 作者:行者123 更新时间:2023-11-30 18:20:03 27 4
gpt4 key购买 nike

我试图编写一个程序来获取一个人输入的最高字符。我编写了一个程序,它可以毫无问题地获得最高数量的工作,但对于字符它不起作用。这是我的代码:

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[]) {
char characters[5];
char highest = "a";

printf("Please enter five characters: \n");
for (int i = 0; i <= 4; i+=1) {
scanf("%c", characters[i]);
}

printf("These are the characters you entered: ");

for (int i = 0; i <= 4; i+=1) {
printf("%c ", characters[i]);
}

for (int i = 0; i <= 4; i+=1) {
if (characters[i] > highest) {
highest = characters[i];
}
}

printf("\nThe highest character is %c", highest);
}

我做错了什么?

最佳答案

您尝试将字符串文字 "a" 转换为 char。您应该使用字 rune 字。

char highest = "a";
//should be
char highest = 'a';

此外,scanf需要一个指针来存储读取的结果。

scanf("%c", characters[i]);
//should be
scanf("%c", &characters[i]);

Live Demo

关于c - 在C中获取 "highest"字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32588931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com