Finding the ASCII Code of a Character in C [duplicate] - c

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 1 year ago.
I'm learning C and I created a small program which outputs the ASCII code of the entered character or outputs the character from ASCII Code entered. It first asks the user whether they want to convert Character to ASCII Code or ASCII Code to Character.
I don't know what the problem is in my code. When I run it as a separate file, it works fine.
# include <stdio.h>
int main(void) {
char character;
printf("Enter a character: \n");
scanf("%c", &character);
printf("The character you entered: %c\n", character);
printf("ASCII code of %c is %d\n", character, character);
}
The above code works fine. This is the code which converts character to ASCII Code.
Here's the code which has the option to choose which one to run :
#include <stdio.h>
int main(void) {
char option;
printf("-------- MENU --------\n");
printf("1. Get ASCII code of a character.\n");
printf("2. Get character from ASCII code.\n");
printf(": ");
scanf("%s", &option);
if (option == '1') {
char character;
printf("Enter a character: \n");
scanf("%c", &character);
printf("The character you entered: %c\n", character);
printf("ASCII code of %c is %d\n", character, character);
}
else {
if (option == '2') {
int number;
printf("Enter a Number: \n");
scanf("%d", &number);
printf("The number you entered: %d\n\n", number);
printf("The ASCII character with code %d is %c\n", number, number);
}
else {
printf("Invalid Input !\n");
}
}
}
The above code asks the user to type in 1 or 2 to decide to run Convert Character to ASCII Code or Convert ASCII Code to Character. When I run this code, If I choose option 2, it works just fine and gives me a proper result and if I give 3 or any other invalid option, it gives me "Invalid Input !" printed on the screen. But, when I choose option 1, It just prints out this
-------- MENU --------
1. Get ASCII code of a character.
2. Get character from ASCII code.
: 1
Enter a character:
The character you entered:
ASCII code of
is 10
I don't know what the problem is. Please help me figure it out.

the problem is the second scanf and the %c. You have to add a white space like this scanf(" %c").
Full code:
#include <stdio.h>
int main(void) {
char option;
printf("-------- MENU --------\n");
printf("1. Get ASCII code of a character.\n");
printf("2. Get character from ASCII code.\n");
printf(": ");
scanf("%s", &option);
if (option == '1') {
char character;
printf("Enter a character: \n");
scanf(" %c", &character);
printf("The character you entered: %c\n", character);
printf("ASCII code of %c is %d\n", character, character);
}
else {
if (option == '2') {
int number;
printf("Enter a Number: \n");
scanf("%d", &number);
printf("The number you entered: %d\n\n", number);
printf("The ASCII character with code %d is %c\n", number, number);
}
else {
printf("Invalid Input !\n");
}
}
}
Output:
-------- MENU --------
1. Get ASCII code of a character.
2. Get character from ASCII code.
: 1
Enter a character:
a
The character you entered: a
ASCII code of a is 97
Please look at the following question were the reason is explained.

Related

Check validation from user in C programming

