2

I am having some difficulties in understanding the condition of the while loop given below:

int main()
{
    char s[]="Let's Get it Started";
    int i=0;
    while(s[i]!=0)
    { 
       //do something
       ++i

    }
}

I know that string is stored with the last character as \0 which has the ASCII value as 0. In the while loop, it is comparing the value of the particular characters of the array. So when it reaches \0 condition would be like

'\0' != 0 // I guess this is also true

So isn't this an infinite loop?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Patel
  • 87
  • 8
  • 3
    `'\0'!=0` is false. And yes, that is an infinite loop as you don't increment `i`. – Spikatrix Jun 11 '15 at 05:49
  • `'\0'` is different from `'0'`. – alk Jun 11 '15 at 05:52
  • @CoolGuy I have edited the question now Not incrementing i was a typo What i don't undertsand is how how `\0!=0 is false` – Patel Jun 11 '15 at 05:52
  • @Patel , Because of the same reason why `'a' != 97` is false. See [The ASCII table](http://www.asciitable.com/index/asciifull.gif) and [@MohitJain's answer](http://stackoverflow.com/a/30772609/3049655) – Spikatrix Jun 11 '15 at 05:56
  • 2
    @Patel: If `'\0'` equals `0` and `!=` means "not equal", then `'\0' != 0` is not true. And "not true" equals "false". So `'\0' != 0` is "false". – alk Jun 11 '15 at 05:57
  • Please, have a look at the answers http://programmers.stackexchange.com/questions/181505/what-is-a-null-terminated-string and http://stackoverflow.com/questions/2911089/null-terminating-a-string. Even you put an int/short 0 at the end of the character array, it works. – Deniz Jun 11 '15 at 06:01
  • For your reference: http://man7.org/linux/man-pages/man7/ascii.7.html – alk Jun 11 '15 at 06:05

3 Answers3

4

In C, '\0' has same value (and even type) as 0. Both are ints with 0 value.

So isn't this an infinite loop ?

So, no it is not an infinite loop because of assumption that \0 and 0 are different. But the loops may be infinited for other factors not in the scope of this question.

From C11 specs section 5.2.1/2 Character sets

A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
1

Maybe you have gone into errors. you can seek ASCII table, '\0'-->0, '0'-->48.

In your codes, while(s[i] != 0), the 0 is int, not a char, so '\0' == 0 is true.

By the way, you can write codes below:

int a = '\0';
int b = '0';
printf("%d  %d\n", a, b);

I believe you can know the problems clearly. So It is not a infinite loop.

cwfighter
  • 502
  • 1
  • 5
  • 20
1

You seem to be making a little confusion between ascii letters '0' and '\0'. The first one is the ascii character '0' which has an equivalent number (48) according to ascii table. But when using the escape bar before the zero '\0' you are using the null character (which is different from the null number) which, by the way, has all its bits zeroed. Therefore, an ascii character with all its bits set to zero is the same as the number 0.

Thus, this is not an infinite loop because when comparing the null character at the end, it is equal to number 0.

FELIPE_RIBAS
  • 401
  • 1
  • 3
  • 14