0

I am new to C programming language and I was trying to run this simple C program , I was expecting the program to go take two char inputs separately and some output based on some if condition , but the program just takes one input and terminates without going through second input , I don't know what I am doing wrong ,please help . this is the output I am getting , below is the code I was trying to run

#include <stdio.h>

int main()
{
    char c,d;
    printf("Enter your first character : ");
    scanf("%c",&c);
    if (c=='y')
    {
        /* code */
        printf("y");
        printf("\nEntered character is %c\n" ,c);
    }
    else
    {
        printf("unknown character");
    }

    printf("Enter your second character : ");
    scanf("%c",&d);
    if (c=='a')
    {
        printf("\nEntered character is %c\n" ,d);
    }
    else
    {
        printf("Unknown character");
    }   
}
Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 1
    Short answer: change `"%c"` to `" %c"`. (But beware that this is a somewhat ridiculous workaround. It doesn't make sense at first. It also doesn't mean that you should go around adding extra spaces to every other `scanf` format string.) – Steve Summit Nov 25 '22 at 17:50

0 Answers0