简体   繁体   中英

What is the difference between stdin with fscanf and scanf

I want to get a integer from to user. But I wonder what is the difference between fscanf(stdin, "%d", &x); and scanf("%d", &x); ?

What is the difference between stdin with fscanf and scanf?

The scanf function is equivalent to fscanf with the argument stdin interposed before the arguments to scanf . C11 §7.21.6.4 2

So aside from a different amount of typing the code, they are the same.

Unless you #defined fscanf , scanf or stdin , both calls will do the same thing. Note that such redefinitions would invoke undefined behavior.

Some standard input output functions have shorter versions for dealing with stdin or stdout :

  • fscanf(stdin, ...) is equivalent to scanf(...
  • getc(stdin) is the same as getchar()
  • putc(c, stdout) can be written putchar(c)
  • fprintf(stdout, "Hello world\\n"); is usually shortened to printf("Hello world\\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM