+ 2
Why I am not able to take multiple character input in c language I am trying it in vs code.Not ableto take morethan one char inp
8 Respuestas
+ 3
scanf is a booger for many users because it keeps the '\n' char in the input buffer. There are ways to take care of it one is the method Jayakrishna showed using getchar() after a scanf call. Another is to use a space prior to the conversion.
char a, b;
int c;
puts("Enter first char.")"
scanf("%a", &a);
puts("Enter an interger.");
scanf(" %d", &c);
puts("Enter a second char.");
scanf(" %c", &b);
The space gets rid of the white space chars. Another way is to use the suppression conversion %*c such that scanf("%d%*c", &c); ... scanf("%c%*c", &b);
Hopefully what I was able to share is using scanf we have to figure out what we will do the remainder of the input buffer and the pesky '\n' char that gets written to strings. Hope to see more code from you soon.
+ 2
Try
scanf("%d", &c) ;
getchar();
scanf("%c", &b) ;
+ 1
How are you giving inputs?
Run this code and see output..
#include <stdio.h>
int main()
{
char a,b;
int c;
printf("first character input");
scanf("%c",&a);
printf("first integer input");
scanf("%d",&c);
printf("second character input");
scanf("%c",&b);
printf("\na=%c c=%d b=%c",a,c,b);
}
input :
a12b
Output: is like
a = 'a' c = 12 b = 'b'
+ 1
Jayakrishna🇮🇳 visual studio code is skipping second character input.
+ 1
One last look at multiple scanf calls:
Here is a snippet with multiple calls that clears the input buffer prior to each subsequent call.
https://code.sololearn.com/cFi09ljet1z7
+ 1
Datatype char is just allow us only one char thats why need to use getchar() ; for add to more char. When we use this no need to add scanf().
0
Thanks 🙏
0
William Owens thankyou