| | |
How to find ASCII value of character in c
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Here is sample code, which demonstrates on how to get a the ASCII code for the entered char.
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
C Syntax (Toggle Plain Text)
#include <stdio.h> #define TRUE 1 void clear_buffer( void ) { int ch; while( ( ch = getchar() ) != '\n' && ch != EOF ); } int main() { unsigned int ch; while( TRUE ) { printf("\nsingle char please - " ); ch = getchar(); clear_buffer(); printf("%c - %d", ch, ch ); } } /* my output single char please - a a - 97 single char please - s s - 115 single char please - d d - 100 single char please - */
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.
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:
myNum would have the value 97 in it.
c Syntax (Toggle Plain Text)
int myNum = 'a';
myNum would have the value 97 in it.
•
•
Join Date: May 2008
Posts: 38
Reputation:
Solved Threads: 2
C Syntax (Toggle Plain Text)
#include<stdio.h> void main (void) { char x; printf("\nEnter a Character : "); x = getchar(); printf("\nThe ASCII for Char is: %d",x); }
•
•
Join Date: May 2008
Posts: 38
Reputation:
Solved Threads: 2
C Syntax (Toggle Plain Text)
#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 ****/ }
Last edited by Software guy; Jul 9th, 2008 at 12:19 pm.
•
•
•
•
#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 ****/ }
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
![]() |
Similar Threads
- numeric TextBox (VB.NET)
- ArrayOutofBounds (Java)
- Pls help :Find first non-repeated char in a string (C++)
- random (C++)
- Codes FOr VGA/SVGA using ports and not interrupts (Assembly)
- HELP - Printing Integers (Assembly)
- GetKey via Interrupt or I/O (Assembly)
- String to integer to ascii (C)
- Array troubles? (C++)
- 4Q i cannot find in my book (Java)
Other Threads in the C Forum
- Previous Thread: any tutorial web site
- Next Thread: help me with this code
Views: 5851 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for C
* adobe api append array arrays bash binarysearch char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest homework i/o ide include infiniteloop initialization interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scheduling segmentationfault send single socketprogramming spoonfeeding stack standard strchr string student suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32 win32api windows.h






