NMilev NMilev - 25 days ago 10
C Question

Defining a replacement for punctuators in C

Several days ago, someone told me that their keyboard is faulty, leaving them unable to type

(
on their keyboard. The first thing that came to my mind, when writing C, was to either copy the character from somewhere and just paste it or to try to use preprocessor directive
#define
. Once I tried to use
#define
, I realised that gcc doesn't let me write something like
#define OB (
and I pretty much understand why. Is it possible to write something similar to this and let me replace a punctuator using
#define
?

Answer

If there was a trigraph for ( I would suggest that. Otherwise your solution with a macro might work, but unfortunately when written in C or a header file it can not be parsed as you want it. However using gcc you can specify that macro directly using the -D option.

Example:

int main OB ) {
    printf OB "Hello, world\n");
}

I can compile it using the following command: cc -o test test.c -DOB='('. I needed to add ' otherwise bash wouldn't accept that.

You can also use make adding the extra parameter to CPPFLAGS: CPPFLAGS+="-DOB='('" make test - again you need more quotes than usual otherwise all the shells would not understand unquoted (. Of course you can just add CPPFLAGS+=-DOB='(' directly to the Makefile.

Edit: In fact you can still have a macro in code. But you have to tell gcc that ( is not a part of macro identifier. The following worked for me:

#define OB \
    (
int main OB ) {
    printf OB "Hello, world\n");
}