Im just curious why there is a need to escape a single quote character when you could actually print a single quote in the printf function without having the need to escape it.
Asked
Active
Viewed 158 times
0
-
2In printf, you can print `'` without or with escaping. But for single character `'`, you need to escape it. – Mohit Jain Aug 13 '14 at 11:39
-
2`printf("%c\n", ''');` is invalid.(`empty character constant`and extra `'`) this should be `printf("%c\n", '\'');` but simply `printf("'\n");` is valid. – BLUEPIXY Aug 13 '14 at 11:40
-
On the other hand (and to the OP's defense), other languages can do without an escape character: [Python](http://stackoverflow.com/questions/9050355/python-using-quotation-marks-inside-quotation-marks), [VBA](http://stackoverflow.com/questions/9024724/how-do-i-put-double-quotes-in-a-string-in-vba). Then again, since in C you already need an escape for several other purposes, it's just easier to tack this on, rather than to invent explicit (other) rules for quotes only. – Jongware Aug 13 '14 at 11:51
-
3It's the same the other way round: You must escape `"` in string literals but not in character constants, `"\""`, `'"'`, `'\"'` are all valid, but `"""` is not. – mafso Aug 13 '14 at 11:53
-
You have a great point there.. thanks! – Aug 13 '14 at 12:39
3 Answers
4
To allow for character initializations like this:
char quote = '\'';

mcleod_ideafix
- 11,128
- 2
- 24
- 32
1
If you want to embed a "
in a string or have a character like '
, you need to escape.
You can workaround this if you only intend to use printf()
, but for general use, you might want to need both.
For example, you might want to send the string I said "hello"
via a socket connection, or write it into a file, then you'd have to make ugly hacks because you are restricted if you couldn't do so.

glglgl
- 89,107
- 13
- 149
- 217
0
Suppose you want to compare a character value with single quote. In this case you need something like if(c == '\'')
.

HaMi
- 539
- 6
- 23