当我使用 scanf_s 时我得到 4,但当我使用 fgets() 时得到 1
I get 4 when I use scanf_s but 1 when I use fgets()?
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
printf("enter the stringn");
//scanf_s("%[^n]s", s, sizeof(s));
fgets(s, sizeof*(s), stdin);
for (i = 0;s[i] != ' ';i++)
{
if (s[i] == ' ')
count++;
}
printf("number of words in given string are: %dn", count + 1);
getchar();
}
fgets(s, sizeof*(s), stdin);
-- 是错误的。 应该是sizeof(s)
*(s) 是字符(数组的第一个元素),而 s 是数组。因为单个字符的大小为 1,所以得到 1。
相关文章:
- scanf() 只取数字作为输入,避免使用其他字符
- 为什么我的 scanf() 没有在我的数组上迭代我的 for 循环?
- 当我尝试使用它时,Scanf 会抛出一个随机异常(scanf_s 也是如此)
- 如何通过 scanf() 读取没有标点符号的字符串?
- 为什么这个scanf()的两个不同的输入会给出两个不同的输出?
- C++ - scanf() 和 printf() 执行顺序不对
- scanf( "%d" , array[i] + 1)是什么意思?为什么+1在那里?
- SCANF 读取错误,因为使用 %d 读取布尔值
- 为什么 printf 在 C++ 中的执行速度比 cout 快?另外scanf比cin慢,为什么?
- scanf() 的宽度说明符 - 要使用的字符长度在编译时不固定,仅在运行时确定.如何使其可变?
- 如何在循环中使用scanf,将值存储到一个变量中,然后打印出来?
- 如何使用 FGETS 获取文本,并保存 std::string
- 在C++中使用哪个函数来输入多个单词,比如C中的fgets
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- 在C/C++中检查数字是否为整数(不带scanf/gets/etc)
- 为什么我应该在scanf()-家族成员中包含一个长度修饰符作为参数?有什么好处?使用长度修改器进行扫描的作用
- 在printf和scanf中%lli和%lld之间的区别?
- 不能使用嵌套结构中的联合元素作为 scanf() 的参数来存储所需的值
- 使用 scanf 读取文件中的图形时出现问题
- 使用 fgets() 从键盘读取时从以前的输入读取换行符