0

I am trying to split a long format string to fprintf() into multiple lines using the \ character as shown below:

fprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n\
                        And returns the day of the week for that date using Zeller's rule.\n\n\
                        Enter date in (dd/mm/yyyy) format: ");

this causes whitespaces to be added to the output as shown below:

This program take a date supplied by the user in dd/mm/yyyy format...
                        And returns the day of the week for that date using Zeller's rule.

                        Enter date in (dd/mm/yyyy) format:

this answer suggests this should work. I also checked this answer before posting here. A comment on it mentions that this approach...

...suffers from the fact that it breaks if there is any whitespace after the '\'; a bug that can be baffling when it occurs.

cat -A's output on the program file...

^Ifprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n\$
^I^I^IAnd returns the day of the week for that date using Zeller's rule.\n\n\$
^I^I^IEnter date in (dd/mm/yyyy) format: ");$

...shows no whitespace after \; although it does introduce <TAB>s into the lines that follow. I am using vim to edit my source files.

I use line continuation with \ all the time in Bash, and was under the impression that it works similarly with fprintf() format strings in C.

I'd like to keep my code readable and line widths reasonable. Other than splitting the long string into multiple fprintf().

  1. Is this standard behavior for printf()/fprintf()
  2. Is it normal for a vim to my code when I do line continuation with \?
  3. How do I fix this issue?

2 Answers2

2

There are fairly low limits in actual portability, but practically you can almost certainly do:

fprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n"
        "And returns the day of the week for that date using Zeller's rule.\n\n"
        "Enter date in (dd/mm/yyyy) format: "
);

If you do start hitting limits on concatenating strings like that, you can also do:

fprintf(stdout, "%s\n%s\n\n%s",
        "This program take a date supplied by the user in dd/mm/yyyy format...",
        "And returns the day of the week for that date using Zeller's rule.",
        "Enter date in (dd/mm/yyyy) format: "
);
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • could you please explain what you meant by "fairly low limits in actual portability". Thanks for your suggestions. –  Oct 02 '20 at 19:09
  • @anitesh — I'd guess that the concern is that pre-standard C compilers did not support string literal concatenation. It is unlikely to be a practical problem except perhaps in obscure embedded systems. – Jonathan Leffler Oct 02 '20 at 19:15
  • I remember with the last decade using a compiler that had a limit on the length of a string literal that was less than 1K. I don't suppose any reasonable modern implementation will have a limit so low that the above strings won't work. – William Pursell Oct 02 '20 at 19:30
2

You do not need \ as it is not a macro definition.

Simply have as many as you want as string literals separated by as many as you want whitespace (new line is also a whitespace). The C compiler ignores the whitespace.

int main(void)
{
    fprintf(stdout, "This program take a date supplied by the user in dd/mm/yyyy format...\n"
                    "And returns the day of the week for that date using Zeller's rule.\n\n"
                    "Enter date in (dd/mm/yyyy) format: ");
}
int main(void)
{
    fprintf(stdout, "This program take a date"
                    " supplied by the user in dd/"
                    "mm/yyyy format...\n"
                    "And returns "
                    "the "
                    "day of "
                    "the "
                    
                    
                    
                    
                    "week for that "
                    "date using Zeller's rule.\n\n"
                    "Enter date in (dd/mm/yyyy) format: ");
}

https://godbolt.org/z/6ovj3G

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0___________
  • 60,014
  • 4
  • 34
  • 74