scanf & wscanf

in msdn——>

scanf, wscanf

Read formatted data from the standard input stream.  从标准输入流读入格式化数据

int scanf( const char *format [,argument]... );

int wscanf( const wchar_t *format [,argument]... );  

例行程序                    
必要的头文件                                                   兼容性

RoutineRequired HeaderCompatibility
scanf                    <stdio.h>ANSI, Win 95, Win NT
wscanf<stdio.h> or <wchar.h>ANSI, Win 95, Win NT

For additional compatibility information, see  Compatibility  in the Introduction    
对于额外的兼容性信息,请参见兼容性的介绍

 

Return Value  返回值

Both scanf and wscanf return the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.
scanf函数和wscanf函数都会成功的返回字段值,并且进行转换和分配操作 ;如果返回值中不包括字段信息就只读取但不分配空间。返回值为0表示没有字段被分配。返回值为EOF表示返回一个错误信息或者是第一次尝试读取一个字符时却遇到了文件结束字符或者字符串结束字符

Parameters 参数

format  格式

Format control string  格式控制字符串

argument  参数

Optional arguments  可选参数

Remarks  附注

The scanf function reads data from the standard input stream stdin and writes the data into the location given by argument. Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. If copying takes place between strings that overlap, the behavior is undefined.
scanf函数从标准输入设备(键盘)所输入的标准输入流中读取数据并把数据写入参数给出的指定位置 。每个参数都必须是一个指向变量类型的指针,即和格式说明符类型相匹配的指针。如果赋值字符串之间发生重叠,则为未定义行为。

wscanf  is a wide-character version of  scanf ; the  format  argument to  wscanf  is a wide-character string.  wscanf  and  scanf  behave identically otherwise.
wscanf是一个宽字符版本的scanf, wscanf的格式参数是一个宽字符的字符串。 wscanf和scanf其他方面的特性完全相同。

Example:


#include <stdio.h>  
  
int main(void)  
{  
       int       i_number, result;  
       float     f_number;  
       char      c_number, str[81];  
       wchar_t   wc_str, ws_str[81];  
  
       printf( "\n\nEnter an int, a float, two chars and two strings\n");  
  
       result = scanf( "%d %f %c %C %s %S", &i_number, &f_number, &c_number, &wc_str, str, ws_str );  
       printf( "\nThe number of fields input is %d\n", result );  
       printf( "The contents are: %d %f %c %C %s %S\n", i_number, f_number, c_number, wc_str, str, ws_str);  
  
       wprintf( L"\n\nEnter an int, a float, two chars and two strings\n");  
  
       result = wscanf( L"%d %f %hc %lc %S %ls", &i_number, &f_number, &c_number, &wc_str, str, ws_str );  
       wprintf( L"\nThe number of fields input is %d\n", result );  
       wprintf( L"The contents are: %d %f %C %c %hs %s\n", i_number, f_number, c_number, wc_str, str, ws_str);  
}  
//VC++6.0
/***************************************************  
  
Enter an int, a float, two chars and two strings  
71  
98.6  
h  
z  
Byte charcaters  
  
The number of fields input is 6  
The contents are: 71 98.599998 h z Byte charcaters  
  
  
Enter an int, a float, two chars and two strings  
36  
92.3  
y  
n  
Wide characters  
  
The number of fields input is 6  
The contents are: 36 92.300003 y n Wide characters  
Press any key to continue  
***************************************************/ 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值