Ravi
Ravi

Reputation: 303

using 0 instead of '\0' in C program

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

Upvotes: 3

Views: 1207

Answers (10)

user5550963
user5550963

Reputation:

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');

Upvotes: 5

UkFLSUI
UkFLSUI

Reputation: 5672

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, the array is actually 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.

Upvotes: 4

Steve Summit
Steve Summit

Reputation: 47933

'\0' is the character with the value 0. It is used to terminate strings in C. So, yes, the loop

while ( s[i] != 0 )

works perfectly fine, although the more conventional way of writing it is

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

to emphasize that we're working with characters.

On the other hand, '0' is the character that looks like the number 0, although the value of this character in the ASCII character set is 48. But in any case the value is not 0! So when you write

printf("%s", "hello how0 are you");

it prints hello how0 are you, because the 0 is an ordinary character, and does not terminate the string. If you wrote

printf("%s", "hello how\0 are you");

then you would have a null character, and it would terminate the string.

Upvotes: 5

Lundin
Lundin

Reputation: 213678

0 and '\0' are the same, but instead of just saying that, how about we explain what these different ways of typing zero actually are.

In C there are many situations where the integer value 0 can be used. Obviously, it can be used as a plain integer or array index. But it can also be used to create a null pointer. And yet another use is null termination of strings.

Since null pointers and null termination of strings are wildly different things, there's a big risk of mixing up the terms. At some point back in the dark ages, a distinction was made by referring to null pointers as NULL and creating a macro named like that, expanding to a null pointer constant. And the character 0 ("the null character") in the symbol table was referred to as NUL with a single L. Still confusing.

Furthermore, C never defined an escape sequence for null termination, as was done for line feed \n, tab \n and so on. So there existed no way to type the null termination character when part of a string.

Then someone (Kernighan & Ritchie?) came up with the idea to use other already existing escape sequences in C for this purpose. Inside string or character literals, we may use escape sequences to type out the index of a symbol in the table. This can be done using either octal or hexadecimal notation.

For example, the line feed character with decimal value 10 can be typed inside a string out as \012 (octal) or \xA (hex). These are called octal/hexadecimal escape sequences. Example:

puts("test\ntest\012test\xAtest");

gives

test
test
test
test

Similarly, we can write the null character using escape sequences, either octal \0 or hex \x0. The octal version became convention and de facto standard for null termination.

Upvotes: 5

kol
kol

Reputation: 28688

'\0' and 0 are equal in C, because the ASCII (ANSI, UTF-8 etc.) character encoding defines the code of the '\0' character as 0. In C, a character can be implicitly converted to an integer, and the resulting integer will be equal to the character code (for example, 'A' will be converted to 65).

If you set a character of a string to either '\0' or 0, it will 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, automatically has a terminating '\0' character, but it's NOT shown in the string literal.

Putting an internal '\0' character into a string literal 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

Upvotes: 6

Thibaud
Thibaud

Reputation: 1095

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

Upvotes: 4

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

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" 

Upvotes: 5

Eric Postpischil
Eric Postpischil

Reputation: 222382

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.

Upvotes: 5

Mohit kumar
Mohit kumar

Reputation: 155

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).

Upvotes: 10

MikeCAT
MikeCAT

Reputation: 75062

'\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.

Upvotes: 11

Related Questions