•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 403,039 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,842 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1234 | Replies: 16
![]() |
•
•
Join Date: Dec 2006
Posts: 215
Reputation:
Rep Power: 2
Solved Threads: 16
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 12:06 pm.
•
•
Join Date: Apr 2006
Posts: 35
Reputation:
Rep Power: 3
Solved Threads: 2
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: 32
Reputation:
Rep Power: 1
Solved Threads: 1
#include<stdio.h>
void main (void)
{
char x;
printf("\nEnter a Character : ");
x = getchar();
printf("\nThe ASCII for Char is: %d",x);
} >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.
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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: May 2008
Posts: 32
Reputation:
Rep Power: 1
Solved Threads: 1
#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 11:19 am.
•
•
•
•
#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 . >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?
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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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



Linear Mode