Printing ASCII-code values and their equivalent chars in C - 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;}

Related

Why does the second "a" have different value than the first "a"?

I'm trying to get the difference of two characters.
My problem is when I input the 2nd character it gives a different value even if the character is just the same as the first input:
Example below. I input "a" as first char and then "a" again for the second but it gives different value
int main(){
char* flet;
char* slet;
printf("Input first character:");
scanf("%c", &flet);
fflush(stdin);
printf("Input Second character:");
scanf("%c", &slet);
printf("First Char is \"%c\" and Second Char is \"%c\".", flet,slet);
DiffofChar(flet,slet);
}
void DiffofChar(char* letter1, char* letter2){
int theDiff;
theDiff = letter1 - letter2;
printf("The difference of %c (%d) and %c (%d) is %d.", letter1, letter1, letter2, letter2, theDiff);
}
Output:
This is incorrect:
char* flet;
char* slet;
printf("Input first character:");
scanf("%c", &flet);
At best, the above will cast the character value typed (a) into a pointer and that address (0x61 == 'a') will just be a pointer value for an invalid memory location.
The fix is to declare the variables as just type char. You still pass &flet and &slet to the scanf functions.
char flet;
char slet;
printf("Input first character:");
scanf("%c", &flet);

I'm struggling with scanf with float

I am having some issues with the scanf function.
I am trying to printf a scanf result, but it only returns 0.00000
Can someone help me? Here is my code done so far:
#include <stdio.h>
int main() {
float grade;
char choise;
do {
printf ("Type your grade: ");
scanf("%f", &grade);
printf("Want to continue? s/n: ");
scanf(" %c", &choise);
printf("%s \n", &choise);
printf("%f", &grade);
}while(choise != 'n');
}
printf("%f", &grade); is wrong
try
printf("%f", grade);
But there are many other issues. At the very least, you must check the values returned by scanf, and you should use int main(void) or int main(int argc, char **argv). eg:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
float grade;
char choise;
do {
printf("Type your grade: ");
fflush(stdout);
if( scanf("%f", &grade) != 1 ){
fprintf(stderr, "Invalid input\n");
exit(1);
}
printf("Want to continue? s/n: ");
fflush(stdout);
if( scanf(" %c", &choise) != 1 ){
fprintf(stderr, "Invalid input\n");
exit(1);
}
printf("%c\n", choise);
printf("%f\n", grade);
} while( choise != 'n' );
return 0;
}
If you don't check the value returned by scanf, you don't know if any data was written into the variable. Since neither choise nor grade is initialized, attempting to read those values if scanf did not assign to them is undefined behavior. The behavior is also undefined if the input stream contains a value that cannot be represented as a float (eg, if it is a value greater than FLT_MAX), but that's really just an argument for avoiding scanf rather than a suggestion to try to make scanf usable. You can try to use scanf to make a user friendly interface, but it's really not worth the effort. Much better to simply abort on bad input. (If you want a user friendly interface, I would recommend you are using the wrong language.) See http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html for more details on why you really ought to just avoid scanf completely.
These calls of printf
printf("%s \n", &choise);
printf("%f", &grade);
are incorrect.
In the first call you are trying to output a single character as a string. In the second call you are trying to output a pointer of the type float * as an object of the type float.
Instead you have to write
printf("%c\n", choise);
printf("%f\n", grade);
The lines
printf("%s \n", &choise);
printf("%f", &grade);
are wrong.
They should be
printf("%c \n", choise);
printf("%f", grade);
The %s format specifier should only be used for strings (null-terminated sequences of characters), not individual characters. The expression &choice does not point to a string, as the character sequence is not null-terminated.
Also, the %c and %f format specifiers require values, not addresses, when using them with printf. Only when using them with scanf should you pass an address.

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

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.

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()

How to convert ASCII code to corresponding int using getchar?

I basically want to know what to put in the last printf statement for the first %d, because from my understanding getchar converts the inputted character to ASCII code. So how do I display the inputted character?
#include <stdio.h>
int main(void) {
int c;
printf("Enter a character: ");
c = getchar();
printf("The ASCII code for the character %d is %d\n", what to put here, c);
return 0;
}
You need to provide the %c format specifier in the format string instead of %d:
printf("The ASCII code for the character %c is %d\n", c, c);

Resources