I need to take a keyboard input letter, and save that letter's decimal value as an integer.
How can I do that with scanf ?
I just figured out myself.
if I want to store the decimal value of 'z'
I can just do int value='z'-'a'+ 97
This is what I wanted to know.
This code reads a char from keyboard (stdin) using scanf(), stores it in byte-sized variable c of type char, and then prints its ASCII decimal value as an int to stdout :
char c;
scanf("%c", &c);
printf("%d", c);
If you want to output the ASCII value, simply printf() with the %d format string.
char ch = 'a';
printf("%d", a); // should be 97
Related
I'm trying to print out the digits of an integer using the char format specifier.
This is what I currently have
//counter = arbitrary int
while (counter > 0) {
char c = (char)(counter % 10);
printf("%c", c);
counter/=10;
}
This just prints out blank but if I change the format specifier to int but leave the type cast, it'll print the correct value. Something like this
char c = (char)(counter % 10);
printf("%d", c);
Any ideas why this isn't printing when I use the char format specifier?
%c is for printing a character, not a number. If you want to print a number, you should use %d. The reason it works for char is because char is implicitly promoted to int when you pass it as an argument to a variadic function.
If c contained 'A', and you used %c to format it, you would get the letter ‘A’, but if you used %d, you would get its integer value, e.g. 65.
If you absolutely must use %c, you can add the value of '0' to the character before printing, e.g.L
printf("%c", c + '0');
%c can be used in this context as printf("%c",c+'0') where +'0' will turn it into the integer equivalent of c.
what happens if I read an integer like 20,30,10000...9999 into variable a ? it only prints the first digit in the number that I've read...why is that ?
for example if I read 123, on the screen it prints 1. Isn't it supposed to convert the integer 123 into it's equivalent ASCII character representation ?
#include <stdio.h>
int main() {
char a;
scanf("%c", &a);
printf("%c", a);
return 0;
}
This is an exam question from C language.
No, it reads the character, which is represented by the machine as a small integer, into the variable.
If you enter 100 (the number 100, three keypresses and thus three characters), it will only store the first character of that, i.e. the leading 1.
If you wanted to convert a number to an actual integer, you should use %d and an int variable of course.
Printing with %c will print back a single character, by interpreting the small integer value as a character (rather than as an integer). So for an input of 100 you will see 1 printed back out, i.e. the character that represents the decimal digit one.
If you want to print out the numeric representation of the character you read in, scan with %c but print with %d, and cast the char to (int) in the printf() call.
The problem is that %c parse a char for console input. From a number like 123 it take only the first letter and dispose the rest. The way to parse a int value is using %d on the scanf function.
No, it will read only the first character into the char variable. How can a char variable store more than one character at an instant? It can't.
So if you want the ASCII value, input as an integer instead.
int a;
scanf("%d", &a); // suppose input is 65
printf("%c", a); // prints 'A'
printf("%d", a); // prints 65
Whereas
char a;
scanf("%c", &a); // suppose input is 65
printf("%c", a); // prints '6'
printf("%d", a); // prints 54 which is the ASCII value of '6'
I want to type in a number that is 10 digits long, then put the digits into an array. But for some reason, I get this random 2-digit numbers that seem to have nothing to do with my input (??).
char number[10]; //number containing 10 digits
scanf("%s",number); //store digits of number
printf("%d\n",number[0]); //print the 1st digit in the number
printf("%d\n",number[1]); //print the 2nd digit in the number
Here is what I got:
Input:
1234567890
Output:
49
50
Actually, 49 should be 1, and 50 should be 2.
You are getting ASCII value of characters 1 and 2. Use %c specifier to print the digits.
printf("%c\n",number[0]);
Warning! Your code may invoke undefined behaviour!
But we'll talk about it later. Let us address your actual question first.
Here is a step by step explanation of what is going on. The first thing you need to know is that every character literal in C is actually an integer for the compiler.
Try this code.
#include <stdio.h>
int main()
{
printf("%d\n", sizeof '1');
return 0;
}
The output is:
4
This shows that the character literal '1' is represented as 4 byte integer by the compiler. Now, let us see what this 4 byte integer for '1' is using the next code here.
#include <stdio.h>
int main()
{
int a = '1';
printf("a when intepreted as int : %d\n", a);
printf("a when intepreted as char: %c\n", a);
return 0;
}
Compile it and run it. You'll see this output.
a when intepreted as int : 49
a when intepreted as char: 1
What do we learn?
The character '1' is represented as the integer 49 on my system. This is so for your system too. That's because in my system as well as yours, the compiler is using ASCII codes for the integers where '1' is 49, '2' is 50, 'A' is 65, 'B' is 66, and so on. Note that the mapping of the characters to these codes could be different for another system. You should never rely on these integer codes to identify the characters.
So when I try to print this value as integer (using %d as the format specifier), well what gets printed is the integer value of '1' which is 49. However, if we print this value as a character (using %c as the format specifier), what gets printed is the character whose integer code is 49. In other words, 1 gets printed.
Now try this code.
#include <stdio.h>
int main()
{
char s[] = "ABC123";
int i;
printf("char %%d %%c\n");
printf("---- -- --\n");
for (i = 0; i < 6; i++) {
printf("s[%d] %d %c\n", i, s[i], s[i]);
}
return 0;
}
Now you should see this output.
char %d %c
---- -- --
s[0] 65 A
s[1] 66 B
s[2] 67 C
s[3] 49 1
s[4] 50 2
s[5] 51 3
Does it make sense now? You need to use the %c format specifier when you want to print the character. You should use %d only when you want to see the integer code that represents that character.
Finally, let us come back to your code. This is how you fix it.
#include <stdio.h>
int main()
{
char number[10];
scanf("%9[^\n]", number);
printf("%c\n", number[0]);
printf("%c\n", number[1]);
return 0;
}
There are two things to note.
I have used %c as the format specifier to print the character representation of the digits read.
I have altered the format specifier for scanf to accept at most 9 characters only where the characters are not newline characters. This is to make sure that a user cannot crash your program by inputting a string that is far longer than 9 characters. Why 9 instead of 10?. Because we need to leave one cell of the array empty for the null-terminator. A longer input would overwrite memory locations beyond the allocated 10 bytes for the number array. Such buffer overruns lead to code that invoke undefined behaviour which could either cause a crash or kill your cat.
printf("%c\n",number[0]); //print the 1st digit in the number
printf("%c\n",number[1]);
should do the job for you, what you see are ascii values.
your number array is an array of char, and so every element of it is a char.
when you type:
printf("%d\n",number[0]);
you printing the chars as integers, and so you get the ASCII code for each char.
change your statement to printf("%c\n",number[0]); to print chars as chars not as ints
Warning! Your code invokes undefined behaviour!
char number[10]; // Can only store 9 digits and nul character
scanf("%s",number); // Inputting 1234567890 (11 chars) will overflow the array!
Use fgets instead:
#define MAX_LEN 10
char number[MAX_LEN];
if(fgets(number, MAX_LEN, stdin)) {
// all went ok
}
Once you have fixed this, you can fix the printing problem. You are printing the character code (number), and not the actual character. Use different type specifier:
printf("%c\n",number[0]);
I have an application that prompt to user an character from user:
char letter;
printf("Letter:\n");
scanf("%s", &letter);
printf("ASCII code = %d\n", letter);
The problem is the accent that the user can write. if input is Á the code above given ASCII code = -61 then I thought, if I turn it in an positive number, I get 61 that is A in ASCII. printf("ASCII code = %d val = %c\n", letter, abs(letter));
but it does not works as expected, it given ASCII code = -61 val =
instead of A why?
%s is the format specifier for a C-string, and a character is only big enough to hold an empty C-string. Use %c, which is the format specifier for a single character.
Á has character code 193 in ASCII.
The range of a char is -128 to 127. The range of unsigned char is 0 to 255.
The problem with your code is that you're working with a signed char instead of an unsigned char. When you output the signed char, it will sign extend letter to an integer and output it. If you use the unsigned variant you'll get the correct output.
In C is there a way to convert an ASCII value typed as an int into the the corresponding ASCII character as a char?
You can assign int to char directly.
int a = 65;
char c = a;
printf("%c", c);
In fact this will also work.
printf("%c", a); // assuming a is in valid range
If i is the int, then
char c = i;
makes it a char. You might want to add a check that the value is <128 if it comes from an untrusted source. This is best done with isascii from <ctype.h>, if available on your system (see #Steve Jessop's comment to this answer).
If the number is stored in a string (which it would be if typed by a user), you can use atoi() to convert it to an integer.
An integer can be assigned directly to a character. A character is different mostly just because how it is interpreted and used.
char c = atoi("61");
char A;
printf("ASCII value of %c = %d", c, c);
In this program, the user is asked to enter a character. The character is stored in variable c.
When %d format string is used, 65 (the ASCII value of A) is displayed.
When %c format string is used, A itself is displayed.
Output:
ASCII value of A = 65
#include <stdio.h>
#include <cs50.h>
int d;
int main(void){
string a = get_string("enter your word: ");
int i = 0;
while(a[i] != '\0'){
printf("%i ", a[i]);
i++;
}
printf("\n");
}