Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C扫描的字符串输入_C_String_Scanf_C11_Tr24731 - Fatal编程技术网

使用C扫描的字符串输入

使用C扫描的字符串输入,c,string,scanf,c11,tr24731,C,String,Scanf,C11,Tr24731,我一直在努力寻找答案,但我找不到答案。 我想插入一部分程序,它读取一个字符串,比如“Hello”,并存储它,并且可以在需要时显示它,这样printf(“%s”,等等)生成你好 这是给我带来麻烦的代码部分 char name[64]; scanf_s("%s", name); printf("Your name is %s", name); 我知道,printf不是问题;在提示后输入内容后,程序崩溃。请帮助?您应该这样做:scanf(“%63s”,name) 更新: 以下代码适用于我: #inc

我一直在努力寻找答案,但我找不到答案。 我想插入一部分程序,它读取一个字符串,比如“Hello”,并存储它,并且可以在需要时显示它,这样
printf(“%s”,等等)生成
你好

这是给我带来麻烦的代码部分

char name[64];
scanf_s("%s", name);
printf("Your name is %s", name);

我知道,
printf
不是问题;在提示后输入内容后,程序崩溃。请帮助?

您应该这样做:
scanf(“%63s”,name)

更新

以下代码适用于我:

#include <stdio.h> 
int main(void) { 
    char name[64]; 
    scanf ("%63s", name); 
    printf("Your name is %s", name); 
    return 0; 
}
#包括
int main(void){
字符名[64];
scanf(“%63s”,名称);
printf(“您的名字是%s”,name);
返回0;
}
如果您使用的是visual studio, 转到
项目属性->配置属性->C/C++->预处理器->预处理器定义
单击
编辑
并添加
\u CRT\u SECURE\u NO\u警告
单击确定,应用设置并再次运行

注意:这只有在你正在做作业或类似的事情时才是好的,不建议用于生产。

#include
    #include<stdio.h>

    int main()
    {
      char name[64];

      printf("Enter your name: ");
      scanf("%s", name);
      printf("Your name is %s\n", name);

     return 0;
    }
int main() { 字符名[64]; printf(“输入您的姓名:”); scanf(“%s”,名称); printf(“您的名字是%s\n”,名字); 返回0; }
来自ISO/IEC 9899:2011标准附录K.3.5.3.2中的
fscanf_s()
规范:

fscanf_s
功能等同于
fscanf
,除了
c
s
[
转换之外 说明符应用于一对参数(除非通过
*
)。这些参数中的第一个与
fscanf
的参数相同。该参数为 在参数列表中紧随其后的是第二个参数,其类型为
rsize_t
并给出第一个参数指向的数组中的元素数 如果第一个参数指向标量对象,则认为它是 一个因素

以及:

scanf_s
函数相当于
fscanf_s
,参数为
stdin
插入到
scanf\u s
的参数之前

MSDN也说了类似的事情(和)

您的代码没有提供长度参数,因此使用了其他一些数字。它不确定找到的值,因此您从代码中获得了异常行为。您需要类似的东西,换行符有助于确保实际看到输出

char name[64];
if (scanf_s("%s", name, sizeof(name)) == 1)
    printf("Your name is %s\n", name);

我在大学课堂上经常使用此选项,因此在Visual Studio中应该可以正常工作(在VS2013中测试):

char name[64];//要读取的以null结尾的字符串
scanf_s(“%63s”,名称,64);
//63=不包括“\0”的最大符号数
//64=字符串的大小;也可以使用_countof(name)代替该数字
//以这种方式调用scanf_s()将从控制台读取多达63个符号(即使您写入的符号更多),并将自动设置名称[63]='\0'
//如果实际读取的符号数<63,则“\0”将存储在字符串中的下一个空闲位置
//请注意,与gets()不同,scanf()在到达“”时停止读取(间隔,空格键),而不仅仅是换行符(回车键)
/还考虑在“(下一个)下一个扫描())之前调用“FLUHUP”(STDIN);
参考:

#包括
int main()
{
字符名[64];
printf(“输入您的姓名:”);
获取(名称);
printf(“您的名字是%s\n”,名字);
返回0;
}

scanf的“安全”版本要求您提供缓冲区的长度。只需使用
scanf()
,而不是使用此
scanf\u s()
。或者更好的是,
fgets()
。您有相同的堆栈跟踪吗?如果有,请发布。如果使用
scanf
您必须指定长度以避免缓冲区溢出:
scanf(“%63s”,name);
这对我来说很有效:,我正在使用VS2013,C程序。不是吗:scanf(“%s”,name);(否)&)此处不需要“&”。数组名称本身将给出数组的地址。
&name
上的
&
没有坏处,但不是必需的。请将其关闭,因为您了解数组和指针之间的关系。如果您不了解该关系,请阅读相关内容,然后将额外的
&
关闭。hmm。..可能是指针转换错误。在这种情况下,一个假设的编译器存储指向数组的指针与其他指针不同,可能会显示不同的行为。@dmckee:notation
&name
在问题的上下文中生成一个
char(*)[64]
)。
%s
格式需要一个
字符*
,而简单地引用
名称
就会产生一个
字符*
。正如您所看到的,这两种类型完全不同。令人遗憾的是,被视为
无效*
&name
的值与被视为无效的
名称
的值相同一个
void*
。这允许
scanf(“%63s”和&name)
工作,但不能阻止它的错误。仍然不工作,不再崩溃,但printf不会显示结果。我尝试了这个,它工作了,#include int main(void){char name[64];scanf(“%63s”,name);printf(“您的名字是%s”,name);返回0;}我使用VisualStudio,它不让我使用SCANF,只是使用SpfFis,但是它不能工作,只是跳过Prtf后缀中的%s。你开始了一个Visual C++项目?转到ProjtProjksProtoy->配置属性-> C/C++ +>预处理器>预处理器定义。点击编辑并添加设置并运行项目
scanf\u s
的第三个参数不应强制转换为
(unsigned int)
,因为
sizeof
返回类型为
size\u t
的值
char name[64]; // the null-terminated string to be read
scanf_s("%63s", name, 64);
// 63 = the max number of symbols EXCLUDING '\0'
// 64 = the size of the string; you can also use _countof(name) instead of that number
// calling scanf_s() that way will read up to 63 symbols (even if you write more) from the console and it will automatically set name[63] = '\0'
// if the number of the actually read symbols is < 63 then '\0' will be stored in the next free position in the string
// Please note that unlike gets(), scanf() stops reading when it reaches ' ' (interval, spacebar key) not just newline terminator (the enter key)
// Also consider calling "fflush(stdin);" before the (eventual) next scanf()
#include<stdio.h>

int main()
{
  char name[64];
  printf("Enter your name: ");
  gets(name);
  printf("Your name is %s\n", name);
 return 0;
}