943,594 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 22467
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 27th, 2008
0

How to find ASCII value of character in c

Expand Post »
Plz..guys i want to know is there any function that can give ASCII value of character? and do i need to use any header file to use that function
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sargarpramod is offline Offline
4 posts
since Jun 2008
Jun 27th, 2008
0

Re: How to find ASCII value of character in c

Here is sample code, which demonstrates on how to get a the ASCII code for the entered char.

  1. #include <stdio.h>
  2.  
  3. #define TRUE 1
  4.  
  5. void clear_buffer( void )
  6. {
  7. int ch;
  8.  
  9. while( ( ch = getchar() ) != '\n' && ch != EOF );
  10. }
  11.  
  12. int main()
  13. {
  14. unsigned int ch;
  15.  
  16. while( TRUE )
  17. {
  18. printf("\nsingle char please - " );
  19. ch = getchar();
  20. clear_buffer();
  21.  
  22. printf("%c - %d", ch, ch );
  23. }
  24. }
  25.  
  26. /* my output
  27. single char please - a
  28. a - 97
  29. single char please - s
  30. s - 115
  31. single char please - d
  32. d - 100
  33. single char please -
  34. */

NOTE: This is limitedto quite a lot of stuff. You coud place some error checking to make it more perfect.

And you can always find a ascii char chart on the net. Here is link for one which is found ASCII.

ssharish
Last edited by ssharish2005; Jun 27th, 2008 at 1:06 pm.
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Jun 27th, 2008
0

Re: How to find ASCII value of character in c

In C, you can use the char data type just like a number and it will automatically convert it to the ASCII value. Here's a minimal example:

  1. int myNum = 'a';

myNum would have the value 97 in it.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jun 28th, 2008
0

Re: How to find ASCII value of character in c

Thanks guys for replying to my query
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sargarpramod is offline Offline
4 posts
since Jun 2008
Jun 30th, 2008
0

Re: How to find ASCII value of character in c

remember this fact:

a variable of type char is just an 8-bit int.

thats all it is, and all it ever was.

try this and see:

int a = 65; printf("%c", a);
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 9th, 2008
0

Re: How to find ASCII value of character in c

  1. #include<stdio.h>
  2.  
  3. void main (void)
  4. {
  5. char x;
  6. printf("\nEnter a Character : ");
  7. x = getchar();
  8. printf("\nThe ASCII for Char is: %d",x);
  9.  
  10. }
This will work , if you need values in Hex then use of "%x" is always there. : )
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008
Jul 9th, 2008
1

Re: How to find ASCII value of character in c

>void main (void)
This is not (and never has been) correct C. The main function returns int.

>a variable of type char is just an 8-bit int.
Just to be thorough, even though your reply is over a week old, char is only guaranteed to be at least eight bits.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 9th, 2008
0

Re: How to find ASCII value of character in c

Click to Expand / Collapse  Quote originally posted by Narue ...
>void main (void)
  1. #include<stdio.h>
  2. int main ()
  3. {
  4. char x;
  5. printf("\nEnter a Character : ");
  6. x = getchar();
  7. printf("\nThe ASCII for Char is: %d",x);
  8. return 0;/*** was avoiding this line ****/
  9. }
Thanks anyway ; )
Last edited by Software guy; Jul 9th, 2008 at 12:19 pm.
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008
Jul 9th, 2008
0

Re: How to find ASCII value of character in c

#include<stdio.h>
int main ()
{
    char x; 
    printf("\nEnter a Character : ");
    x = getchar();
   printf("\nThe ASCII for Char is: %d",x);
   return 0;/*** was avoiding this line ****/
 }
Since getchar returns an int , x should be an int . This is done to handle EOF .
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 9th, 2008
1

Re: How to find ASCII value of character in c

>return 0;/*** was avoiding this line ****/
Lazy? Seriously, do 8 characters of boilerplate really make enough of a difference to totally destroy the portability and correctness of your code?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
This thread is currently closed and is not accepting any new replies.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC