Reputation: 9
char inputvalue=7;
char zerovalue=0;
while(inputvalue != zerovalue)
{
scanf (" %c",&inputvalue);
printf("%c\n",inputvalue);
}
Why doesn't my while loop abort after I enter 0?
Thank you for your answer. Question part b: How do I fix this, so it would work as expected?
char inputvalue[100]='48';
char zerovalue[100]='0';
while(inputvalue != zerovalue)
{
scanf (" %c",&inputvalue);
printf("%c\n",inputvalue);
}
Upvotes: 0
Views: 109
Reputation: 4409
Note that this answer follows the edit to the question (part b)
I don't know why you suddenly declared the char
s to be char[100]
s.
ASCII 0
is either (char) 48
or (char) '0'
not '48'
.
The following works (and compiles)
#include <stdio.h>
int main()
{
char inputvalue = 0; //initialised to 0, just so no undefined behaviour can happen
char zerovalue = '0'; //this is ASCII '0' (== 48)
while(inputvalue != zerovalue)
{
scanf (" %c",&inputvalue);
printf("%c\n",inputvalue);
}
}
Upvotes: 0
Reputation: 9005
You are entering the character '0' which is not the same as the integer 0.
'0' is in the middle of the ASCII character set.
Upvotes: 1