Forum

Hilarious printf solution

Discuss programming topics for the various GPL'd game engine sources.

Moderator: InsideQC Admins

Hilarious printf solution

Postby Baker » Wed Apr 06, 2016 12:40 pm

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 ..
User avatar
Baker
 
Posts: 3661
Joined: Tue Mar 14, 2006 5:15 am

Re: Hilarious printf solution

Postby frag.machine » Wed Apr 06, 2016 1:01 pm

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)
User avatar
frag.machine
 
Posts: 2052
Joined: Sat Nov 25, 2006 1:49 pm

Re: Hilarious printf solution

Postby Baker » Wed Apr 06, 2016 1:32 pm

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 ..
User avatar
Baker
 
Posts: 3661
Joined: Tue Mar 14, 2006 5:15 am

Re: Hilarious printf solution

Postby Spike » Wed Apr 06, 2016 10:49 pm

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.
Spike
 
Posts: 2874
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: Hilarious printf solution

Postby Baker » Thu Apr 07, 2016 12:29 pm

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 ..
User avatar
Baker
 
Posts: 3661
Joined: Tue Mar 14, 2006 5:15 am


Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 1 guest