+ 1
printing value of char variable in C
hi everyone how can i print the value like 127 in C as a signed char or 255 as unsigned char, as i have tried using priintf("%c",char_var) but got garbage values or a single character. please help. thanks in advance.
8 Réponses
+ 1
i am working on a sensor having 8bit value i want to use char to store and print its value,suggest me.
+ 1
char ch = 'a' ;
pruntf("%c = %d", ch, ch);
Outout:
a = 97
Check this code for all values of charf from 1 to 255
#include <stdio.h>
int main() {
for(unsigned char c=1; c<=255;c++)
printf("%c = %d \n",c ,c);
return 0;
}
+ 1
Jayakrishna🇮🇳 and what about inputting value in char variable like 127 ?
+ 1
and then printing it.
+ 1
Jayakrishna🇮🇳
thanks for your help but i got that,
as
char c;
scanf("%d",c);
printf("%hhd",c);
+ 1
it will take input of length equal to char as integer.
0
You mean int 127 to char ch
Like
char ch;
scanf("%c", &ch);
and input 127, then it only read 1, as you taking char %c..
If you want int 127, as a charecter asci value, then
take it as int,
int ch;
scanf("%d", &ch);
Input 127,
printf("%c", ch);
127 is I think, invisible character, so try with 97, then
You output is 'a'.
0
Jayakrishna🇮🇳
check it out.
#include <stdio.h>
int main() {
char i;
scanf("%hhd",&i);
printf("%c=%hhd\n",i,i);
return 0;
}