[SOLVED] Having trouble with escape characters in C
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <stdio.h>
main()
{
char number[3] = "32";
printf("The number is %s\r", number);
}
I have tried to put the carriage return escape character all sorts of places but it doesn't work. i am compiling with GCC on Linux 64bit and the -ansi option.
Hi madsovenielsen,
Whatever may be the language you are using, /r always moves your cursor to the starting point of the current line.
Whatever you print before a carriage return all those print statements will be deleted. This is what actually happening with your code.
Try to give it as:
#include <stdio.h>
main()
{
char number[3] = "32";
printf("The number is %s\r", number);
}
I have tried to put the carriage return escape character all sorts of places but it doesn't work. i am compiling with GCC on Linux 64bit and the -ansi option.
Where is the flaw ?
/mads
Hi,
I just copied + pasted your text and compiled it. It does work, you just do not understand what carriage return actually does. Do you actually want to add a newline character? This is done by '\n'.
As for your issue, the '\r' character returns the cursor to the beginning of the line WITHOUT adding a new line. All further output then starts from the begining of the line. You do not see your text because after your program exits your text is overwritten by your bash prompt. To illustrate this try a longer text in printf(), e.g.
Code:
printf("this is some preceding junk text: The number is %s : this is some trailing junk text\r", number);
Now your printed text should be not completely overwritten by vour bash prompt.
Hi madsovenielsen,
Whatever may be the language you are using, /r always moves your cursor to the starting point of the current line.
Whatever you print before a carriage return all those print statements will be deleted. This is what actually happening with your code.
Try to give it as:
Code:
printf("\rThe number is %s", number);
Now, this will print
Code:
The number is 32
HTH
Hi,
if you do it this way, then you can omit the '\r' at all. Your are at the begining of the line when your program enters. Doing a '\r' does not accomplish anything at this point.
You do not see your text because after your program exits your text is overwritten by your bash prompt. To illustrate this try a longer text in printf(), e.g.
Code:
printf("this is some preceding junk text: The number is %s : this is some trailing junk text\r", number);
Now your printed text should be not completely overwritten by vour bash prompt.
Hi crts,
Can you explain in detail. I'm not getting your explanation.
'\r' is used to get the cursor to the first character.If you write any character after the '\r' then those characters will overwrite the characters from the first character in the line.
Consider the following example,
Here I want to print the number count and I have to print the numbers in one line.In this case I can use '\r'.Because after printing the count just go to the first position and then print the next count.
Code:
#include <stdio.h>
main()
{
int i;
for(i=0; i<=1000;i++){
fprintf(stderr,"The number is %d\r" ,i);
sleep(1);
}
}
Since ,because of the buffer problem I used fprintf ,because to solve the buffer problem I need "\n" in printf.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.