Home > Software design >  When output to the console, a "%" symbol is added in linux. C
When output to the console, a "%" symbol is added in linux. C

Time:11-17

I use Manjaro Linux, DISTRIB_RELEASE=22.0.0, GNOME 43.1, Kernel 5.19.17-2, and I used zsh.

I decided to learn C , but I ran into a problem. If I didn't add std::endl when outputting to the console, the symbol "%" is added.

See the screenshots attached.

Code1:

#include <iostream>

int main()
{
    int age;
    age = 28;
    std::cout << "Age = " << age;
    return 0;
}

Result1

Code2:

#include <iostream>

int main()
{
    int age;
    age = 28;
    std::cout << "Age = " << age << std::endl;
    return 0;
}

Result2

Why is this happening? All I tried was just adding std::endl. I want to know why the "%" symbol is being added.

CodePudding user response:

Ah, you're omitting the final line break.

Your shell hence should (would it be very true to what your program actually produced in output) display the prompt on the same line as your output.

Now, that would look terrible and be confusing. So, instead your shell inserts a special character with a special background color to mark "hey, this isn't the program's output, but I'm still inserting a line break here, because I don't hate you, dear user".

That percentage symbol is not from your program. It's your shell trying to be sensible.

CodePudding user response:

From the man page for zsh:

When a partial line is preserved, by default you will see an inverse bold character at the end of the partial line: a % for a normal user or a # for root. If set, the shell parameter PROMPT_EOL_MARK can be used to customize how the end of partial lines are shown.

  • Related