'what is the difference between char and int used in scanf function in a loop? [duplicate]
when I write a for loop to scanf some int value, the output is correct. however, I try to this loop to scanf some char value, it seems something wrong.(I was wondering the space or '\t' as a char to be scanned)
//when the input are
2
1 2
3 4
//
#include<stdio.h>
int main(void){
int n;
scanf("%d", &n);
int x;
int y;
for(int i = 0; i< n; i++){
scanf("%d %d", &x, &y);
}
return 0;
}
//when I enter
2
a b
the program is finished and i cannot enter more char.
//
#include<stdio.h>
int main(void){
int n;
scanf("%d", &n);
char x;
char y;
for(int i = 0; i< n; i++){
scanf("%c %c", &x, &y);
}
return 0;
}
Solution 1:[1]
Remember, the space (' ') is also a char, so the scanf is reading the space instead of the second letter.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | aleiis |
