Hilarious printf solution
Moderator: InsideQC Admins
5 posts
• Page 1 of 1
Hilarious printf solution
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
Or leading and trailing ...
printf ("I think his name was" _QUOTED_ "bob", name);
Or just trailing ...
printf ( QUOTED_ "is his name", name);
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?
Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
-

Baker - Posts: 3666
- Joined: Tue Mar 14, 2006 5:15 am
Re: Hilarious printf solution
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:
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)
-

frag.machine - Posts: 2090
- Joined: Sat Nov 25, 2006 1:49 pm
Re: Hilarious printf solution
No argument here. printf in C is not nice at all.
The night is young. How else can I annoy the world before sunsrise?
Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
-

Baker - Posts: 3666
- Joined: Tue Mar 14, 2006 5:15 am
Re: Hilarious printf solution
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.
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: 2892
- Joined: Fri Nov 05, 2004 3:12 am
- Location: UK
Re: Hilarious printf solution
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?
Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
-

Baker - Posts: 3666
- Joined: Tue Mar 14, 2006 5:15 am
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest