Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

printf

zsh

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?

like image 275
rounak Avatar asked Dec 01 '14 22:12

rounak


People also ask

What does the percent sign mean in terminal?

I learned in this stack exchange question that the percent sign actually comes from zsh and means that the file didn't end with a newline, but zsh is showing one anyways to make your terminal output more clear & consistent. User geekq adds: I've now put PROMPT_EOL_MARK='' into my .

How do I print a percent sign in printf?

To obtain the character "%" in a format string, enter "%%", e.g. printf("The percentage is %d %%. *n",5); yields The percentage is 5 %.


1 Answers

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.

like image 59
Kevin Avatar answered Oct 06 '22 00:10

Kevin