Why is one single function \ printf not showing up on the output console screen, but double \\ does it?
Why is the single one \
in the function printf
not showing up on the output console screen, but double \\
does?
#include <stdio.h>
#include <stdlib.h>
int main()
{
//why is double \\ necessary?
printf("\\");
printf("\");
//why here shown error in the second printf?
printf(" " "dd " " ");
//why its executed successfully
printf(" " "" ");
//why it is shown errow
return 0;
}
source to share
backslash is used for escape sequences.
An escape sequence is a sequence of characters that does not represent itself when used within a character or string literal, but translates to another character or sequence of characters that may be difficult or impossible to represent directly. (From Wikipedia)
In C, look at:
\ n for newline, \ t for tab. here the backslash is used as an escape sequence that does not represent itself
printf("\\");
here the first backslash is used as an escape sequence. so the second \ backslash will be printed. but
printf("\");
but for this case it's just an escape sequence. so it will show error
source to share
An operator \
in a statement is printf()
used to escape characters. For example, \n
means newline
, \0
means null character
. etc. So when you use just \
it expects some character to be escaped. In the second, the printf()
character to be escaped becomes "
, making your statement printf()
incomplete. So it shows an error whereas the first code is \
after the first \
, so it is treated as a character to be displayed, not an escape character.
From Wikipedia
An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is converted to another character or sequence of characters that may be difficult or impossible to represent directly.
source to share
because the backslash ie \ is used for escape sequences. These escape sequences are used to perform special tasks like formatting, playing a beep (\ a), and since it is treated as a special character, the compiler thinks you want to do some special task when you write "\" in printf. which means you can't print \ so, to print \ just type it twice "\"
source to share