What is it called when you can fill a string with <<< and an end-delimiter?

后端 未结 4 1667
忘了有多久
忘了有多久 2021-01-19 19:51

I know in C++ and in PHP you can fill a string or a file with hard-coded text. If I remember correctly this is how it is supposed to look:

var <<< D         


        
4条回答
  •  迷失自我
    2021-01-19 20:11

    C++ doesn't have any equivalent to PHP's HEREDOC syntax.

    You can, however, do this in C++:

    cout << "   Menu for program X\n"
            "   1.Add two numbers\n"
            "   2.Substract two numbers\n"
            "   3.Multiply two numbers\n"
            "   Please pick an option from (0-3);" << endl;
    

    Or this in C:

    printf( "   Menu for program X\n"
            "   1.Add two numbers\n"
            "   2.Substract two numbers\n"
            "   3.Multiply two numbers\n"
            "   Please pick an option from (0-3);\n" );
    fflush(stdout);
    

    Which is directly equivalent to PHP's HEREDOC syntax:

    echo <<

    The above syntax for C and C++ is treated by the compiler as one long string, by stitching them together. It has no other effect on the string literal, hence the need for '\n'.

提交回复
热议问题