3

In C it is not normally possible to use ' for printf of a string. However, I have text which are full of double quote ", and I need to escape all of them as

printf("This is \"test\" for another \"text\"");

Is it possible to printf in a way without escaping ". I mean using another character for wrapping the string.

verbose
  • 7,827
  • 1
  • 25
  • 40
Googlebot
  • 15,159
  • 44
  • 133
  • 229

6 Answers6

5

Not recommended, but you can use a macro:

#include <stdio.h>
#define S(x) #x

int main() {
    printf(S(This "is" a string (with nesting)!\n));
}

This prints

This "is" a string (with nesting)!

Now the delimiters are balanced () characters. However, to escape single ), ", or ' characters, you have to write something like S(right paren: ) ")" S(!\n), which is quite ugly. This technique is not recommended for writing maintainable code.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • `S(This "is" a)" (maybe the) "S(way of doing "it".)` – jxh Aug 20 '13 at 01:57
  • @jxh: Yeah, I guess that's it. (If you have *balanced* parens, it actually works nicely without having to escape, so I guess the caveat isn't that big of a deal). – nneonneo Aug 20 '13 at 01:58
  • I was surprised to learn that escapes work "as expected". Is that actually standard or just a `gcc`-ism? Hmm... – nneonneo Aug 20 '13 at 02:01
  • 1
    I would recommend you use the macro for those parts that need the quoting: `printf("This is %s for %s.\n", S("test"), S("text"));` – jxh Aug 20 '13 at 02:02
  • Escapes working is per standard. C.99 §6.10.3.2: *a \ character is inserted before each " and \ character of a character constant or string literal (including the delimiting " characters)* – jxh Aug 20 '13 at 02:04
  • Doesn't work. Print something out with with: echo "Text" | cowsay -f eyes | toilet --gay -f term > deleteme.txt. Now try and print that text with your scheme. – Owl Oct 02 '16 at 18:30
  • @Owl: this solution changes the string delimiter from double quotes to balanced parens (noting that quotes still need to be balanced). If your parens aren't balanced then this solution still won't work for you. – nneonneo Oct 03 '16 at 05:52
2

No, that is not possible in the C language. There is only one syntax for string literals, and that is that they are delimited by double quotes.

The only way to write unescaped quotation marks is as character literals inside character arrays, which is uglier and more difficult to write, so there's very little reason to do so in a case like this:

char array[] = {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', '"'};  // etc.
printf("%s", array);
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
2

No there is not other way, the draft C99 standard in section 6.4.5 String literals has the following grammar:

string-literal:
  " s-char-sequenceopt "
  L" s-char-sequenceopt "
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
1

No, it's not possible in standard C.

C11 6.4.5 String literals

The same considerations apply to each element of the sequence in a string literal as if it were in an integer character constant (for a character or UTF−8 string literal) or a wide character constant (for a wide string literal), except that the single-quote ' is representable either by itself or by the escape sequence \', but the double-quote " shall be represented by the escape sequence \".

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

First of all, separate a program's requirements from the solutions to meet those requirements. Given the minimum amount of info. in this question, the requirement is to print, using C, a string that has double quotes. There are several ways to do this in C.

For example, the following code fragment:

char string[] = "This string \" has one double quote.";

printf("This string %cprints%c with %cdouble%c quotes", '"', '"', '"', '"');
printf("%s", string);

produces:

This string "prints" with "double" quotes.
This string " has one double quote.

Your application might have more requirements that you have not mentioned, but it should be possible to achieve what you want, just NOT the way you initially believe it should be done (welcome to the world of "Needs Analysis").

JackCColeman
  • 3,777
  • 1
  • 15
  • 21
0
//R "delimiter( raw_characters )delimiter"

printf(R"SOME/\STRING");

Raw string will terminate after the first )" it sees.

Therefore, if )" is in the string, you have to add delimiter ("a" is used below)

/* Print dog without any escape. */
printf(R"a(|\_/|
|q p|   /}
( 0 )"""\
|"^"`    |
||_/=\\__|)a");
}

It's C++11 feature and you can find more information in document

Simliar question have been answered escape R"()" in a raw string in C++

Sean
  • 489
  • 1
  • 8
  • 29