6

As I was trying to understand more 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 that in the first example I got Hello and in the second I got Hello\0world

why are the two characters \ and 0 interepreted as an end character of string in the first and not in the second one ?

tissa
  • 396
  • 2
  • 13
  • I don't see two strings here. – wingerse Dec 24 '14 at 14:50
  • 2
    The C compiler will make it a single character. The scanf() function doesn't know beans about escape sequences, it sees two characters. – Hans Passant Dec 24 '14 at 14:51
  • one _(comment_padding)_ – bolov Dec 24 '14 at 14:52
  • 1
    possible duplicate of [What does the \0 symbol mean in a C string?](http://stackoverflow.com/questions/4711449/what-does-the-0-symbol-mean-in-a-c-string) – Cliff Ribaudo Dec 24 '14 at 14:52
  • 1
    I am asking actually about the different behavior and not in the meaning of the character \0 !! @CliffRibaudo – tissa Dec 24 '14 at 14:55
  • The behavior IS a direct result of the MEANING of '\0' which is an explicit NULL in c strings. – Cliff Ribaudo Dec 24 '14 at 14:57
  • @HansPassant I think you are wrong because when you consider this code `scanf("%[^\n]s",str)` you will realize that scanf know about the escape sequences !! – tissa Dec 24 '14 at 14:58
  • You have essentially entered the string `"Hello\\0world"`. Type `Hello`, then Ctrl+Space, then `world`. – barak manos Dec 24 '14 at 15:05
  • 1
    @tanit Perhaps you'd be better off trying to understand what Hans means. The compiler interprets escape characters in literals. But `scanf` doesn't do so for the text that you enter. – David Heffernan Dec 24 '14 at 15:07
  • @DavidHeffernan I totally agree on that !! but maybe I misunderstood his comment as it was implicit ! :) – tissa Dec 24 '14 at 15:12

6 Answers6

12

\0 is an escape sequence, and while it consists of two characters in the source file, it is interpreted as a single character in the string, namely the null character. However, this special interpretation only happens in the source file; if you input \0 when you run the program, it gets interpreted literally as the two characters \ and 0.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
1

Because when you enter "Hello\0World" for the scanf then you don't enter the same.

When you use \0 in the code that means a character with ascii code 0. Whereas when you're prompted these escape sequences are not interpreted. You actually entering a backslash and a zero character.

So your input will be equal to "Hello\0World" not Hello\0 World

fejese
  • 4,601
  • 4
  • 29
  • 36
1

\0 is one character. In first case it is written in code so treated as special sign, like new line (\n) or similar, so printing function stops here. In second case you are literally inputting two separate characters in one byte you have \ and in second 0 so it is not threaten as termination sign. If you want to place "real" \0 from keyboard look here How to simulate NUL character from keyboard?

Community
  • 1
  • 1
Kuba
  • 839
  • 7
  • 16
1

This is a really good question! My assumption is that when you declare the string on line 3, as expected, the compiler is able to identify that there is an escape character and allocate the \0 appropriately. Next when you are reading from scanf, because your code is already compiled, the program has no way of knowing what characters will be entered and therefore doesn't treat the \ as an escape character and just treats it as a slash.

Here is a sample code I wrote based off of yours to try and solve this problem:

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

In your first statement, char str[]="Hello\0World" '\0' is a NULL terminated. Strings consider '\0' as the end of the string.

In your second statement, scanf() takes the input until space occurs.

Overall '\0' is just a single character. '\' is used for ignorance

Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42
  • C-"strings" or terminated by the `NUL` character (aka `'\0'` aka `null`). `NULL` (Capitals with **two** ells) is something different. – alk Dec 24 '14 at 16:12
0

In the first case:

char str[]="Hello\0World"

the compiler has interpreted the string and place a null terminator (the \0) in the string.

In the second case:

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

You've entered the string, and there's been do compiler interpretation. It's the same as if you'd entered:

char str[]="Hello\\\0World"
Sean
  • 60,939
  • 11
  • 97
  • 136