Operating System - HP-UX
1753379 Members
4870 Online
108792 Solutions
New Discussion юеВ

Printing characters in C by referring to the ASCII code

 
SOLVED
Go to solution
Joseph A Benaiah_1
Regular Advisor

Printing characters in C by referring to the ASCII code

Is it possible to print out charaters in a C program by refering to the the ASCII code in much the same way as the tr program does in UNIX.

Cheers,

Joseph.
4 REPLIES 4
Graham Cameron_1
Honored Contributor
Solution

Re: Printing characters in C by referring to the ASCII code


int main () {
int i ;

for (i=1;i<=127;i++) {
printf ("%03d: %c\n", i, i) ;
}
}

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
A. Clay Stephenson
Acclaimed Contributor

Re: Printing characters in C by referring to the ASCII code

What do you think tr itself is written in?
If it ain't broke, I can fix that.
ian Dennison
Regular Advisor

Re: Printing characters in C by referring to the ASCII code

yes by using escape sequences within printf

eg printf("\007"); /* prints bell char */
eg printf("\066"); /* prints A or B (I forget which) */

The 3 digit code after the \ is the ascii code to be printed.

Share and Enjoy! Ian
Lets do it to them before they do it to us! www.fred.net.nz
Joseph A Benaiah_1
Regular Advisor

Re: Printing characters in C by referring to the ASCII code

Graham/Ian,

Thanks for your response. I actually worked it out 10 minutes after posting this question.

Anyway your answers are worth 10 points.

Cheers,

Joseph