This is a snippet of my code. I'm confused why is digit can't function. It should if we input character/alphabet. The line is digit print "Please enter in numeric " but it doesn't print it. I need your opinion about this.
This my code:
printf("\nenter the amount of food to be purchased : ");
scanf("%d", &b);
printf("\n");
if (b >= 0) {
for (a=1; a<=b; a++){
printf("the price of food of- %d \t : ",a);
scanf("%d", &c);
printf("\n");
if (isdigit(c)) {
printf("Please enter in numeric !!\n");
while ((getchar()) != '\n');
system("PAUSE");
goto cashier;
}
printf("the amount ordered \t : ");
scanf("%d", &d);
printf("\n");
if (isdigit(d)) {
printf("Please enter in numeric !!\n");
while ((getchar()) != '\n');
system("PAUSE");
goto cashier;
}
scanf("%d", &c);. Reads an integer to c. When you call isdigit(c), you are not checking whether the input string is a number, you are checking whether the number inputted corresponds to an ascii character that represents a digit. This is not the intended behavior. What you want is this:
while (scanf("%d", &c) != 1) // Repeatedly get input until scanf reads 1 integer.
{
while (getchar()!='\n'); // Clear stdin.
puts("Please enter a number!");
}
// The resulting number is now stored in c.
This will try to read a number (not a string) into c. If the user does not enter 1 number, scanf() will not return 1 and the loop will try again. make sure that c is declared as an int and not a char, else numbers above 128 will overflow.

C program, getting the integer value of a character. Why everything comes out after line 10?

Hi I am new to programming and I got a trouble when I try to make a little change to the example in the book.
/* Chapter 3 Example, C Prime Plus */
#include <stdio.h>
int main(void)
{
char Letter, ch;
int intValue;
printf("Please enter a letter: \n");
scanf("%c", &Letter); /* user inputs character */
printf("The code for %c is %d.\n", Letter, Letter);
printf("Now is another we to implement the process: \n");
printf("RN, the value of ch is %c, and the value of intValue is %d\n", ch, intValue);
printf("Please enter a letter: \n");
scanf("%c", &ch);
intValue = ch;
printf("The code for %c is %d.\n", ch, intValue);
return 0;
}
When I run it, the outcome would be
Please enter a letter:
M
The code for M is 77.
Now is another we to implement the process:
RN, the value of ch is , and the value of intValue is 0
Please enter a letter:
The code for
is 10.
and the part
"
Now is another we to implement the process:
RN, the value of ch is , and the value of intValue is 0
Please enter a letter:
The code for
is 10. " will all come out without asking me to enter a value.
I want to know why and are there any other way to implement it that is different from examples in the book?
Thank you for your time!
Hi Matt_C and welcome to SO.
First, you don't need the second bloc of printfs and the scanf, it just trying to do the same thing and there is an order error.
Second, it is tricky when you try consecutive scanf, it holds the last key pressed (enter is the last key pressed = \n). This is why it skips the second scanf.
There a little solution for that, add a space at the beginning of the scanfs. Try this:
int main() {
char exit, letter;
while (1) {
printf("Please enter a letter: ");
scanf(" %c", &letter);
printf("\nThe code for '%c' is %d. \n\n", letter, letter);
printf("Exit ? (y/n): ");
scanf(" %c", &exit);
if(exit == 'y')
{
break;
}
system("clear"); // UNIX
//system("cls"); // DOS
}
}
Don't forget to choose one answer that you believe is the best solution to your problem.

getchar following scanf (to read an integer) takes terminating "enter" key of scanf as character

When I have a scanf followed by a getchar, why does the getchar always keep getting the last delimiting character of scanf? How can I stop that? I tried looking into "format specifiers" for scanf, read quite a few things but none solves this.
The code is shown below -
#include <stdio.h>
#include <conio.h>
int main()
{
int a;
char b;
printf ("Enter an integer \n");
scanf_s(" %d", &a);
printf("Enter a character \n");
b = getchar();
printf("The integer you entered is %d \n", a);
printf("The character you entered is %c \n", b);
_getch();
return 0;
}
The output is as below -
Enter an integer
4563
Enter a character
The integer you entered is 4563
The character you entered is
The enter key I press at the end of integer entry is being returned by getchar. The screen does not even stop after printing "Enter a character". What is the correct way to do this ?
Use the scanf(" %c", &b) instead of getchar()
When you put the space befor the %c you clean the buffer
Or you can clean the buffer using this too:
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
Complete example:
int main(void)
{
printf("Enter an integer \n");
int a;
scanf(" %d", &a);
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) {
}
printf("Enter a character \n");
char b = getchar();
printf("The integer you entered is %d \n", a);
printf("The character you entered is %c \n", b);
_getch();
}
But I think the scanf()

Why isn't the Isdigit function working properly?

I wrote this short code to test my understanding of the isdigit function:
int inChar;
printf("enter input:");
scanf(" %d", &inChar);
if (isdigit(inChar))
printf("Your input was a number");
else
printf("Your input was not a number.\n");
When I test this program and I enter a number, C returns the else statement (Your input was not a number.). So regardless of if I enter a number or a letter, the program returns the else statement.
Why is this so?
isdigit() checks if a single character that was passed to it by converting the char value an unsigned char.
So, you can't directly pass any int value and expect it to work.
Man isdigit() says:
isdigit()
checks for a digit (0 through 9).
To check single digit, you can modify:
char inChar;
printf("enter input:");
scanf(" %c", &inChar);
if (isdigit((unsigned char)inChar)) {
printf("Your input was a number");
}
else {
printf("Your input was not a number.\n");
}
If you have an array (a string containing a number) then you can use a loop.
The function's purpose is to classify characters (like '3'). Running it on something that's read using %d doesn't make sense.
You should read a single char using %c. Remember to check that reading succeeded.
The C library function void isdigit(int c) checks if the passed character is a decimal digit character.
If you badly wanna try it with an int you can init in this way
int inChar = '2';
The following code gave expected results.
int main()
{
char inChar;
printf("enter input:");
scanf(" %c", &inChar);
if (isdigit(inChar))
printf("Your input was a number. \n");
else
printf("Your input was not a number.\n");
return 0;
}
Output:
vinay-1> ./a.out
enter input:1
Your input was a number

Printing ASCII-code values and their equivalent chars in C

How could I print char for given ASCII code value?..
I saw this question, my problem is similar, just contrary
Below is my program, but it doesn't work.
int main (){
int code; // code that user enters
char ch; //"codes" related ASCII charcter
printf("Enter a ASCII code value: \n");
scanf("%d", &code);
ch=code;
printf(" %c is cherechter that have %d ASCII code\n", &ch ,&code);
system("PAUSE");
return 0;}
In your code, you have to change your printf statement
printf(" %c is cherechter that have %d ASCII code\n", &ch ,&code);
to
printf(" %c is cherechter that have %d ASCII code\n", ch ,code);
because, to print the values, you don't need to supply the address of the variables. The variable name is enough.
Change your code to:
int main (){
char code; //change from int to char
char ch;
printf("Enter a ASCII code value: \n");
scanf("%c", &code);//ASCII is a character not integer
ch=code;
printf(" %c is cherechter that have %x and %d ASCII code\n", ch ,code,code);//don't print the address access the value
system("PAUSE");
return 0;}

Resources