忽略“否定扫描集”的输入字符。规范,存入数据缓冲区,用C编程? [英] Do ignored input characters for "negated scanset" specification, get stored in data buffer, while programming in C ?
问题描述
#include< stdio.h>
main()
{
char name [ 15 ],年龄[ 3 ];
printf( 请输入你的名字:\ n);
scanf( %[^ $],& name);
printf( 您的名字是%s。\ n,名称);
printf( 请输入您的年龄:\ n);
scanf( %s,& age);
printf( 您的年龄是%s \ n,年龄);
getch();
}
输入:
Shubham $$
输出:
您的名字是Shubham。
请输入您的年龄:
您的年龄是$$
从输出中可以清楚地看到,即使被拒绝的无效字符(即$)未显示在输出中,但它们已经存储到数据缓冲区中,因此的下一个输入年龄是缓冲区中剩余的数据,即$$。
我的问题:我想确认在每种情况下都会出现这种情况。是否,被拒绝的字符总是如此存储到数据缓冲区?
我尝试过:
我已经在2-3个程序中尝试了它但是我想知道这是否总是如此?
每次我们使用否定扫描集时,被拒绝的字符都存储在缓冲区中。
,& name);
printf( < span class =code-strin g>你的名字是%s。\ n,名字);
printf( 请输入您的年龄:\ n);
scanf( %s,& age);
printf( 您的年龄是%s \ n,年龄);
getch();
}
输入:
Shubham
输出:
您的名字是Shubham。
请输入您的年龄:
您的年龄是
从输出中可以看出,即使被拒绝的无效字符(即
#include<stdio.h>
main()
{
char name[15],age[3];
printf("Please enter your name :\n");
scanf("%[^$]",&name);
printf("Your name is %s.\n",name);
printf("Please enter your age :\n");
scanf("%s",&age);
printf("Your age is %s\n",age);
getch();
}
Input :
Shubham$$
Output :
Your name is Shubham.
Please enter your age :
Your age is $$
From the output, it is clear that even though the rejected invalid character(i.e.,$) is not shown in the output, but they have been stored to the data buffer, so the next input for age is the data left in buffer i.e.,$$.
My Question : I want to confirm that is this behaviour expected in every case.i.e., Does the rejected characters always get stored to the data buffer ?
What I have tried:
I have tried it in 2-3 programs but i want to know whether is this always the case ?
That the rejected characters are stored in the buffer every time we use negated scanset.
",&name); printf("Your name is %s.\n",name); printf("Please enter your age :\n"); scanf("%s",&age); printf("Your age is %s\n",age); getch(); }
Input :
Shubham
Output :
Your name is Shubham.
Please enter your age :
Your age is
From the output, it is clear that even though the rejected invalid character(i.e.,
这篇关于忽略“否定扫描集”的输入字符。规范,存入数据缓冲区,用C编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!