Home > Back-end >  How to use print statements in C more efficiently? Why do I have a percentage sign at the end of my
How to use print statements in C more efficiently? Why do I have a percentage sign at the end of my

Time:11-09

this is not a problem I was just wondering if I could use fewer print statements for this problem.

#include <stdio.h> // for print statments
int main(int argc, char const *argv[]) {

  int thisNumber;
  printf("%s", "Hey you! input your Number here: " );

  scanf("%d", &thisNumber );

  printf( "%s","Your number is: " );
  printf("%d\n", thisNumber );
  return 0;
}

I have tried this:

#include <stdio.h> // for print statments
int main(int argc, char const *argv[]) {
  int thisNumber;
  printf("%s", "Hey you! input your Number here: " );

  scanf("%d", &thisNumber );

  printf( "Your number is: %d", thisNumber );

  return 0;
}

And the output was:

> Hey Bekhruz! input your Number here: <my input say:125>
> Your number is: 125%

and for some reason, I have a % sign at the end with this code. Why is it occurring and how can i solve it? Thanks!

CodePudding user response:

That's your shell prompt. The problem is not the appearance of %; the problem is that it's on the same line as your program's output. This is solved by outputting a line feed.

printf( "Your number is: %d\n", thisNumber );

When you combined the two statements, you left out the \n.

  • Related