How to find ASCII value of character in c

Reply

Join Date: Jun 2008
Posts: 4
Reputation: sargarpramod is an unknown quantity at this point 
Solved Threads: 0
sargarpramod sargarpramod is offline Offline
Newbie Poster

How to find ASCII value of character in c

 
0
  #1
Jun 27th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: How to find ASCII value of character in c

 
0
  #2
Jun 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: How to find ASCII value of character in c

 
0
  #3
Jun 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 4
Reputation: sargarpramod is an unknown quantity at this point 
Solved Threads: 0
sargarpramod sargarpramod is offline Offline
Newbie Poster

Re: How to find ASCII value of character in c

 
0
  #4
Jun 28th, 2008
Thanks guys for replying to my query
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,607
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: How to find ASCII value of character in c

 
0
  #5
Jun 30th, 2008
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);
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

Re: How to find ASCII value of character in c

 
0
  #6
Jul 9th, 2008
  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. : )
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to find ASCII value of character in c

 
1
  #7
Jul 9th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

Re: How to find ASCII value of character in c

 
0
  #8
Jul 9th, 2008
Originally Posted by Narue View Post
>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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to find ASCII value of character in c

 
0
  #9
Jul 9th, 2008
Originally Posted by Software guy View Post
#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 .
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to find ASCII value of character in c

 
1
  #10
Jul 9th, 2008
>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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC