I would like to know how could I use the value of an input char in C.
Ex:
The user types a, how could I use the value of a (97) in my code? Is that possible?
MORE DETAILS: I made a while loop to get inputs from the user. If the user inputs a number 1-9, then I increment a variable called numbers, but the problem comes now: If the user types a for example, I made a condition to check wether the input is greater than 95 or not, and it does not work. What I want is to get this input (scanf("%d")) and if it is a letter (ascii >= 97), then increment another variable called letters.
Part of the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long int teclas=0, letras=0, numeros=0, i=0, aux=0, k=0, j=0, flag=0,
numprint=0, seq=0;
scanf("%lld", &teclas);
while(i<=teclas){
scanf("%lld", &aux);
if(aux>0 && aux<10){
numeros ;
aux = 0;
}else if(aux>96 && aux<123){
letras ;
aux = 0;
}
i ;
}
CodePudding user response:
Yes, you can use the ASCII value of characters. Refer to the below example.
char c;
scanf("%c", &c);
int asciiValue;
asciiValue = c; // Here we are typecasting char to int implicitly.
printf("ASCII val of %c is %d", c, asciiValue);
Here the variable asciiValue
will have the int value of the character, and you can use it for any operations that you may want to perform in your code.
Note: char is 1 byte signed integer whereas int is a 4-byte signed integer.
CodePudding user response:
What I want is to get this input (scanf("%d")) and if it is a letter (ascii >= 97), then increment another variable called letters.
The short answer is: You can't read a letter using the format specifier %d
But you can do it in another way by checking the return value of scanf
.
Something like:
int input_number;
char input_char;
if (scanf("%d", &input_number) == 1) // Try to pass input as an int
{
// The user typed a valid integer number which is now stored in input_number
if(input_number>0 && input_number<10)
{
numeros ;
}
}
else if (scanf("%c", &input_char) == 1) // Didn't get an int so read a single char
{
// The user typed a non-integer char which is now stored in input_char
//
// Now you can do:
if (input_char >= 'a' && input_char <= 'z')
{
letras ;
}
}
else
{
// Input error
exit(1);
}
CodePudding user response:
char c='a'
int value=a;
printf("%d",value);
This is called as implicit casting
.output of this program will be 97
which is ascii code
for a
;
OR You can directly print it using
printf("%d",'a');
Output
97
CodePudding user response:
If you want the ASCII code then you can just do this:
#include <stdio.h>
int main()
{
char a = 'a';
int x = (int)a;
printf("%d", x);
return 0;
}
CodePudding user response:
You already get what you need: the value in the variable. Wheter it is a character('A','a') or an integrate number(65, 97) only depends on how you interpret it. See the example below:
#include <stdio.h>
#include <ctype.h>
int main(int argc, char **argv) {
char input_buf = 0;
printf("input any character:\n");
while (1) {
scanf("%c", &input_buf);
if (isprint(input_buf)) {
printf("\nthe value you just input: %c, the value: %d "
",\n Ctrl C or Ctrl Z to exit\n",
input_buf, (int)input_buf);
printf("\ninput any character:\n");
}
}
return 0;
}