Since the printf function variable parameter is implemented, there must be a variable parameter implementation of the SCANF function. Don't say much nonsense, the source:
Source code But more analysis, such as to understand the principle, please fan Ben Blog previous articles to see the description.
Welcome to follow Sina Weibo: http://weibo.com/u/1896293701/home?topnav=1&wvr=6
#include <stdio.h> #include <stdarg.h>int myscanf (const char *FMT, ...), int main (void) {int num;p rintf ("Pls i Nput num:\n "); MYSCANF ("%d", &num);p rintf ("num:%d\n", num); return 0;} The following implementation methods I do not explain more, the previous blog is written in detail, turn over can read, not difficult. int myscanf (const char *FMT, ...) {va_list ap; int ret; Va_start (AP, FMT); ret = vscanf (FMT,AP);//The VSCANF function of C library is called here, and then it is found, and the analysis method and vprintf are identical. Va_end (AP); return ret;}
Operation Result:
Input 100, output 100, use the MYSCANF function to enter
C-language variable parameter implementation scanf function