6

I've not had the Kernighan and Ritchie C reference in years, but I remember that there was a page in there that talked about how to enter characters that were unavailable to you. (WAY back in the day, some keyboards lacked characters like ", ~, etc.)

To be clear, let me give an example. I'm not looking for a way to get quotes in strings, but rather, I want to replace this:

printf("foo");

with this:

printf([alternate sequence]foo[alternate sequence]);

For the curious, I have an automated process that involves generating C/C++ code, but the (closed source) commercial tool involved strips quotes in its data streams and the documentation is quite clear on the fact that they do not provide a way to escape them.

EDIT:

Wow, I hadn't expected such a heavy response. This might merit a little more detail on my process. I'm doing automated build systems, which means that I live with certain restrictions when it comes to changing the code I'm compiling. For now, we have to live with the assumption that I have to get a string, spaces and all, into a preprocessor definiton. I already went down the 'PreprocessorDefinition' road. This left me with my usual fallback: Define the string in the operating environment and have the project file set the definition from there:

Preprocessor Definitions     WIN32;_DEBUG;THINGIE=$(THINGIE)

The hope was that I could get around MSVC's stripping of quotes in anything handed to the build with /D using a trigraph, by doing something like this in my build automation script:

ENV['THINGIE'] = "??''Yodeling Monkey Nuggets??''"
run_msbuild_command

I guess it's time for a plan C.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Sniggerfardimungus
  • 10,708
  • 10
  • 46
  • 91
  • Can you further process the output of the program? If you can do that, you can declare some rare character sequence for `"` and pipe the output of that program to `sed` which gives your double quotes back. – mmx Feb 05 '10 at 18:33
  • Time to think about generating a header that contains the quotes, and then including that where you need the pre-processor definition. – Jonathan Leffler Feb 05 '10 at 20:39

6 Answers6

10

You are looking for a trigraph for " character? I don't think one exists.

Trigraphs don't exist for all characters. Only a few characters have trigraph sequences.

mmx
  • 390,062
  • 84
  • 829
  • 778
8

None as per the standard. Try including a header with a macro:

 #define QUOTE(x) #x

and generate a printf as:

 printf(QUOTE(hello));
dirkgently
  • 101,474
  • 16
  • 123
  • 183
  • How would that help if the quotes are stripped? – Michael Myers Feb 05 '10 at 18:05
  • 4
    Do you mean `#define QUOTE(x) #x`? AFAIK, your current macro outputs `"x"` string for all inputs. – mmx Feb 05 '10 at 18:06
  • @Keep the header separate and add a `#include` when you generate the C++ files. The preprocessor should take care of quoting. – dirkgently Feb 05 '10 at 18:07
  • You also need to change `QUOTE("hello")` to `QUOTE(hello)`. How do you construct the string `" "`? – avakar Feb 05 '10 at 18:10
  • 3
    Now he just has to make sure and setup everything so he can #include without using quotes too. :) Better to define the QUOTE macro on the command line in this case. –  Feb 05 '10 at 18:12
  • @Roger: He could copy-paste it from here. – mmx Feb 05 '10 at 18:14
  • @Mehrdad: the problem is not typing the double-quote, the problem is using a tool that completely generates a source file and cannot emit this character. –  Feb 05 '10 at 18:18
  • @Roger: I see. I guess he could write his own simple preprocessor that replaces his custom trigraph and pipe the output to it--people have weird problems btw ;) – mmx Feb 05 '10 at 18:20
  • 1
    I upvoted this answer (I'm the orig. poster) because I'd already considered this path. It has the drawback that if the argument is itself a preprocessor definition, the name of the argument is printed, rather than the value. – Sniggerfardimungus Feb 05 '10 at 18:21
  • @user30997 You can recurse it: #define QUOTE_VALUE_OF(x) QUOTE(x) – Arthur Shipkowski Feb 05 '10 at 18:45
  • @Arthur: that makes it worse, not better: http://codepad.org/8CSOBPFd @user30997: sure about that? –  Feb 06 '10 at 02:07
4

you are thinking of trigraphs

 Character   Trigraph
 [           ??(
 \           ??/
 ]           ??)
 ^           ??'
 {           ??<
 |           ??!
 }           ??>
 ~           ??-
 #           ??=

but " isnt on the list

pm100
  • 32,399
  • 19
  • 69
  • 124
3

I think you're talking about trigraphs. As far as I've read, there is not one for the " character.

Carl Norum
  • 201,810
  • 27
  • 390
  • 454
0
but the (closed source) commercial tool involved strips quotes in its data streams and the documentation is quite clear on the fact that they do not provide a way to escape them.

Sounds like a crappy tool.

It looks ugly, but you could try something like this:

static const char foo[] = {'H', 'e', 'l', 'l', 'o', 0};

printf(foo);

I also like dirkgently's suggestion to use # in a macro, however I wonder how that would do with spaces?

asveikau
  • 35,672
  • 2
  • 48
  • 66
  • @mark4o - Sure. I originally had `static const char foo[]` (as I've seen some compilers generate code which will actually copy a large array onto the stack without the `static` in circumstances like this, even when `const`) but I thought it would be distracting to the actual idea -- and that it might provoke nitpicky comments, so I left it out. – asveikau Feb 05 '10 at 18:24
  • @asveikau: it is required to have a unique address when non-static, even if const, and since C-style strings are passed around as pointers often, it's much easier to copy than do full analysis to make sure a duplicated value won't matter. –  Feb 06 '10 at 02:13
  • @Roger Pate: No arguments here. I'm just writing this because I once declared a large lookup table inside a function, then was a bit startled/amused to look at the disassembly of that function. Since then I try to remember to make such things static. :-) But as is mentioned this is pretty tangential to the question. – asveikau Feb 06 '10 at 05:53
  • If it's a local variable then `static` can help avoid copying the data to the stack each time the function is called. If it is intended to behave like a string literal then `const` is good because the data will be in read-only memory like a real string literal, which prevents accidental overwrites, reduces the amount of data that must be written to disk when paging or dumping core, and increases the amount of data that can be shared between processes when there are multiple instances of the program running. – mark4o Feb 06 '10 at 07:35
-1

What do you think about using ´ instead of '?

I faced the same problem and tried to avoid it by replacing the text delimiter by something harmless.

Goodbye StackExchange
  • 21,680
  • 7
  • 47
  • 83
Volker
  • 373
  • 1
  • 3
  • 13