Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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
当用于读取字符类型的输入时,scanf的行为不可预测。_C_Char_Scanf - Fatal编程技术网

当用于读取字符类型的输入时,scanf的行为不可预测。

当用于读取字符类型的输入时,scanf的行为不可预测。,c,char,scanf,C,Char,Scanf,我正在学习链表,当我使用scanf输入字符时,代码可以很好地编译,但在运行时它不要求输入并跳过scanf语句 #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *ptr; }; struct node* allocate(); struct node* create(); void display(struct node*); int main() {

我正在学习链表,当我使用scanf输入字符时,代码可以很好地编译,但在运行时它不要求输入并跳过scanf语句

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *ptr;
};
struct node* allocate();
struct node* create();
void display(struct node*);
int main()
{
    struct node *new;
    new=create();
    display(new);
    return 0;
}
struct node* allocate()
{
    struct node *temp;
    temp=(struct node*)malloc(sizeof(struct node));
    return temp;
}
struct node* create()
{
    struct node *start,*next;
    char ch;
    start=next=allocate();
    printf("Enter data:\n");
    scanf("%d",&start->data);
    perror("store data");
    start->ptr=NULL;
R1: printf("Do you want to enter more data? y or n::    ");
    scanf("%c", &ch); //Check for error here
    if(ch=='y'||ch=='Y')
    {
        while(ch=='y'||ch=='Y')
        {
            next->ptr=allocate();
            next=next->ptr;
            printf("Enter data:\n");
            scanf("%d",&next->data);
            next->ptr=NULL;
            printf("Do you want to enter more data? y or n::    ");
            scanf(" %c",&ch);
        }
    }    
    if(ch=='n'||ch=='N')
    {
        return start;
    }
    else
    {
        printf("Please enter correct option.\n");
        goto R1;
    }
}
void display(struct node* temp)
{
    printf("%d\n",temp->data);
    while(temp->ptr!=NULL)
    {
        temp=temp->ptr;
        printf("%d\n",temp->data);
    }      
}
当我使用getchar而不是scanf时,我面临同样的问题

ch=getchar();
当我在scanf语句中的格式说明符前面不使用空格或使用getchar()语句运行代码时,我的程序不会请求输入。它在ch中不存储任何内容。 谁能给我解释一下背后的原因吗?为什么scanf对字符数据类型的行为如此不同

其他信息:

  • 使用GCC
  • Linux内核3.6.11-4
  • OS Fedora 16(64位)
  • 英特尔i5处理器

为什么scanf在字符数据类型上的行为如此不同

scanf()
的行为不同,因为类型不同

对于像%i%u%e%f这样的数字格式说明符,
scanf()
丢弃前导空格。所以“123”和“123”都被解读为123

对于%c,
scanf()
获取输入的1字节,任何1字节,并返回它,包括空格和\0

使用%s
scanf()。它在
chars
中扫描,直到找到另一个空格


格式说明符%[…]的工作原理与%s类似,它扫描多个
字符,但“…”部分告诉您要查找什么。它不会抛出前导空格。

为什么scanf对字符数据类型的行为如此不同

 why does scanf behave so differently with character data types?
scanf()
的行为不同,因为类型不同

对于像%i%u%e%f这样的数字格式说明符,
scanf()
丢弃前导空格。所以“123”和“123”都被解读为123

对于%c,
scanf()
获取输入的1字节,任何1字节,并返回它,包括空格和\0

使用%s
scanf()。它在
chars
中扫描,直到找到另一个空格

格式说明符%[…]的工作原理与%s类似,它扫描多个
字符,但“…”部分告诉您要查找什么。它不会抛出前导空格

 why does scanf behave so differently with character data types?
这是因为scanf的行为是这样的,实际上它是一个非常复杂的函数,在必要时应该尽量避免使用它。然而,主要原因在于格式字符串中的空格意味着在下一个输入项之前跳过任何空格,除了%c

因此,
scanf(“%d%d”、&n和&m)
的行为与
scanf(“%d%d”、&n和&m)
的行为相同。对于
%c
,添加 格式字符串中的空格字符确实会有所不同。例如,如果格式字符串中的
%c
前面有空格,
scanf()
会跳到第一个非空白字符。也就是说,命令
scanf(“%c”,&ch)
读取输入中遇到的第一个字符,而
scanf(“%c”,&ch)
读取遇到的第一个非空白字符

请注意,空白也是一个字符

根据C11标准(7.21.6.2 fscanf功能,第8节)

输入空白字符(由isspace函数指定) 跳过,除非规范中包含[c, 或n说明符

这是因为
scanf
的行为是这样的,实际上它是一个非常复杂的函数,在必要时应该尽量避免使用。然而,主要原因在于格式字符串中的空格意味着在下一个输入项之前跳过任何空格,除了%c

因此,
scanf(“%d%d”、&n和&m)
的行为与
scanf(“%d%d”、&n和&m)
相同。对于
%c
,添加 格式字符串中的空格字符确实会产生影响。例如,如果格式字符串中的
%c
前面有空格,
scanf()
会跳到第一个非空白字符。也就是说,命令
scanf(“%c”,&ch)
读取输入中遇到的第一个字符,并且
scanf(“%c”,&ch)
读取遇到的第一个非空白字符

请注意,空白也是一个字符

根据C11标准(7.21.6.2 fscanf功能,第8节)

输入空白字符(由isspace函数指定) 跳过,除非规范中包含[c, 或n说明符