Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Printf and scanf ascii chars

Posted on 2003-03-07
7
Medium Priority
?
2,208 Views
Last Modified: 2008-02-01
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
0
Comment
7 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8092939
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
 

Expert Comment

by:sam_exp
ID: 8093270
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
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8093373
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:cryptosid
ID: 8093410
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
 

Author Comment

by:somemadfanaticalperson
ID: 8096622
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 504 total points
ID: 8117483
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
 

Author Comment

by:somemadfanaticalperson
ID: 8124285
thanks you've all been helpful except that I think mayankeagle gets the points as he explained it for me more.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question