Fun with C

Jul. 27th, 2006 04:08 pm
jgrafton: (tj csl terminal)
[personal profile] jgrafton
A fun way to make your C (C99, really) code slightly less readable:

If you need to make a multiline comment, you would typically either use the /* */ comment style or prefix each line with //, as newly defined in the C99 standard.

For example,
/* This is
   some longer comment
*/

// This is also
// a multiline comment


But the more fun way is to use a combination of the newer //-style comment combined with \.

For example:
// This is also a \
   multiline comment \
   though much less obviously \
   so


If you use gcc with -Wall it'll complain at you, but otherwise it works perfectly fine. Yay C!

(I happened to discover this by accident when I ended a //-style comment with a :\ smiley and suddenly things broke.)

(no subject)

Date: 2006-07-27 11:30 pm (UTC)
From: [identity profile] twizmer.livejournal.com
Point 1: Wait, everybody doesn't already know about using \ to extend lines?
Point 2: Is that really less readable?
Point 3: Real men comment C code with #if 0

(no subject)

Date: 2006-07-28 01:20 am (UTC)
From: [identity profile] physics-dude.livejournal.com
From what Wikipedia says is the current C99 spec:

"Except within a character constant, a string literal, or a comment, the characters // introduce a comment that includes all multibyte characters up to, but not including, the next new-line character. The contents of such a comment are examined only to identify multibyte characters and to find the terminating new-line character." (page 78)


Bad gcc. No biscuit.

(no subject)

Date: 2006-07-28 03:12 am (UTC)
From: [identity profile] brpxqzme.livejournal.com
By golly, -pedantic doesn't spit any errors!

// Now that's a comment that deserves an =O

(no subject)

Date: 2006-07-29 03:36 am (UTC)
From: [identity profile] brpxqzme.livejournal.com
I meant -pedantic -std=c99

GNU standards allow // comments in normal ol' C code, for example. You gotta say -ansi or -std=c99 to really say it's non-standard. The point still stands though... then again, I bet some people who put messages in ./configure scripts that make out to something like "GCC WILL DESTROY ALL FOREIGN CC". Nice find.

(no subject)

Date: 2006-07-28 02:09 am (UTC)
From: [personal profile] chrisamaphone
Aw, I'm sorry your code was depressing enough to merit a :\.

Profile

jgrafton: (Default)
Jeff Grafton

January 2017

S M T W T F S
1234567
891011 12 1314
15161718192021
22232425262728
293031    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags