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

output:

C:\Users\Saran\Desktop>gcc new.c
C:\Users\Saran\Desktop>a
Enter a : s
Enter b : Enter c : c
Enter d :

if this is my code, it takes first char into variable 'a' and then if i press enter the variable 'b' takes enter as its input. how to overcome this problem?

saran teja
  • 31
  • 1
  • 6

1 Answers1

0

In C language all the input operation done with the help of ASCII code. Enter key have ASCII code 10, when you press Enter key then scanf("%c",&b) convert it into ASCII and store 10. You can see by printing printf("%d",b).

In your case you can use alternative input-string getchar() for handling Enter like

int main() {

    char a,b,c,d;
    printf("Enter a : ");
    scanf("%c",&a);
    getchar();
    printf("Enter b : ");
    scanf("%c",&b);
    getchar();
    printf("Enter c : ");
    scanf("%c",&c);
    getchar();
    printf("Enter d : ");
    scanf("%c",&d);
    printf("\n print a - %c ",a);
    printf("\n print b - %c ",b);
    printf("\n print c - %c ",c);
    printf("\n print d - %c",d);


    return 0;
}

Output:

Enter a : Enter b : Enter c : Enter d :
print a - m
print b - n
print c - 0
print d - p

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70