0

It is a statement given in K&R that printf() and putchar() can be interleaved. If it true then why is the following code not giving the required output:-

#include"stdio.h"
void main()
{
    char c,d;
    printf("Enter the first character\n");
    scanf("%c",&c);
    printf("%c\n",c);
    printf("Enter the second character\n");
    d=getchar();
    putchar(d);
    printf("\n");
}

Whenever I am executing this program, the output is as follows:-

Enter the first character
a
a
Enter the second character


This is the output. This is also happening if I replace printf() by putchar() and scanf() by getchar(). Why is this happpening?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
kusur
  • 598
  • 11
  • 33
  • 6
    1) main() should return int. 2) the second character is in fact the '\n' . 3) try to show the hex value as well, and you will see more. – wildplasser Dec 29 '12 at 16:14
  • You can reuse `c` by the way. (Unless of course you are saving it to use it later on.) – RastaJedi Apr 19 '16 at 19:36

4 Answers4

5

The first scanf leaves in the input buffer the \n resulting from the Return press, so your second getchar() will acquire this \n instead of acquiring another character from the user.

If you want to skip that newline character, you can either instruct the scanf to "eat" it:

scanf("%c\n",&c);

or "eat it" directly with a call to getchar():

scanf("%c",&c);
getchar();

(notice that these are not exactly equivalent, since the second snippet will eat whatever character happens to be in the buffer, while the first one will remove it only if it's a \n)

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Thank you for the reply. How exactly does getchar() "eat away" the '\n' ? – kusur Dec 29 '12 at 16:35
  • 1
    @KunalSuri: `getchar()` extracts the next character from the input buffer; `\n` is the next character in the input buffer. So, it extracts and discards it (since the returned value is not assigned to anything). – Matteo Italia Dec 29 '12 at 16:36
  • 3
    @KingsIndian: It's not K&R code; they'd never write `void main()`. From the questions, K&R said only that calls to `printf()` and `putchar()` can be interleaved, which they can, as demonstrated by the (flawed) program. The issue is not the output; it is the input. – Jonathan Leffler Dec 29 '12 at 16:42
  • @MatteoItalia: I tried removing '\n' from the code. Even then the output for the second character is the same. – kusur Dec 29 '12 at 16:45
  • @KunalSuri: you didn't get what I mean. I'm not talking about the `\n` you *write* on `stdout`, but about the one that is in the input buffer due to the fact that when you input the first character you press the *Return* key on the keyboard. – Matteo Italia Dec 29 '12 at 16:52
  • 1
    When you type the response to the 'first character' prompt, you actually type two characters. For sake of example, you type 'a' and return. The first `scanf()` reads the 'a', leaving the newline to be read by the `getchar()`. In fact, you probably don't even have to enter anything before the second output appears. You could also enter 'ab' (and return) and you'd see the first character is 'a' and the second 'b' and so on. – Jonathan Leffler Dec 29 '12 at 16:54
1

You can correct your code like this:

#include <stdio.h>

int main() {
    char c, d;
    printf("Enter the first character\n");
    scanf("%c\n", &c);    // Ask scanf to read newline and skip
    printf("%c\n", c);

    printf("Enter the second character\n");
    d = getchar();
    putchar(d);
    printf("\n");
    return 0;
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
0

You are getting two a's because you type one in which is echoed to the console and then you print it out.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
-2

flush the stdin before using getchar()..

In turbo, use fflush()..

In gcc, use __fpurge(stdin)..(this is available in <stdio_ext.h> header)..

Flushing the standard input before scanning anything will solve your issue..

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42