Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C can a long printf statement be broken up into multiple lines?

Tags:

c

printf

I have the following statement:

printf("name: %s\targs: %s\tvalue %d\tarraysize %d\n", sp->name, sp->args, sp->value, sp->arraysize); 

I want to break it up. I tried the following but it doesn't work.

printf("name: %s\t args: %s\t value %d\t arraysize %d\n",  sp->name,  sp->args,  sp->value,  sp->arraysize); 

How can I break it up?

like image 991
neuromancer Avatar asked Nov 17 '09 21:11

neuromancer


People also ask

How do you break a long string into multiple lines?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

What is the problem with printf in C?

The printf functions are implemented using a variable-length argument list. Arguments specified after the format string are passed using their inherent data type. This can cause problems when the format specification expects a data object of a different type than was passed.

How do you separate a multi line in C language?

"\ operator is used to separate a multiline macro in C language" Explanation: In C language, macros are used to substitute a string, which in turn defined as a macro substitution. In C, macros can be used to replace a first part using a second part.

What happens when printf is executed?

Most of the compilers takes each parameter of printf() from right to left. So in the first printf() statement, the last parameter is x++, so this will be executed first, it will print 20, after that increase the value from 20 to 21. Now print the second last argument, and show 21.


1 Answers

If you want to break a string literal onto multiple lines, you can concatenate multiple strings together, one on each line, like so:

printf("name: %s\t" "args: %s\t" "value %d\t" "arraysize %d\n",  sp->name,  sp->args,  sp->value,  sp->arraysize); 
like image 65
James McNellis Avatar answered Nov 16 '22 01:11

James McNellis