views:

161

answers:

5

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.

+7  A: 

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.

Mehrdad Afshari
Per 2.3 Trigraph sequences and 2.5 Alternative tokens, a replacement for double-quote definitely doesn't exist.
Roger Pate
Trigraph was the word I was after. Haven't heard that one in about a decade. Thanks.
+3  A: 

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

Carl Norum
+4  A: 

you are thinking of trigraphs

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

but " isnt on the list

pm100
+6  A: 

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
How would that help if the quotes are stripped?
Michael Myers
You mean QUTOE(hello). +1
Notinlist
Do you mean `#define QUOTE(x) #x`? AFAIK, your current macro outputs `"x"` string for all inputs.
Mehrdad Afshari
@Keep the header separate and add a `#include` when you generate the C++ files. The preprocessor should take care of quoting.
dirkgently
@Mehrdad Afshari: Yes, of course.
dirkgently
You also need to change `QUOTE("hello")` to `QUOTE(hello)`. How do you construct the string `" "`?
avakar
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.
Roger Pate
@Roger: He could copy-paste it from here.
Mehrdad Afshari
@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.
Roger Pate
@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 ;)
Mehrdad Afshari
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.
@user30997 You can recurse it: #define QUOTE_VALUE_OF(x) QUOTE(x)
Arthur Shipkowski
@Arthur: that makes it worse, not better: http://codepad.org/8CSOBPFd @user30997: sure about that?
Roger Pate
+1  A: 
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
Better: `const char foo[]`
mark4o
@mark: why is that better?
Roger Pate
@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
@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.
Roger Pate
@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
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