记得当初从C语言学习开始就使用scanf,关于scanf的用法也略知一二,对使用scanf出现的问题并未进行深刻探究,故笔者打算对scanf实现进行探究。
如何找到scanf源码
关于VC中的CRT代码在 VS目录下的\VC\crt\src中,我们就先把scanf.c扒出来。
int __cdecl scanf (
const char *format,
...
)
{
va_list arglist;
va_start(arglist, format);
return vscanf_fn(_input_l, format, NULL, arglist);
}
scanf函数实际调用的是vscanf_fn
int __cdecl vscanf_fn (
INPUTFN inputfn,
const char *format,
_locale_t plocinfo,
va_list arglist
)
/*
* stdin 'SCAN', 'F'ormatted
*/
{
int retval = 0;
_VALIDATE_RETURN( (format != NULL), EINVAL, EOF);
_lock_str2(0, stdin);
__try {
retval = (inputfn(stdin, format, plocinfo, arglist));
}
__finally {
_unlock_str2(0, stdin);
}
return(retval);
}
实际上我们是根据inputfn这个函数指针来进行我们的scanf操作,我们继续来找这个inputfn出处。
我们在input.c中找到了真正的处理函数,我们的重点是解析这个文件,我们来看input函数说明就知道了。
/***
*int _input(stream, format, arglist), static int input(format, arglist)
*
*Purpose:
* get input items (data items or literal matches) from the input stream
* and assign them if appropriate to the items thru the arglist. this
* function is intended for internal library use only, not for the user
*
* The _input entry point is for the normal scanf() functions
* The input entry point is used when compiling for _cscanf() [CPRFLAF
* defined] and is a static function called only by _cscanf() -- reads from
* console.
*
* This code also defines _input_s, which works differently for %c, %s & %[.
* For these, _input_s first picks up the next argument from the variable
* argument list & uses it as the maximum size of the character array pointed
* to by the next argument in the list.
*
*Entry:
* FILE *stream - file to read from
* char *format - format string to determine the data to read
* arglist - list of pointer to data items
*
*Exit:
* returns number of items assigned and fills in data items
* returns EOF if error or EOF found on stream before 1st data item matched
*
*Exceptions:
*
*******************************************************************************/
浅析源码前准备
首先来学习几个关键函数和Macro的用法:
</