Hilarious printf solution

Discuss programming topics for the various GPL'd game engine sources.
Post Reply
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Hilarious printf solution

Post by Baker »

I hate this ...

printf ("name is \"%s\"", name)

I hate writing it. I hate reading it. Messing up the escapes seems to happen some of the time.

My solution:

printf ("name is" _QUOTED, name); // _ means leading space

Code: Select all

#define _QUOTED_ " \"%s\" " // leading and trailing space form
#define _QUOTED " \"%s\""   // leading space form
#define QUOTED_ "\"%s\" " // trailing space form 


Or leading and trailing ...

printf ("I think his name was" _QUOTED_ "bob", name);

Or just trailing ...

printf ( QUOTED_ "is his name", name);
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
frag.machine
Posts: 2126
Joined: Sat Nov 25, 2006 1:49 pm

Re: Hilarious printf solution

Post by frag.machine »

I used to suffer a lot with missing quotes in my beginning in C but nowadays muscle memory (most of time) protects me from silly mistakes.
But TBH when you work with languages that support string builders you realize how ugly printf is:

Code: Select all

  StringBuilder sb= new StringBuilder();
  sb
    .append(player.netname)
    .append(" left the game with ")
    .append(player.frags)
    .append(" frags.\n");

System.out.println (sb.toString());
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Hilarious printf solution

Post by Baker »

No argument here. printf in C is not nice at all.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Hilarious printf solution

Post by Spike »

raw strings:
printf(R"(Hello "%s", your file is in "c:\program files\etc" or something. crap example)", name);
good for regex, but % still needs to be doubled up.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Hilarious printf solution

Post by Baker »

Looks like a gnu extension and a C++ thing so might actually work with most of the C compilers anyone actually uses including Microsoft's, despite not being part of the C language as far as I have been able to determine.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Post Reply