0

So i was running some tests and there was strange behavior going on whenever I tried to change a character from a to 0. Instead of changing the element in the array like I expected, the entire array was deleted. Here is what I used to debug

#include <stdio.h>

int main()
{
    char test[] = "aa432aa";
    for(int i = 0; i<7; i++){
        printf("%s\n", test);
        if(test[i] == 'a'){
            test[i] = 0;
            printf("an a\n");
        }
        else{
            printf("not a\n");
        }
    }
    printf("%s", test);
}

And here is the output:

aa432aa                                                                                    
an a                                                                                       
                                                                                           
an a                                                                                       
                                                                                           
not a                                                                                      
                                                                                           
not a                                                                                      
                                                                                           
not a                                                                                      
                                                                                           
an a                                                                                       
                                                                                           
an a                                                                                       
       
2
  • 6
    Are you trying to set it to the digit zero? Use '0' instead of 0 Commented Feb 10, 2021 at 3:17
  • 1
    When you do: test[i] = 0; instead of test[i] = '0', you are setting the string terminator character which is 0x00 and not 0x30, the ASCII code for '0' [as you probably want]. So, the final printf will stop much earlier than you expect because it stops when it sees 0x00 Commented Feb 10, 2021 at 3:23

2 Answers 2

2

As pointed out in the comments you should use test[i] = '0' not test[i] = 0, because '0' is the 0 character, while 0 is equal to '\0'. When printing, '\0' is treated as the final character of the string, so that's why, after changing the first 'a' to 0('\0'), you're not able to print it correctly.

Code:

#include <stdio.h>

int main()
{
    char test[] = "aa432aa";
    for(int i = 0; i<7; i++){
        printf("%s\n", test);
        if(test[i] == 'a'){
            test[i] = '0'; // Here!
            printf("an a\n");
        }
        else{
            printf("not a\n");
        }
    }
    printf("%s", test);
}

Output:

aa432aa
an a
0a432aa
an a
00432aa
not a
00432aa
not a
00432aa
not a
00432aa
an a
004320a
an a
0043200
Sign up to request clarification or add additional context in comments.

Comments

0

This is because ASCII value 0 is actually equal to \0 and \0 is indicating the end of string in C. In the for loop when you set test[i] = 0;, this means it will convert the value of the test variable into "\0a432aa". So when you are trying to print it, it found an ending character at the beginning and do not able to print anything. To clear the idea, try to print a string like "xx\0a432aa", and it will only print "xx".

I think you are trying to put '0' instead of 'a'. If that is the case, your code should be like this:

#include <stdio.h>

int main()
{
    printf("\'\\0\' : %d\n", '\0');
    printf("0 : %d\n", 0);
    
    char t[] = "xx\0a432aa";
    printf("%s\n", t);

    char test[] = "aa432aa";
    for(int i = 0; i<7; i++){
        printf("%s\n", test);
        if(test[i] == 'a'){
            test[i] = '0';
            printf("an a\n");
        }
        else{
            printf("not a\n");
        }
    }
    printf("%s", test);
}

I also add 4 initial lines to give you intuition about the problem you are facing here. For further understanding you can check this post from stackoverflow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.