PDA

View Full Version : Is there a way to separate lines of code in C?


singularity2006
01-24-2004, 12:13 AM
If you have really long lines of code in C, is there a way of breaking them up into two or more lines? I know there is a way in VB of doing it using the underscore character, but is there something similar in C?

playmesumch00ns
01-24-2004, 12:19 AM
yup, the backslah character, which is used to break lines in just about any other given language except lame VB...

char[1024] mystring = "hello there, i'm a really, really long line \
in fact i'm so long I need to span multiple lines";

I've used the string example above because string literals and preprocessor statements (iirc) are the only thing you will ever need to split in C. Because C uses the semicolon as a statement terminator, rather than a stoopid newline character, you can split statements over as many lines as you like and the compiler will eat them just fine.

int a =

b = c = d




= e







= f;



is perfectly valid (if rather ugly)

singularity2006
01-24-2004, 05:44 AM
aaahhhh, much thanks. I'll give that a go next time I code something for lab. :beer:

markyjerky
01-24-2004, 01:38 PM
char msg[] = "some text from one line"
"some text from another line"
"some text from a thrid line"
;


I like the above pattern where strings are concerned. It's a little cleaner even if it requires an extra character ... it's more clear what's happening with white space.

It's great for long printf patterns.


For things other than long strings ... the backslash is naturally a great tool.

WATCH OUT for white space after the backslash ... it will mess up many old and new preprocessors.

singularity2006
02-13-2004, 05:56 AM
hmm... getting compile errors. Does that slash work for:

printf("long string of text \
continuing text etc.")

Is that valid?

rendermaniac
02-13-2004, 09:50 AM
You still need a semicolon on the end of the line.... Mark has a good point about whitespace after the slash too.

Simon

Jhavna
02-13-2004, 10:20 AM
The slash thing may work or may not. It depends on the compiler.

You can just do

if (variable > value && variable3 < value2
|| boolean == true && variable < value)

on some compilers, on others you have to use the backslash with no trailing spaces.

However, you will always have to use the backslash in multiline macros...

markyjerky
02-13-2004, 02:51 PM
This below might be valid with a semi-colon.

printf("long string of text \
continuing text etc.")


But it's certainly not as REASONABLE as

printf("long string of text "
"continuing text etc.");

The two string above are not separate. That are for all intents and purposes the same string where the compiler is concerned.

singularity2006
02-14-2004, 07:39 AM
ooooicy, much thanks. I'll give that a go next time around. My primary point of doing that was so I could print my code cleanly on 8.5 x 11 paper. But yeah, thanks!

bla
02-14-2004, 11:51 PM
this

printf("long string of text \
continuing text etc.")

is not totally valid(even with a semicolon) on any compiler, its kind of a dependant on the compilers implementor i think, you'd have to check the C spec, inside a string("in here") the backslash marks the beginning of an escape character('\n', '\0'), so a lone backslash in a string literal? should not necessarily compile.

this however should!

printf("long string of text "
"continuing text etc.");

the \ is for macros and the like, the C compiler in nearly all cases excepting inside strings and characters ignores all whitespace characters...
so just break the lines where you would normally have a space and where it looks pleasant, the compiler will tell you if you've done it wrong...

then again they may have changed it since i learned C so...

CGTalk Moderation
01-17-2006, 07:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.