0
votes

why my program did not go in infinite loop. I have not used '\0' for testing string end in while loop, instead of that I have use 0. Does '\0' and 0 same in C?

If yes, then if 0 is in the middle of a string, then printf should end printing the string right there. e.g. printf("%s", "hello how0 are you") should print 'hello how'

int main( )
{
    char s[ ] = "No two viruses work similarly" ;
    int i = 0 ;
    while ( s[i] != 0 )
    {
        printf ( "%c", s[i]) ;
        i++ ;
    }
}
8
how below two differes?Ravi
printf("%s", "hello how0 are you") vs printf("%s", "hello how\0 are you") why output differes in the above two cases?Ravi
Maybe this is not clear: a string like "ABC" is implicitely terminated by a null character, that's the reason your loop ends. So in memory "ABC" looks like this: 65 66 67 0, 65 being the ASCII value of the A character etc.Jabberwocky
@4386427 you're right, deleted the commentJabberwocky

8 Answers

6
votes

'\0' has the same meaning as 0, but '0' doesn't have the same meaning as 0.

printf("%s", "hello how0 are you") will print hello how0 are you.

printf("%s", "hello how\0 are you") will print hello how.

6
votes

In the C language, '\0' means exactly the same thing as the integer constant 0 (same value zero, same type int).

Moreover, to someone reading the code, writing '\0' suggests that you're planning to use this particular zero as a character(null).

3
votes

In C source code, 0 and '\0' are effectively the same: Each is an int constant with value zero. '\0' is used to indicate that we are working with characters, but it is the same as 0 to the compiler. (In C++, '\0' is a char instead of an int.)

The code for the display character “0” is not zero. In ASCII, it is 48. In any C implementation, it can be written as '0'. The value for this will always be positive and not zero, even if the C implementation uses some character encoding other than ASCII. So having a character “0” in the middle of a string will not act as a null terminator.

3
votes

Does '\0' and 0 same in C?

Yes, they are different ways of writing the same integer constant 0. The both constants in C (opposite to C++) have the type int and the same representations.

why my program did not go in infinite loop. I have not used '\0' for testing string end in while loop, instead of that I have use 0

The condition in this while loop

while ( s[i] != 0 )

is equivalent to the condition

while ( s[i] != '\0' )

as it is pointed above.

if 0 is in the middle of a string, then printf should end printing the string right there. e.g. printf("%s", "hello how0 are you") should print 'hello how'

This string literal

"hello how0 are you"

does not have an embedded zero character. This string with an embedded zero character can look for example the following way

"hello how\0 are you" 
3
votes

0 is the ascii value of the character '\0'. 0 is NOT the ascii value of the '0' character.

2
votes

I would of put this into comment but it would be too long for comment.

Does '\0' and 0 same in C I suggest you try this:

int main( )
{
    char s[ ] = "No two viruses work similarly" ;
    int i = 0 ;
    while ( s[i] != 0 )
    {
        printf ( "%d", s[i]); // this will print decimal values of
                              // all characters before `\0`
        ++i;
    }
    printf ("%d", s[i]); // this will print decimal value of `\0`
}

You can also do this:

printf("%d\n", '\0');

and it will answer your first question.

If yes, then if 0 is in the middle of a string, then printf should end printing the string right there. e.g. printf("%s", "hello how0 are you") should print 'hello how'.

0 in "" or '' is treated as ASCII character thus try this:

printf("%d\n", '0');

that should give you decimal value of ASCII character '0'. and take a look at ASCII character table.

As fun exercise you can do this:

printf("%d\n", '0' - '\0');
2
votes

'\0' and 0 have different types, character and integer (also see the comments below). But C treats them as equal, because ASCII (and UTF-8, etc.) character encoding defines the value of the '\0' character as zero, so in C it can be implicitly converted into an integer with value zero, and vice versa. Because of this, if you set a character of a string to 0 (instead of '\0'), it will also terminate the string.

Test code (online):

#include <stdio.h>

int main() {
    // Are they equal? Yes!
    char zc = '\0';
    char zi = 0;
    printf("%d\n", zc == zi); // Prints 1
    
    
    // Does setting a char to 0 terminate a string? Yes!
    char s[] = "hello, world";
    printf("%s\n", s); // Prints hello, world
    s[5] = 0;
    printf("%s\n", s); // Prints hello
    
    return 0;
}

It's also important that a string literal, like s in the code above, has a terminating '\0' character, but it's not shown in the string literal.

And nothing stops you from adding internal '\0' characters! This might sound stupid, because the length of a string is defined as the number of characters until the first '\0', and printf also prints only these characters, but internal '\0's are really used in practice, e.g. when setting file type filters for the Open/Save File dialog on Windows (link, link). Here the internal zero characters are used as filter delimiters:

ofn.lpstrFilter = "Text files (*.txt)\0*.txt\0All files (*.*)\0*.*\0";

enter image description here

0
votes

In C, for each character, there is a corresponding ASCII value for it, which is basically an integer number between 0 and 127.

For example, when you declare:

char a[] = "Hello";

Then, basically the array is having 6 elements, not 5. You can test it using:

int n = sizeof(s)/sizeof(s[0]);
printf("%d\n", n);

The actual representation of that string has become: Hello\0

Here, you can see \ sign, which is known as Escape Character, which resembles an alternative interpretation of that character. Here \0 represents ASCII value 0 on that index of char array. But, without the escape character 0 is just a normal character - not an alternate representation.

Now if you want to view the corresponding ASCII value of each character of previous string - Hello, run this:

int i;
for (i = 0; i < 6; i++) {
    printf("%d ", s[i]);
}

// Output: 72 101 108 108 111 0 

Now about your original question:

Does '\0' and 0 same in C?

ASCII 0 and char 0 are different. But char \0 represents ASCII value of 0.