浅析Scanf源码

记得当初从C语言学习开始就使用scanf,关于scanf的用法也略知一二,对使用scanf出现的问题并未进行深刻探究,故笔者打算对scanf实现进行探究。如何找到scanf源码关于VC中的CRT代码在 VS目录下的\VC\crt\src中,我们就先把scanf.c扒出来。int __cdecl scanf ( const char *format,
摘要由CSDN通过智能技术生成

记得当初从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的用法:

</

  • 27
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值