?
Solved

Printf and scanf ascii chars

Posted on 2003-03-07
7
Medium Priority
?
2,193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 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