• C

Printf and scanf ascii chars

I read somewhere how to print and do other stuff with ascii chars somewere excpet now I can't fint it again.
I need to be able to print any of the 256 Chars and also read them from a scanf or file.  
am using Pacific C 7 with MSDOS6.22
somemadfanaticalpersonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajeev_devinCommented:
To do operations with ascii characters, I prefer to do with getch() and putch(). You should also try, its' very handy.

To do manipulation using using scnaf() and printf() use simply use statements of type

     scanf("%c",&c);
     printf("%d",c);

But remember that through keyboard u cannot enter some of the characters. For this use have to use Alt+(number through number pad). Ex. Alt+255 to enter ascii character 255. Hope this may help you.
0
sam_expCommented:
somemadfanaticalperson
   
u can read the ascii chrecter with the help of the printf and the scanf function.
  the program of this is
#include <stdio.h>

 void main()
{
 int a;
printf(" Enter the number of the chrecter::\t");
scanf("%d",&a);
printf("The equivalent chrarecter of %d is %c",a,a);
}
0
sarda_rameshCommented:
hi
to print any particular character u can also do it like that .. suppose u have to print 'A'

printf ("\101"); //101 is the ASCII in Octal for 'A'

by putting the ascii value in octal any character can be printed. this way u can print characters that r otherwise nonprintable

regards
ramesh
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

cryptosidCommented:
use for loop if u want...

for(i=0;i<256;i++)
   printf("%c",i);


for reading from a file...

void main()
{

/*declare the file pointer*/
 FILE *fp;
/*declare character variable */
 char c;

/*open the file */
 fp = fopen("file.dat","rb");
 if(fp==NULL) return 1;

 while(!feof(fp))
 {
   fscanf(fp,"%c",&c);
   printf("%c",c);
 }


/*close the file*/
 fclose(fp);


}
0
somemadfanaticalpersonAuthor Commented:
So I use %c when printing the number rather then say %i
does it have to be in hex or decimal or can I swop between the two?
0
Mayank SAssociate Director - Product EngineeringCommented:
You have to give a %c to print the character and %i or %d to print the corresponding ASCII code (number).

char a = 'A' ;

printf ( "%c", c ) ; // prints A
printf ( "%d", c ) ; // prints 65, the ASCII code

Another thing - cryptosid's loop for printing all the 256 ASCII characters is correct, but in DOS, you will face a problem: the printing of the 26th character suppresses the rest of the output, so you'd better give:

for ( i = 0 ; i < 256 ; i ++ )
  if ( i != 26 )
    printf ( "%c", i ) ; // end if, for

>> does it have to be in hex or decimal or can I swop between the two?

Your number will always be stored in binary form in the memory. It depends upon you whether you want to interpret it as in decimal form or in hexadecimal form. The character 'A' will be stored as 01000001 in memory. Now, you can interpret it as 01000001 binary, or 65 decimal or 41 hex. That depends upon you. To print the decimal value, you use "%d", and to print the hexadecimal equivalent, you use "%x".

Hope that helps!

Mayank.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
somemadfanaticalpersonAuthor Commented:
thanks you've all been helpful except that I think mayankeagle gets the points as he explained it for me more.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.