Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is this C program detecting two '\0' characters in a string when an unconnected line of code gets uncommented?

I am currently learning C and I wrote this program to check if the ‘\0’ char is really at the end of strings, as pointed in "K and R".

I had the strangest result though.

If I comment the "int lista[] = {0, 1, 2, 3, 4};" statement out of the program (this is a statement that has nothing to do with the other statements of this program, it was part of another test I was going to make).
the output of the program comes out how expected, detecting one ‘\0’ char ending the string.
However, if I leave the statement uncommented, the program output detects two ‘\0’ chars at the end of the string.
Why does this happens?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the program with the statement uncommented:

#include <stdio.h>

int main(void)
{
    int lista[] = {0, 1, 2, 3, 4};
    char string[] = "linhas";
    
    for (int i = 0; i <= sizeof(string); i++)
    {
        if (string[i] != '\0')
        {
            printf("%c\n", string[i]);
        }
        else
        {
            printf("this dawmn null char\n");
        }
    }
}

This outputs:

l
i
n
h
a
s
this dawmn null char
this dawmn null char

this is the program with the line commented out:

#include <stdio.h>

int main(void)
{
    /*int lista[] = {0, 1, 2, 3, 4};*/
    char string[] = "linhas";

    for (int i = 0; i <= sizeof(string); i++)
    {
        if (string[i] != '\0')
        {
            printf("%c\n", string[i]);
        }
        else
        {
            printf("this dawmn null char\n");
        }
    }
}

it outputs:

l
i
n
h
a
s
this dawmn null char

>Solution :

Your loop

for (int i = 0; i <= sizeof(string); i++)

is ever-so-slightly wrong. It should be

for (int i = 0; i < sizeof(string); i++)

By using <=, you make one too many trips through the loop, and you access memory outside of the string array. It looks like, with the lista array in place, the extra byte you mistakenly access (outside of the string array) happens to be a 0, so you get an extra, second printout of your "this dawmn null char" message.

But then, when you comment out the lista array, it must be the case that the extra byte you mistakenly access isn’t 0, so it gets printed as itself, instead. It might be an invisible control character, which is why you don’t see anything. I suggest changing your code to

if (string[i] != '\0')
     printf("string contains %d\n", string[i]);
else printf("this damn null char\n");

to see this more clearly.

The important lesson here is that if you have a loop that’s supposed to run N times, there are two ways to write it. In C, the vast majority of the time, you want to write it as

for(i = 0; i < N; i++)

That’s a "0-based" loop, that runs from 0 to N-1, for a total of N trips. Once in a while, you want a 1-based loop:

for(i = 1; i <= N; i++)

This runs from 1 to N, again for a total of N trips. But if you write

for(i = 0; i <= N; i++)      /* usually WRONG */

your loop runs from 0 to N, for a total of N+1 trips.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading