You are not logged in. Please login at www.codechef.com to post your questions!

×

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; }

asked 30 Jun '17, 19:42

hvishnu_123's gravatar image

0★hvishnu_123
1
accept rate: 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')
link

answered 30 Jun '17, 20:48

utkalsinha's gravatar image

5★utkalsinha
723118
accept rate: 12%

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

link

answered 30 Jun '17, 20:05

shilpa_12345's gravatar image

2★shilpa_12345
12
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×43
×4

question asked: 30 Jun '17, 19:42

question was seen: 207 times

last updated: 30 Jun '17, 20:48