The * Modifier with printf() and scanf()
来源:互联网 发布:医疗器械 软件确认 编辑:程序博客网 时间:2024/06/07 08:03
Both printf() and scanf() can use the * modifier to modify the meaning of a specifier, but they do so in dissimilar fashions. First, let 's see what the * modifier can do for printf().
1. Suppose that you don 't want to commit yourself to a field width in advance, but that you want the program to specify it. You can do this by using * instead of a number for the field width, but you also have to use an argument to tell what the field width should be. That is, if you have the conversion specifier %*d, the argument list should include a value for * and a value for d. The technique also can be used with floating-point values to specify the precision as well as the field width. Listing 4.16 is a short example showing how this works.
Listing 4.16 The varwid.c Program
/* varwid.c -- uses variable-width output field */
#include <stdio.h>
int main(void)
{
unsigned width, precision;
int number = 256;
double weight = 242.5;
printf( "What field width?\n ");
scanf( "%d ", &width);
printf( "The number is :%*d:\n ", width, number);
printf( "Now enter a width and a precision:\n ");
scanf( "%d %d ", &width, &precision);
printf( "Weight = %*.*f\n ", width, precision, weight);
return 0;
}
The variable width provides the field width, and number is the number to be printed. Because the * precedes the d in the specifier, width comes before number in printf() 's argument list. Similarly, width and precision provide the formatting information for printing weight. Here is a sample run:
What field width?
6
The number is : 256:
Now enter a width and a precision:
8 3
Weight = 242.500
Here, the reply to the first question was 6, so 6 was the field width used. Similarly, the second reply produced a width of 8 with 3 digits to the right of the decimal. More generally, a program could decide on values for these variables after looking at the value of weight.
2. The * serves quite a different purpose for scanf(). When placed between the % and the specifier letter, it causes that function to skip over corresponding input. Listing 4.17 provides an example.
Listing 4.17 The skip2.c Program
/* skip2.c -- skips over first two integers of input */
#include <stdio.h>
int main(void)
{
int n;
printf( "Please enter three integers:\n ");
scanf( "%*d %*d %d ", &n);
printf( "The last integer was %d\n ", n);
return 0;
}
The scanf() instruction in Listing 4.17 says, "Skip two integers and copy the third into n. " Here is a sample run:
Please enter three integers
1976 1992 1996
The last integer was 1996
This skipping facility is useful if, for example, a program needs to read a particular column of a file that has data arranged in uniform columns.
- The * Modifier with printf() and scanf()
- printf and scanf format codes
- C Printf and Scanf Reference
- Test of scanf and printf
- printf & scanf
- printf,scanf
- scanf-printf
- scanf && printf
- printf、scanf
- printf scanf
- printf scanf
- The differences between scanf() and gets()
- printf|scanf注意事项
- 用好printf和scanf
- printf与scanf
- printf与scanf
- printf和scanf函数
- scanf和printf
- 数据结构学习之二分查找法递归实现
- Objectvie-C继承、新增类、内部函数调用、外部函数调用
- 任小笨空间伤感日志:享受瞬间的华丽、绽放凄美的诀别
- 对于软件项目管理者,你怎么看?
- 【五】WinXP+IIS6 配置Discuz和phpwind论坛全完手册---IIS6配置、测试
- The * Modifier with printf() and scanf()
- linux PPTP VPN客户端安装
- Storyboard多View的切换 [xcode 4.4.1]
- 用正则表达式判断邮箱命名是否合法
- 信号的接受
- MySql常用命令行
- CSS控制TD内字数长度 省略号 css截取 超过 显示 省略号
- Evaluate the Costs and Benefits of Using DBMS_REPAIR【每日一译】--2012-10-26
- Google Chrome Tools