4

With reference to the answer given here, what output one must expect from the following code:

#include<stdio.h>

int main()
{
    int a=65;
    printf("%d\n",printf("%d\
    \n",a));
    return 0;
}

It gives the output:

65
4

But to me it would seem that it should give this:

65
3

Why is the output 65 4?

ruohola
  • 21,987
  • 6
  • 62
  • 97
beta_me me_beta
  • 846
  • 1
  • 5
  • 14
  • 10
    Are you running on windows? – dbush Jan 30 '20 at 15:44
  • 2
    windows probably issues a \r character automatically – Jean-François Fabre Jan 30 '20 at 15:45
  • 1
    Here it prints `7`: https://ideone.com/hZNaFM Remove this `\\` - you don't need it for line continuation, just enclose both parts of the string in double quotes. Otherwise it is taking your indentation as part of the string. – Eugene Sh. Jan 30 '20 at 15:45
  • 1
    I would avoid `\\` at the end of line. It's useless and error-prone – Jean-François Fabre Jan 30 '20 at 15:46
  • 1
    Is that your literal code, including the superfluous backslash? Have you tried counting all the output characters including non-printing ones (eg, pipe through `cat -v`)? – Useless Jan 30 '20 at 15:48
  • 1
    @EugeneSh. removing '\' gives correct output but why not with it. – beta_me me_beta Jan 30 '20 at 15:48
  • Because you're including some escaped whitespace as a literal part of the format string. Hence checking for non-printing characters in your output. – Useless Jan 30 '20 at 15:49
  • @Jean-FrançoisFabre: Actually, it is changing the output. – machine_1 Jan 30 '20 at 15:50
  • okay, bar "useless", and "error-prone" remains because now changing the indentation changes the behaviour. – Jean-François Fabre Jan 30 '20 at 15:50
  • @Useless I didn't got what you said, does '\' counts into non printing characters – beta_me me_beta Jan 30 '20 at 15:50
  • 3
    Hint for those reading the code: In the original source code there is a single TAB, not multiple spaces before `\n",a));` in the code. It is not rendered correctly on the page (at least for me). – walnut Jan 30 '20 at 15:51
  • @walnut: Now I see why it is printing `7` on my system. – machine_1 Jan 30 '20 at 15:55
  • No, backslash is an escape character. It changes the operation of the following character, in this case the newline (actually they are both simply deleted in [phase 2](https://en.cppreference.com/w/c/language/translation_phases)). Then you have some whitespace (spaces or tabs) in your string before the following '\n'. – Useless Jan 30 '20 at 15:55
  • 2
    @dbush: This definitely has nothing to do with Windows line-endings. They would affect the file position/length but not the return value of `printf`. – R.. GitHub STOP HELPING ICE Jan 30 '20 at 15:56
  • 1
    You can see this (the whitespace) in action by just piping your program output through `cat -te` (not actually `cat -v` as I said before). – Useless Jan 30 '20 at 15:56
  • @CodyGray This question doesn't really share anything but the title (which I admit, is quite bad) with the dupe, it's simply a totally different question. – ruohola Jan 31 '20 at 07:32
  • In what way is it different, @ruohola? I read the answers quite carefully; they are almost exactly the same, with the only difference being that there is a new-line character in the string literal here, whereas there is no new-line in the other question. However, reading those answers adequately explains the behavior described here, and gives one everything she needs to know to understand the code and its output. – Cody Gray - on strike Jan 31 '20 at 07:35
  • @CodyGray OP obviously had read the dupe target question, and knew how printf's return value worked, and despite the title, the question was not about that but the line continuation, which was in no way addressed in the dupe. I worded the title to be more inline with the questions intent. – ruohola Jan 31 '20 at 09:39
  • 1
    I do not agree it is dupe as well – Eugene Sh. Jan 31 '20 at 14:03

3 Answers3

9

You are printing 4 characters <tab>, \n, 6, and 5, so the result you're getting makes total sense.

Notice that the \ at the end of this printf("%d\n",printf("%d\ line, will include all of the indentation of the next line into the formatting string. This indentation was originally a <tab> character when you ran your file.

The reason why some people are reporting the ouput of 65 7 is that StackOverflow changes all tabs in the pasted code into 4 spaces, so the code they copied from your question was not the same code you ran on your machine.

See this demo, which shows the existance of the <tab> in the output (online version):

#include<stdio.h>

int main()
{
    int a=65;
    printf("%d\n",printf("%d\
    <--tab here\n",a));
    return 0;
}

Output:

65 <--tab here
15

If you remove the weird, totally unnecessary, and obviously error prone line continuation, it will print the expected output just fine:

#include<stdio.h>

int main()
{
    int a=65;
    printf("%d\n",printf("%d\n",a));
    return 0;
}

Output:

65
3
ruohola
  • 21,987
  • 6
  • 62
  • 97
6

Because of \ it is taking the indentation characters (tabs, spaces) of the next line as a part of the string. To concatenate strings on different lines simply enclose each part in double quotes:

#include<stdio.h>
int main()
{
    int a=65;
    printf("%d\n",printf("%d"
    "\n",a));
    return 0;
}

As a general note - the \ for line continuation is useful in multi-line macro definitions only or some exotic cases I am not aware of.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
-1

It is quite simple to check

void printbuff(const char *str, size_t len)
{
    while(len--)
    {
        printf("%03d = '%c'\n", *str, *str >= ' ' && *str < 127 ? *str : ' ');
        *str++;
    }
}

int foo()
{
    char buff[32];
    int a=65;
    size_t len;
    printf("%d\n", len = sprintf(buff, "%d\
    \n",a));
    printbuff(buff, len);
    return 0;
}

int bar()
{
    char buff[32];
    int a=65;
    size_t len;
    printf("%d\n",len = sprintf(buff, "%d\n",a));
    printbuff(buff, len);
    return 0;
}

int main()
{
    foo();
    bar();
}

check yourself and you will know

It is just the char (or chars) between beginning of the line and the '\n', as it is still the same string. What is there depends on the editor.

Godbolt adds 4 spaces for example

https://godbolt.org/z/boPYNs

0___________
  • 60,014
  • 4
  • 34
  • 74