I am learning by myself how to code using the C languange. In order to study this latter a bit more in depth, I'm doing some basic exercises and, due to this, today I have faced a little issue using the scanf() instruction. Infact, the following code:
int main() { char inputOne; char inputTwo; printf("Insert a char: "); scanf("%c", &inputOne); // &inputOne is the pointer to inputOne. printf("Insert another char: "); scanf("%c", &inputTwo); if (inputOne == inputTwo) { printf("You have inserted the same char!\n"); printf("Chars inserted: %c, %c\n", inputOne, inputTwo); } else { printf("You have inserted two different chars!\n"); printf("Chars inserted: %c, %c\n", inputOne, inputTwo); } } when compiled does not return any error, but when I launch the application on the Terminal, I am not able to insert the second character. Here is an example of what happens:
Macbook-Pro-di-Rodolfo:~ Rodolfo$ /Users/Rodolfo/Documents/GitHub/Fondamenti\ di\ C/esempio-if-else ; exit; Insert a char: a Insert a second char: You have inserted two different chars! Chars inserted: a, logout [Process completed] Can anybody explain me why this happens?
scanf(" %c", &inputTwo)andscanf(" %c", &inputTwo)identical for the compiler?scanf()behaves in the same way indipendently from the number of white spaces between the quotation mark and the conversion specification%. In other words, if<space><space><space>%cand<space>%coutputs the same stuff.