Print Text After Scanf on Same Line in C Programming

In summary, to print text after a scanf statement on the same line, you will need to use ansi screen codes to reposition the cursor after writing the prompt line. These codes will only work if your terminal window supports them and they have been enabled.
  • #1
jaydnul
558
15
How do you print text after a scanf statement on the same line? This is what i need the output to look like:

Enter a number here: _%

The _ is the scanf waiting for input. Now when i write the code like this:

printf("Enter a number here: ");
scanf("%d", &x);
printf("%\n");

It won't put the % sign until after i input a value. How should i rewrite this to achieve what I need? thanks in advance!
 
Physics news on Phys.org
  • #2
lundyjb said:
How do you print text after a scanf statement on the same line? This is what i need the output to look like:

Enter a number here: _%

The _ is the scanf waiting for input. Now when i write the code like this:

printf("Enter a number here: ");
scanf("%d", &x);
printf("%\n");

It won't put the % sign until after i input a value. How should i rewrite this to achieve what I need? thanks in advance!

You can only do that if you use ansi screen codes to reposition the cursor after you've written the prompt line:

http://courseweb.stthomas.edu/tpsturm/private/notes/qm300/ANSI.html

These work only if your terminal window supports them and if its been enabled. I've used them in my awk scripts a lot.
 
  • #3
Alright. Thanks man
 

Related to Print Text After Scanf on Same Line in C Programming

1. How do I print text after using scanf on the same line in C Programming?

In order to print text after using the scanf function on the same line in C Programming, you can use the printf function with a newline character (\n) at the end of the desired text. This will print the text on the next line after the user input from scanf.

2. Can I use multiple scanf and printf functions on the same line in C Programming?

Yes, you can use multiple scanf and printf functions on the same line in C Programming. However, you will need to separate each function with a comma (,) and make sure to include the appropriate format specifiers for each function's input and output.

3. Why does my text not print after using scanf on the same line in C Programming?

If your text is not printing after using scanf on the same line in C Programming, it is likely because you did not include a newline character (\n) at the end of the desired text in the printf function. This is necessary to print on a new line after the user input from scanf.

4. Is it necessary to use the newline character (\n) when printing text after using scanf on the same line in C Programming?

No, it is not necessary to use the newline character (\n) when printing text after using scanf on the same line in C Programming. However, if you want the text to be printed on a new line after the user input from scanf, then you will need to use the newline character.

5. Can I use other formatting characters when printing text after using scanf on the same line in C Programming?

Yes, you can use other formatting characters when printing text after using scanf on the same line in C Programming. Some other commonly used formatting characters in C Programming include tab (\t), carriage return (\r), and backspace (\b). These can be used to add spacing or to manipulate the cursor position when printing text.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
912
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
686
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Back
Top