What is scanf reading?



  • What is scanf reading?



  • This design considers any symbol from the inlet flow and does not retain it. I mean, it's a way to miss one symbol.

    For example, we read every second symbol:

    #include <stdio.h>
    

    int main() {
    char c;
    while (scanf("%*c%c", &c) == 1) {
    putc(c, stdout);
    }
    }

    $ gcc scanf_c.c

    $ echo "Hello, world!" | ./a.out
    e_l,w_r_d

    $ echo " Hello, world!" | ./a.out
    H_l_o o_l!
    $

    P.S. I sent the answer because I erroneously wrote that %*c passes underwear symbols. That's not true. In order to pass the gaps in the format, a gap must be clearly inserted:

    #include <stdio.h>

    int main() {
    char c;
    while (scanf(" %*c%c", &c) == 1) {
    printf("_%c", c);
    }
    }

    $ gcc scanf_c_skipws.c

    $ echo "Hello, world!" | ./a.out
    e_l,o_l!
    $ echo " Hello, world!" | ./a.out
    e_l,o_l!
    $ echo " Hello, world!" | ./a.out
    e_l,o_l!
    $

    P.P.S. Thank you. https://ru.stackoverflow.com/users/232/avp For comment.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2