0

I'm making function working like

Input contains : “1 2 3 4 5 6 7 8 9 10\n”

Function Outputs: “{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\n”

I want to take a look how getchar() works, so I wrote the function like this:

    int c ;

    printf("{");

    while ((c = getchar()) != EOF) {
            printf("%c, ", c);
            getchar();

    }
    getchar();
    printf("}\n");

and when I put "1 2 3 4 5 6 7 8 9 10\n”, it comes like:

{1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 
 , }

I guess something wrong with buffers inside. Maybe getchar() reads and out character by character so that 10 is considered as 1 and 0 separately?

I looked up some past questions,, but I didn't get it. Thanks for your insight.

  • Yes you're right `getchar()` only reads one character from `stdin`. – Benjamin J. Feb 06 '18 at 19:33
  • `getchar()` is not `getmultiplechars()`. You get exactly what the name says you get - a single char. *10* is two chars (*0* and *1*). – Ken White Feb 06 '18 at 19:33
  • 1
    Do yourself a favor and [get a good book to learn from](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). C is not a good language to learn by trial-and-error, and learning from online resources may not be the best plan, as there is a lot of bad or completely wrong information floating around the interwebs. – ad absurdum Feb 06 '18 at 20:18

2 Answers2

6

Yes you are right - suprisingly it does what its name says - it inputs character by character. Here '1' is a character and '0' is another.

Also there are different ways to deal with this - you can form the number on your own from those characters (whenever you see a space you will know that you have seen a number and likely skip mutiple whitespaces) or take a line at a time and get the numbers separated by spaces using strtok and then converting them using strtol.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • 1
    "_suprisingly_ it does what its name says" -- is that the _principle of maximal surprise_? Hmmm, I'll have to incorporate that into my next user interface. – ad absurdum Feb 06 '18 at 20:21
1

The input stream is a sequence of characters - not integers, not floats, not strings, just characters. When you type in something like 1 2 10, what gets put into the input stream is the character sequence {'1', ' ', '2', ' ', '1', '0', '\n' }. getchar simply reads the next character from that character sequence.

Note that in your loop you're calling getchar twice and throwing away every other input, which is why the trailing 0 doesn't show up.

If you want to interpret the character sequence '1' '0' as the integer value 10, then you'll need to either buffer those characters and convert them to an integer value yourself, or use scanf with the %d conversion specifier:

int value;

putchar( '{' );
while ( scanf( "%d", &value ) == 1 )
  printf( "%d, ", value );
printf( "}\n" );
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Does 'scanf' return '1' when it finishes reading from the stream? – Whiskey Mental Disturb Feb 06 '18 at 20:14
  • @WhiskeyMentalDisturb: `scanf` returns the number of items read and assigned; in this particular case, we're only expecting to read 1 item, so we expect a return value of 1 on success. If the next non-whitespace character in the input stream is not a decimal digit (say we typed `'a'` instead of `'1'`), then we have a *matching failure*, and in this case `scanf` will return 0. If there are no characters before end-of-file or there's an error on the input operation, then `scanf` will return EOF. – John Bode Feb 06 '18 at 20:18