Philluminati
Philluminati

Reputation: 29

Why the charater "%" is printed after performing a printf without "\n"?

Here is my simple c code. Can anyone tell me why it gives me this output? I am coding on an arch linux 4.15.1-2-ARCH machine with the gcc compiler (version: 7.3.0) I compile with: gcc --std=c99 -Wall --pedantic client.c -o client

I have the following code:

#include <stdio.h>

int main(void) {
  printf("Test.");
  return 0;
}

But it get the following Output: Test.%

I don't know where the % is comming from. Would be great if someone can give me a hint.

Upvotes: 1

Views: 95

Answers (2)

dbush
dbush

Reputation: 223972

Your printf string doesn't contain a newline character. As a result, whatever string your shell normally prints as a prompt will appear immediately after what your program prints.

Running on my machine:

$ ./x1
Test.$ 

My prompt is "$", so that's what appears after the string

Adding \n, which is the escape sequence for a newline, to your string will make your prompt appear on the following line:

printf("Test.\n");

Output:

$ ./x1
Test.
$ 

Upvotes: 2

lvella
lvella

Reputation: 13453

Try:

printf("Test.\n");

The word Test. printed by your program probably joined with the first character of your terminal prompt. Adding a \n will print a new line character, so your terminal prompt will be written on the next line when your program exits.

Upvotes: 0

Related Questions