while loop is not working

#include<stdio.h>
#include<string.h>
int main()
{
char a,roll[51];
int i=0;

scanf("%s",&roll);
while(roll[i]<='\0'){

printf("%c\n",roll[i]);
i++;
}

return 0;
}

you can not write <’\0’ cozz it is a null chracter and we can not compare it.
use dis one
while(roll[i]!=’\0’)

It is not like you cannot compare ‘\0’ as @shilpa_12345 has said. Characters are in fact stored as integers only according to their ASCII values. Your code is perfectly fine and your while loop also works fine. Only thing is your logic might be wrong which is why you are not getting the results you are expecting. ‘\0’ has an ASCII value of decimal 0. So if you input any numbers or characters, the expression while(roll[i]<=’\0’) will always evaluate to false as any numbers or alphabets ASCII values are greater than decimal 0. So basically, what ever you type in numbers and alphabets, the while loop wont get executed at all.

Try to run your code with special characters like plus '+', '(', ')' etc, it will work perfectly. So, instead, if you want to iterate through every characters read via scanf(), you would want to check if ‘\0’ is reached as follows:

while(roll[i] != '\0')
1 Like
//