2

I'm trying to make a C program which accepts two character consecutively and then print them, but I'm getting some anomaly in the output.

The program

#include<stdio.h>
int main()
{
    char c1, c2;
    printf("\n Enter two characters : ");
    scanf("%c%c", &c1, &c2);
    printf("\n The the characters are %c and %c ", c1, c2);
    return 0;
}

The output

Enter two characters : a
The the characters are a and

In the output without asking for the second character it is directly going to the next printf() statement. Why is this happening?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
raja THE KING
  • 61
  • 1
  • 8
  • 1
    Always show your input in questions concerning I/O. – cadaniluk Dec 30 '15 at 16:46
  • 1
    this can help you - http://stackoverflow.com/questions/24099976/taking-two-characters-consecutively-in-c – Amit Upadhyay Dec 30 '15 at 16:46
  • The result is perfectly valid: `scanf` is reading 2 characters: `{a}{\n}`, and so the output will be _"[..]characters are {a} and {\n}"_. Perfectly valid and predictable – Elias Van Ootegem Dec 30 '15 at 16:53
  • @EliasVanOotegem @nos And what difference is being created just by adding a space between the two `%c`? – raja THE KING Dec 30 '15 at 16:54
  • @rajaTHEKING: the leading space in `scanf` skips over all whitespace characters that might be in the buffer (including newlines). Alternatively you can use something like this: `%[^\n]`. All format specifiers automatically skip whitespace, except for `%c`, `%[]` and `%n`. There's a lot of ways to tackle these kind of situations either way, most of them will end with saying that you should avoid `scanf` whenever you can – Elias Van Ootegem Dec 30 '15 at 16:58

3 Answers3

2

Use the following:

scanf(" %c %c", &c1, &c2);
/*     ^  ^               */

When you hit return key after first character, '\n' is read in b. Using space before %c instructs to ignore any white-space(space, tab, newline etc).

Further read: What does space in scanf mean

Community
  • 1
  • 1
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
1

Try

#include<stdio.h>
int main()
{
    char c1, c2;
    printf("\n Enter two characters : ");
    scanf(" %c %c", &c1, &c2);
    printf("\n The the characters are %c and %c ", c1, c2);
    return 0;
}

The space between the %c consumes the trailing left out newline \n character.

Haris
  • 12,120
  • 6
  • 43
  • 70
  • In the comment they say that while giving the inputs I'm entering a and '\n'.. So what difference your code is creating ? – raja THE KING Dec 30 '15 at 16:51
0

add a space in between %c in scanf() function. When you press enter eg. \n after first input c1, it gets stored in c2 in your case. By using space in between %c instructs to ignore any white-space(space, tab, newline etc).

  #include<stdio.h>
    int main()
    {
        char c1, c2;
        printf("\n Enter two characters : ");
        scanf("%c %c",&c1,&c2);
        printf("\n The the characters are %c and %c ", c1, c2);
        return 0;
    }
roottraveller
  • 7,942
  • 7
  • 60
  • 65