Does the final character `\ 0` count as one character or two characters?

Since I was trying to better understand the behavior of some functions, I took two examples:

char str[]="Hello\0World"

and

char str[100];
scanf("%s",str);// enter the same string "Hello\0world"

The problem here is that in the first example I got Hello, and in the second I gotHello\0world

Why two characters \and 0are interpreted as the final character in the string in the first and not the second?

+4
source share
6 answers

\0 - escape-, , , . ; \0 , \ 0.

+10

, "Hello\0World" scanf, .

\0 , ascii 0. escape- . .

, "Hello\0World" Hello\0 World

+1

\0 - . , , , (\n) , . , \ 0, . "" \0 , NUL ?

+1

! , 3, , , escape- \0. , scanf, , , , \ escape- .

, , :

#include<stdio.h>
int main(){
  char str1[]="Hello\0World";
  printf("str1 = %s\n",str1);
  char str2[100];
  scanf("%s",str2);
  printf("str2 = %s",str2);
}
+1

char str[]="Hello\0World" '\0' NULL. '\ 0' .

scanf() .

Common '\0'is just one character. '\'used for ignorance

0
source

In the first case:

char str[]="Hello\0World"

the compiler interpreted the string and placed the null terminator ( \0) on the string.

In the second case:

char str[100];
scanf("%s",str);// enter the same string "Hello\0world"

You entered a string, and there was an interpretation of the compiler. This is the same as if you entered:

char str[]="Hello\\\0World"
0
source

Source: https://habr.com/ru/post/1568896/


All Articles