Getting a weird percent sign in printf output in terminal with C

CPrintfZsh

C Problem Overview


I have this printf statement at the end of my program:

printf("%d", total_candies);

total_candies is an int, and while I expect everything to work correctly, along with the actual number, I'm getting a weird percent sign at the end. enter image description here

Can anyone tell me why this is happening?

C Solutions


Solution 1 - C

When (non-null) output from a program doesn't include a trailing newline, zsh adds that color-inverted % to indicate that and moves to the next line before printing the prompt; it's generally more convenient than bash's behavior, just starting the command prompt where the output ended.

Solution 2 - C

In zsh

PROMPT_SP Attempt to preserve a partial line (i.e. a line that did not end with a newline) that would otherwise be covered up by the command prompt due to the PROMPT_CR option. This works by outputting some cursor-control characters, including a series of spaces, that should make the terminal wrap to the next line when a partial line is present (note that this is only successful if your terminal has automatic margins, which is typical).

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrounakView Question on Stackoverflow
Solution 1 - CKevinView Answer on Stackoverflow
Solution 2 - Cshrey deshwalView Answer on Stackoverflow