Search code examples
cgccvimprintfline-continuation

C: trying to split fprintf() format string into multiple lines with line-continuation using \ adds tabs at the beginning of lines that follow


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?

Solution

  • 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