Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 2nd, 2005, 8:34 PM   #1
bivhitscar
Hobbyist Programmer
 
bivhitscar's Avatar
 
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3 bivhitscar is on a distinguished road
Printing escape sequences using printf()?

Is it possible to print the escape sequence representation of a nonprinting character using printf()? ie. print "\n" instead of actually printing the newline itself.

I am trying to get a loop to read a string and print each character with it's equivalent ascii code. But this all turns to shit mainly with the newline character. Aside from making a kinda long if statement to filter the possibilites, any suggestions?
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me...
bivhitscar is offline   Reply With Quote
Old Nov 2nd, 2005, 8:37 PM   #2
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
[code]printf("\\n");[code]
Basically, escape the backslash, so the escape sequence isn't seen.
nindoja is offline   Reply With Quote
Old Nov 2nd, 2005, 8:48 PM   #3
bivhitscar
Hobbyist Programmer
 
bivhitscar's Avatar
 
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3 bivhitscar is on a distinguished road
Quote:
Originally Posted by nindoja
printf("\\n");
Basically, escape the backslash, so the escape sequence isn't seen.
That only works when I know what character is coming.

This is the code I'm using:

for (i = 1; i <= length; i++)
  {
    printf("%c = %3d, ", string[i - 1], string[i - 1]);

    if (i % 8 == 0)
      printf("\n");
  }

That basicaly says, read the current element of the array, print the character and print it's ascii code. The if statement is just for formatting purposes.

Ideally, I just want a simple if else that says if ch == isspace() print the escape sequence instead of the actual character, else print the character. Am I making any sense?
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me...
bivhitscar is offline   Reply With Quote
Old Nov 2nd, 2005, 8:56 PM   #4
bivhitscar
Hobbyist Programmer
 
bivhitscar's Avatar
 
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3 bivhitscar is on a distinguished road
Actually, after reviewing what characters would actually pop up in real input, it's only two - a fair bit easier than I first thought. Although, I'd still like to hear if it's possible.
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me...
bivhitscar is offline   Reply With Quote
Old Nov 3rd, 2005, 3:08 AM   #5
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
You can do something like
if(ch=='\n')
  printf("<new-line> - %3d",ch);
else if(ch==' ')
  printf("<space> - %3d",ch);
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Nov 3rd, 2005, 10:08 PM   #6
bivhitscar
Hobbyist Programmer
 
bivhitscar's Avatar
 
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3 bivhitscar is on a distinguished road
Quote:
Originally Posted by InfoGeek
You can do something like
if(ch=='\n')
  printf("<new-line> - %3d",ch);
else if(ch==' ')
  printf("<space> - %3d",ch);
Yeh, I just ended up using a switch. Thanks though.
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me...
bivhitscar is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC