LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-03-2010, 05:10 AM   #1
madsovenielsen
Member
 
Registered: Aug 2009
Posts: 179

Rep: Reputation: 15
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
 
Old 03-03-2010, 05:43 AM   #2
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
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

Last edited by ashok.g; 03-03-2010 at 05:52 AM.
 
Old 03-03-2010, 05:54 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by madsovenielsen View Post
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.

Last edited by crts; 03-03-2010 at 05:59 AM.
 
Old 03-03-2010, 05:58 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by ashok.g View Post
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.
 
Old 03-03-2010, 06:11 AM   #5
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Quote:
Originally Posted by crts View Post
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.
 
Old 03-03-2010, 06:36 AM   #6
karthi_27
LQ Newbie
 
Registered: Mar 2010
Posts: 6

Rep: Reputation: 0
Thumbs up

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

Last edited by karthi_27; 03-03-2010 at 06:39 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Paths and escape characters code-breaker Linux - Newbie 4 07-11-2006 11:24 PM
passing escape characters along with lp command zita Linux - General 0 10-05-2005 01:58 PM
Variables and escape characters? flobadon Programming 4 09-10-2005 04:34 PM
Escape Characters in linux shunraj Linux - Software 1 05-18-2004 03:21 PM
escape characters not escaping BobNz Linux - Software 2 04-09-2004 03:34 AM


All times are GMT -5. The time now is 05:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration