In C, why do these two pieces of code give the same output?
#include<stdio.h>
int main(void)
{
const char c='\?';
printf("%c",c);
}
and
#include<stdio.h>
int main(void)
{
const char c='?';
printf("%c",c);
}
I understand that a backslash is used to make quotes (" or ') and a backslash obvious to the compiler when we use printf(), but why does this work for the '?'?
\? is an escape sequence exactly equivalent to ?, and is used to escape trigraphs:
#include <stdio.h>
int main(void) {
printf("%s %s", "??=", "?\?="); // output is # ??=
}
Quoting C11, chapter §6.4.4.4p4
The double-quote " and question-mark ? are representable either by themselves or by the escape sequences \" and \?, respectively, but ... .
Emphasis mine
So the escape sequence \? is treated the same as ?.
Because '\?' is a valid escape code, and is equal to a question-mark.
when you're defining a char or string the compiler parses backslash in that char or string as an escape sequence.
**
the simple answer of your question is
\? means ?. instead of using \? you can using ? .
\? is escape representation and ? is character representation means both are same.
i have linked a image so that you understand it more easily..
**
"click here to see the image " --> in this image you need to find \? in Escape character
Related
When I printf following code, I get hex escape sequence out of range. \x1b is numeric escape for ESC. If my string is literally the 1ESC1, how to represent it?
#include <stdlib.h>
#include <stdio.h>
int main(void){
printf("1\x1b1\n");
return EXIT_SUCCESS;
}
"\x1b is numeric escape for ESC" is true, but the hex escape sequence here is \x1b1, not \x1b.
Avoid printf(st) to print a string st. The first argument to printf() is for a format. OK in OP's case, but better to avoid that maybe some escape sequence was used that was the same as %.
Try puts("1" "\x1b" "1");.
suggest correcting the syntax:
printf("1\x1b\1\n");
In C, on mentioning three backslashes, like so:
#include <stdio.h>
int main() {
printf(" \\\ ");
}
prints out just one backslash in the output. Why and how does this work?
That sequence is:
A space
A double backslash, which encodes a single backslash in the runtime string
A backslash followed by a space, which is not a standard escape sequence and should give you a diagnostic
The C11 draft says (in note 77):
The semantics of these characters were discussed in 5.2.2. If any other
character follows a backslash, the result is not a token and a diagnostic is
required.
On godbolt.org I got:
<source>:8:14: warning: unknown escape sequence '\ ' [-Wunknown-escape-sequence]
So you seem to be using a non-conforming compiler, which chooses to implement undefined backslash sequences by just letting the character through.
That is printing:
space
slash
escaped space
The 3rd slash is being interpreted as "slash space"
C11; 6.4.4.4 Character constants:
The double-quote " and question-mark ? are representable either by themselves or by the
escape sequences \" and \?, respectively, but the single-quote ' and the backslash \
shall be represented, respectively, by the escape sequences \' and \\.
So, To represent a single backslash, it’s necessary to place double backslashes \\ in the source code. To print two \\ you need four backslash \\\\. In your code extra \ is a space character, which isn't valid.
It is indeed a very simple operation in c. A \ is just a escape sequences. Hence below statement will print two slash.
printf(" \\\\ ");
For example some characters in c are represented with a slash like end of a line character \n or end of a string character \0 etc. But if you want to print such a character as it is what will you do? Hence you need to add a escape sequence character in front of it:
printf("\\n"); // will print \n
But
printf("\n"); // will print end of character hence you don't see anything in output
I am trying to print a parenthesis using:
printf("\)");
However, it is giving me the following warning:
warning: unknown escape sequence '\)'
I can't seem to find a clear explanation anywhere on how to fix this.
I realize it's just a warning, but since it's still treating that as a parenthesis it's throwing off all my other parentheses and giving me errors so that the code does not compile.
EDIT: Treating it as a regular character and just saying printf(")") is not working. It's still mismatching all the parentheses and I have gone through multiple times to make sure I'm not actually missing any.
The warning is coming from the C compiler. It is telling you that \ is not a known escape sequence in C. You need to double-escape the slash, like so: \\
Edit: if you just want to print the parenthesis, i.e. the ) then drop the slash altogether and use:
printf(")");
try this:
#include <stdio.h>
int main()
{
printf("Printing quotation mark \")\" ");
}
you need to add an escape character to get the quote to print which in this case is \"
This will result in Printing quotation mark ")"
just write parenthesis in double quote " " ,because parenthesis is not a escape character .
try this :
#include<stdio.h>
int main(){
printf( "( )" ); // print parenthesis here
}
Hope this helps.
Using variables seems to be a viable solution using my compiler.
#include <stdio.h>
int main() {
char var = ')';
printf("Hello, World!\n");
printf("Success :%c",var); //As you can see this is one way to go about the problem
return 0;
}
#include<stdio.h>
#include<conio.h>
FILE *fp;
int main()
{
int val;
char line[80];
fp=fopen("\Users\P\Desktop\Java\a.txt","rt");
while( fgets(line,80,fp)!=NULL )
{
sscanf(line,"%d",&val);
printf("val is:: %d",val);
}
fclose(fp);
return 0;
}
Why is there a compile error in the line fp=fopen("\Users\P\Desktop\Java\a.txt","rt")?
Escape your backslashes.
fp=fopen("\\Users\\P\\Desktop\\Java\\a.txt","rt");
xx.c:8:12: error: \u used with no following hex digits
fp=fopen("\Users\P\Desktop\Java\a.txt","rt");
^
xx.c:8:12: warning: unknown escape sequence '\P'
xx.c:8:12: warning: unknown escape sequence '\D'
xx.c:8:12: warning: unknown escape sequence '\J'
The issue with the backslash. Backslash is an escape in a C char string.
Try this
fp=fopen("\\Users\\P\Desktop\\Java\\a.txt","rt");
or this depending on your OS:
fp=fopen("/Users/P/Desktop/Java/a.txt","rt");
You may be familiar with how "\n" (newline) and "\t" (tab) are used in C-strings.
The compiler will look at any \<Character> and try to interpret it as an Escape-Sequence.
So, where you wrote "\Users\P\Desktop\Java\a.txt", the compiler is trying to treat
\U, \P, \D, \J and \a as special escape-sequences.
(The only one that seems to be valid is \a, which is the Bell/Beep sequence. The others should all generate errors)
As others have said, use \\ to insert a literal Backslash character, and not start an escape sequence.
P.S. Shame on you for not including the the compiler message in your question.
The worst questions all say, "I got an error", without ever describing what the error was.
How does the following code compile correctly,
#include <stdio.h>
#define stringer( x ) printf_s( #x "\n" )
int main() {
stringer( "In quotes when printed to the screen" );
}
isn't it supposed to get expanded into
printf_s(""In quotes when printed to the screen""\n");
which is an error as there are nested double quotes in printf_s??
No, the # operator handles character string literals specially. It must \ escape each " in a character string literal that is passed to it. The correct expansion is:
printf_s( "\"In quotes when printed to the screen\"" "\n" );
No, it's expanded into
printf_s("\"In quotes when printed to the screen\"" "\n");
which will finally be
printf_s("\"In quotes when printed to the screen\"\n");
and should print
"In quotes when printed to the screen"
In C, adjacent string literals are concatenated:
Adjacent string literals are concatenated at compile time; this allows long strings to be split over multiple lines, and also allows string literals resulting from C preprocessor defines and macros to be appended to strings at compile time: