scanf的高级输入
来源:互联网 发布:江苏瑞中数据 编辑:程序博客网 时间:2024/05/16 14:08
The scanf function pieces tips:
sequence of characters can be input using a scan set. A scan set is a set of charactersenclosed in square brackets, [] , and preceded by a percent sign in the format control
string. A scan set scans the characters in the input stream, looking only for those characters
that match characters contained in the scan set. Each time a character is matched, it’s
stored in the scan set’s corresponding argument—a pointer to a character array. The scan
set stops inputting characters when a character that is not contained in the scan set is
encountered. If the first character in the input stream does not match a character in the
scan set, only the null character is stored in the array. An example as followed uses the
scan set [aeiou] to scan the input stream for vowels. Notice that the first seven letters
of the input are read. The eighth letter ( h ) is not in the scan set and therefore the
scanning is terminated.
for example:
#include <stdio.h> int main() { char z[9]; printf("Enter string: "); scanf("%[aeiou]", z); printf("The input was \"%s\"\n", z); return 0; }
while you run this program, it results as:
+---------------------------------------------------------------+
| Enter string: ooeeooahah |
| The input was "ooeeooa" |
+---------------------------------------------------------------+
The scan set can also be used to scan for characters not contained in the scan set by
using an inverted scan set. To create an inverted scan set, place a caret (^) in the square
brackets before the scan characters. This causes characters not appearing in the scan set to
be stored. When a character contained in the inverted scan set is encountered, input ter-
minates. The followed example uses the inverted scan set [^aeiou] to search for consonants—more
properly to search for “nonvowels.”
for example:
#include <stdio.h> int main() { char z[9]; printf("Enter a string: "); scanf("%[^aeiou]", z); printf("The input was \"%s\"\n", z); return 0; }
+---------------------------------------------------------------+
| Enter a string: String |
| The input was "Str" |
+---------------------------------------------------------------+
A field width can be used in a scanf conversion specifier to read a specific number of
characters from the input stream. The next example inputs a series of consecutive digits as a two-
digit integer and an integer consisting of the remaining digits in the input stream.
for example:
#include <stdio.h> int main() { int x, y; printf("Enter a six digit integer: "); scanf("%2d%d", &x, &y); printf("The integers input were %d and %d\n", x, y); return 0; }
+-----------------------------------------------------------------------+
| Enter a six digit integer: 123456 |
| The integer input were 12 and 3456 |
+-----------------------------------------------------------------------+
scanf provides the assignment suppression character * . The assignment suppres-
sion character enables scanf to read any type of data from the input and discard it without
assigning it to a variable.
for example:
#include <stdio.h> int main() { int month1, day1, year1; int month2, day2, year2; printf("Enter a date in the form mm-dd-yyyy: "); scanf("%d%*c%d%*c%d", &month1, &day1, &year1); printf("month = %d day = %d year = %d\n\n", month1, day1, year1); printf("Enter a date in the form mm/dd/yyyy: "); scanf("%d%*c%d%*c%d", &month1, &day1, &year1); printf("month = %d day = %d year = %d\n\n", month2, day2, year2); return 0; }
+-------------------------------------------------------------------------------+
| Enter a date in the form mm-dd-yyyy: 11-18-2003 |
| month = 11 day = 18 year = 2003 |
| |
| Enter a date in the form mm/dd/yyyy: 11/18/2003 |
| month = 11 day = 18 year = 2003 |
+------------------------------------------------------------------------------+
0 0
- scanf的高级输入
- scanf的格式输入
- Scanf的输入类型
- scanf的输入格式
- C语言的scanf输入
- 深入理解scanf()的输入
- Scanf函数的输入问题
- scanf的安全输入问题
- scanf输入
- Scanf系列函数的高级用法
- scanf的一个扩展的输入格式
- scanf的输入字符时的陷阱
- scanf引出的输入缓冲区问题
- scanf读输入缓存引发的问题
- 关于输入scanf()中缓冲区的几个问题
- 增快输入速度的scanf
- 标准输入的原理:cin与scanf
- c中scanf的格式化输入函数
- 十条有用的 Go 技术
- Facebook-android-keyhashes
- 张正友摄像机标定的研究(MATLAB+OpenCV)
- UISwitch开关控件
- 9413827补丁安装
- scanf的高级输入
- 解决使用MathJax时弹出框显示MathJax no longer loads a default configuration file的问题~
- 一个身份证码校验的demo
- javaMail
- MySQL DDL操作语句
- Linux下Makefile的automake生成全攻略
- java 权限控制算法
- ANDROID 隐藏 任务栏 systemui systembar 全屏显示
- struts action中获取表单传递的值