LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Having trouble with escape characters in C (http://www.linuxquestions.org/questions/programming-9/having-trouble-with-escape-characters-in-c-792835/)

madsovenielsen 03-03-2010 06:10 AM

Having trouble with escape characters in C
 
Hey all.

i am learning C

this is my code:

Code:

#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

ashok.g 03-03-2010 06:43 AM

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 :)

crts 03-03-2010 06:54 AM

Quote:

Originally Posted by madsovenielsen (Post 3883738)
Hey all.

i am learning C

this is my code:

Code:

#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.

crts 03-03-2010 06:58 AM

Quote:

Originally Posted by ashok.g (Post 3883760)
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.

ashok.g 03-03-2010 07:11 AM

Quote:

Originally Posted by crts (Post 3883769)
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.

karthi_27 03-03-2010 07:36 AM

'\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.


All times are GMT -5. The time now is 03:49 AM.