-2
#include<stdio.h>
int main()
{
  char a,b;  
  printf("Enter Character 1 ");
  scanf("%c",&a);
  printf("Enter Character 2");
  scanf("%c",&b);
  printf("%c%c",a,b);
}

find output of program. It is not taking 2 characters.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    When you ran it, how many keys did you press, which ones, and in what order? – Martin James Sep 22 '17 at 11:28
  • 1
    If you want to read only one char, why you are using scanf? Check get char http://www.manpagez.com/man/3/getchar/ – Bizzu Sep 22 '17 at 11:32
  • 1
    Even consecutive getchar() with enter will give the same result. See [this](https://stackoverflow.com/questions/13473693/scanf-getchar-function-is-skipped). fgets() is the best alternative for scanf() in most case – Nerdy Sep 22 '17 at 11:35

1 Answers1

0

When you enter the first character; The character Ascii value is placed in the variable A . Then you press enter and the ascii value of enter thats 13 is placed in the second variable. So B should be 13.

In order to run this program use flushall() library function..

int main() { 
    char a,b; 
    printf("Enter Character 1 "); 
    scanf("%c",&a);
    flushall();
    printf("Enter Character 2"); 
    scanf("%c",&b); 
    printf("%c%c",a,b); 
    return 0;
}

You could also use the standard Library function scanf() Write something like:

Scanf(" %c",&variablename);

The space before the %c tells the compiler to skip any whitespace (if any).

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
Zahid Khan
  • 2,130
  • 2
  • 18
  • 31