Is there a way to separate lines of code in C?


#1

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?


#2

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)


#3

aaahhhh, much thanks. I’ll give that a go next time I code something for lab. :beer:


#4

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.


#5

hmm… getting compile errors. Does that slash work for:

printf(“long string of text
continuing text etc.”)

Is that valid?


#6

You still need a semicolon on the end of the line… Mark has a good point about whitespace after the slash too.

Simon


#7

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…


#8

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.


#9

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!


#10

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(’
', ‘\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…


#11

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.