Signadup
Signadup

Reputation: 9

Why doesn't my while loop abort after I enter 0?

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

Answers (3)

Baldrickk
Baldrickk

Reputation: 4409

Note that this answer follows the edit to the question (part b)

I don't know why you suddenly declared the chars 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

Geoduck
Geoduck

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

haccks
haccks

Reputation: 106012

Change

char zerovalue=0;  

to

char zerovalue = '0';  // 0 != '0'

Upvotes: 5

Related Questions