标签: scanf

简单的C scanf不起作用?

如果我尝试以下内容:

int anint;
char achar;

printf("\nEnter any integer:");
scanf("%d", &anint);
printf("\nEnter any character:");
scanf("%c", &achar);
printf("\nHello\n");
printf("\nThe integer entered is %d\n", anint);
printf("\nThe char entered is %c\n", achar);
Run Code Online (Sandbox Code Playgroud)

它允许输入一个整数,然后scanf完全跳过第二个,这真的很奇怪,因为当我交换两个(charscanf优先)时,它工作正常.究竟是什么错了?

c console scanf

20
推荐指数
3
解决办法
4万
查看次数

使用scanf读取unsigned char

我正在尝试使用此代码读取0到255(unsigned char)之间的值.

#include<stdio.h>
int main(void)
{
    unsigned char value;

    /* To read the numbers between 0 to 255 */
    printf("Please enter a number between 0 and 255 \n");
    scanf("%u",&value);
    printf("The value is %u \n",value);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我按预期得到了以下编译器警告.

warning: format ‘%u’ expects type ‘unsigned int *’, but argument 2 has type ‘unsigned char *’

这是我对该计划的输出.

Please enter a number between 0 and 255
45
The value is 45 
Segmentation fault

运行此代码时,我确实遇到了分段错误.

unsigned char使用读取值的最佳方法是什么scanf

c scanf

20
推荐指数
1
解决办法
3万
查看次数

scanf的"正则表达式"是否支持标准?

scanf的"正则表达式"是否支持标准?我无法在任何地方找到答案.

此代码适用于gcc,但不适用于Visual Studio:

scanf("%[^\n]",a);
Run Code Online (Sandbox Code Playgroud)

它是Visual Studio错误还是gcc扩展?

编辑:看起来VS工作,但必须考虑Linux和Windows之间的行结束的差异.(\ r \n)

c gcc scanf

18
推荐指数
2
解决办法
5631
查看次数

如何从输入读取,直到使用scanf()找到换行符?

当我应该从输入读取直到有空格然后直到用户按下回车时,我被要求在C中完成工作.如果我这样做:

scanf("%2000s %2000s", a, b);
Run Code Online (Sandbox Code Playgroud)

它将遵循第一条规则而不是第二条规则.
如果我写:

I am smart

我得到的相当于:
a ="我";
b ="am";
但它应该是:
a ="我";
b ="很聪明";

我已经尝试过:

scanf("%2000s %2000[^\n]\n", a, b);
Run Code Online (Sandbox Code Playgroud)

scanf("%2000s %2000[^\0]\0", a, b);
Run Code Online (Sandbox Code Playgroud)

在第一个,它等待用户按Ctrl+ D(发送EOF),这不是我想要的.在第二个,它不会编译.根据编译器:

警告:'%['格式没有关闭']'

有什么好办法解决这个问题吗?

c format scanf

18
推荐指数
3
解决办法
11万
查看次数

我什么时候应该使用带有scanf(&)的&符号

使用时在c中使用&符有什么规则scanf()

struct Student 
{
  char name[20];
  int id;
};

int main(void)
{
  struct Student std1;
  printf("enter name and id of std1\n");
  scanf("%s %d", std1.name, &(std1.id));
}
Run Code Online (Sandbox Code Playgroud)

为什么对于String我不需要使用&符号,因为int我必须使用它?

是否有关于何时使用&符号的规则?

c string scanf

18
推荐指数
2
解决办法
3万
查看次数

为什么glibc的sscanf比Linux上的fscanf慢得多?

我在x86_64 Linux上使用GCC 4.8和glibc 2.19.

在为不同的问题使用不同的输入法时,我比较fscanfsscanf.具体来说,我要fscanf直接使用标准输入:

char s[128]; int n;

while (fscanf(stdin, "%127s %d", s, &n) == 2) { }
Run Code Online (Sandbox Code Playgroud)

或者我首先将整个输入读入缓冲区然后遍历缓冲区sscanf.(将所有内容读入缓冲区需要花费很少的时间.)

char s[128]; int n;
char const * p = my_data;

for (int b; sscanf(p, "%127s %d%n", s, &n, &b) == 2; p += b) { }
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,该fscanf版本是大大加快.例如,处理数以万计的行fscanf需要这么长时间:

10000       0.003927487 seconds time elapsed
20000       0.006860206 seconds time elapsed
30000       0.007933329 seconds time elapsed
40000       0.012881912 …
Run Code Online (Sandbox Code Playgroud)

c performance glibc scanf

18
推荐指数
2
解决办法
1984
查看次数

如何使用像scanf这样的函数使用int16_t或int32_t

我理解int16_tint32_t在C中的方式是它们在您的计算机上分别被命名为16位和32位数字.我相信当你需要保证数字为16或32位时你会使用这些,因为不同的系统并不总是代表int32位或short16位(这个假设是正确的吗?当我在网上看时,我找到了混合的答案.) .

我的问题是当我要求它们实际上是16位或32位或其他什么时,我将如何使用一个函数scanf来获取来自具有int16_t或者int32_t或任何其他类型定义数字类型的用户的输入?有某种特殊的字符串修饰符吗?通常情况下,如果我想int从一个用户那里得到一个没有关心它实际表示多大的用户,我会写这样的东西

scanf("%d", &int);
Run Code Online (Sandbox Code Playgroud)

这是有效的,如果我传入一个,int32_t但我认为它只是因为int我的系统是32位,它没有特别给我一个32位数,而只是给了我一个int.我如何获得保证为32位的数字?我查看了这个字符串修饰符和其他一些地方的页面,但没有提到这些类型定义的数字类型.

编辑:自从收到我的问题的答案后,我做了一些谷歌搜索并找到了这个.我将它包括在下面以供参考.

uppercase hexadecimal printf format for uintptr_t

#define SCNd16   "d"
decimal scanf format for int16_t

#define SCNd32   "ld"
decimal scanf format for int32_t

#define SCNd8   "hhd"
decimal scanf format for int8_t

#define SCNdFAST16   "d"
decimal scanf format for int_fast16_t

#define SCNdFAST32   "ld"
decimal scanf format …
Run Code Online (Sandbox Code Playgroud)

c user-input scanf

18
推荐指数
1
解决办法
2万
查看次数

17
推荐指数
2
解决办法
1830
查看次数

scanf()跳过变量

在C中,使用scanf()参数,scanf("%d %*d", &a, &b)行为不同.只为一个变量而不是两个变量输入值!

请解释一下!

scanf("%d %*d", &a, &b);
Run Code Online (Sandbox Code Playgroud)

c scanf

17
推荐指数
2
解决办法
3万
查看次数

如何通过fscanf找到EOF?

我在fscanf()的帮助下通过文件读取矩阵.我怎样才能找到EOF?即使我试图在arr []中捕获的每个字符串后找到EOF,那么我也无法找到它.

在计数的帮助下,我正在读取输入文件

-12 23 3

1 2 4

int main()
{
    char arr[10],len;
    int count=0;

    FILE *input= fopen("input.txt", "r");

    while(count!=7)
    {   
           fscanf(input,"%s",arr);
          //storing the value of arr in some array.
                   len=strlen(arr);
           count++;

           if(arr[len+1]==EOF)
           printf("\ni caught it\n");//here we have to exit.
    }
return 0;
}
Run Code Online (Sandbox Code Playgroud)

而不是计数我想通过EOF退出循环.怎么解决?

c scanf

17
推荐指数
2
解决办法
8万
查看次数

标签 统计

c ×10

scanf ×10

console ×1

format ×1

gcc ×1

glibc ×1

performance ×1

printf ×1

string ×1

user-input ×1