#include <stdio.h>
int main(void)
{
int i,j,k;
char st;
printf("enter string\n");
scanf("%s", st);
printf("the entered string is %s\n", st);
}编写上面的程序给我一个警告:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]
palindrom.c:8:1: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]我在这里做错什么了?
当我运行它时会发生这样的情况:
$ ./a.out
enter string
kiaaa
the entered string is (null)编辑:
下面是代码的另一个版本(将char st;转换为char *st):
#include <stdio.h>
int main(void)
{
int i,j,k;
char *st;
printf("enter string\n");
scanf("%s", st);
printf("the entered string is %s\n", st);
}但是,它在运行时的行为是相同的。
发布于 2012-05-24 06:08:27
你的类型不匹配。
scanf不是类型安全的,您需要提供正确的类型。scanf使您能够从输入中获取数据,并且需要告诉它要读取的数据的类型。通过为字符串提供一个字符变量来指定%s,让它读取字符串。
你需要一个数组:
#define MAX_LENGTH 256
char st[MAX_LENGTH];正如曾傑瑞正确指出的那样,你只需简单地避免所有的麻烦,只需使用:
getline()而不是使用scanf
https://stackoverflow.com/questions/10732010
复制相似问题