Link to home
Start Free TrialLog in
Avatar of somemadfanaticalperson
somemadfanaticalperson

asked on

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
Avatar of rajeev_devin
rajeev_devin

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.
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);
}
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
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);


}
Avatar of somemadfanaticalperson

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks you've all been helpful except that I think mayankeagle gets the points as he explained it for me more.