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
Hello
Hello\0world
Why two characters \and 0are interpreted as the final character in the string in the first and not the second?
\
0
\0 - escape-, , , . ; \0 , \ 0.
\0
, "Hello\0World" scanf, .
scanf
\0 , ascii 0. escape- . .
, "Hello\0World" Hello\0 World
Hello\0 World
\0 - . , , , (\n) , . , \ 0, . "" \0 , NUL ?
\n
! , 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); }
char str[]="Hello\0World" '\0' NULL. '\ 0' .
'\0'
scanf() .
scanf()
Common '\0'is just one character. '\'used for ignorance
'\'
In the first case:
the compiler interpreted the string and placed the null terminator ( \0) on the string.
In the second case:
You entered a string, and there was an interpretation of the compiler. This is the same as if you entered:
char str[]="Hello\\\0World"
Source: https://habr.com/ru/post/1568896/More articles:Add phparray variable inside javascript dynamically - jqueryvim: what's the difference between buffers and args - vimDownload sqlite file - phpIdentify multiple ports from ManagedVM - google-app-engineSimulation of an incoming call or SMS in Android Studio - androidThe header is not allowed on the server? - phpAndroid login and registration error - javaMultiple Mailchimp subscription forms for one list - javascriptide-haskell doesn't work for atom editor - haskellКинжал: переопределить @метод-метод в областном модуле - dependency-injectionAll Articles