![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3
![]() |
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... |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
[code]printf("\\n");[code]
Basically, escape the backslash, so the escape sequence isn't seen. |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3
![]() |
Quote:
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... |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3
![]() |
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... |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3
![]() |
Quote:
![]()
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me... |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|