gpt4 book ai didi

c - 字符串的简单字符输出

转载 作者:行者123 更新时间:2023-11-30 18:37:52 25 4
gpt4 key购买 nike

我正在学习 C,我只是想输出用户输入的字符串的第一个字符。不知何故它不起作用?我也没有收到错误消息。这一定是一个非常简单的问题,但我不明白。

#include <stdio.h>

int main(void)
{
char input[200];
char test;
printf("Text input: ");
scanf("%s", input);
test = input[0];
printf("%s", test);
return 0;
}

最佳答案

您需要使用%c来打印char%s 用于以 null 结尾的字符串,即 char 数组。下面的代码对我来说效果很好。

#include <stdio.h>

int main() {
char input[200];
char test;
printf("Text input: ");
scanf("%s", input);
test = input[0];
printf("%c\n", test);
return 0;
}

关于c - 字符串的简单字符输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518755/

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