+ 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.

2nd Sep 2020, 7:14 PM
Muhammad Faheem Nasir
Muhammad Faheem Nasir - avatar
8 odpowiedzi
+ 1
i am working on a sensor having 8bit value i want to use char to store and print its value,suggest me.
2nd Sep 2020, 7:19 PM
Muhammad Faheem Nasir
Muhammad Faheem Nasir - avatar
+ 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; }
2nd Sep 2020, 7:53 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 and what about inputting value in char variable like 127 ?
3rd Sep 2020, 3:05 AM
Muhammad Faheem Nasir
Muhammad Faheem Nasir - avatar
+ 1
and then printing it.
3rd Sep 2020, 3:06 AM
Muhammad Faheem Nasir
Muhammad Faheem Nasir - avatar
+ 1
Jayakrishna🇮🇳 thanks for your help but i got that, as char c; scanf("%d",c); printf("%hhd",c);
5th Sep 2020, 3:57 AM
Muhammad Faheem Nasir
Muhammad Faheem Nasir - avatar
+ 1
it will take input of length equal to char as integer.
5th Sep 2020, 3:58 AM
Muhammad Faheem Nasir
Muhammad Faheem Nasir - avatar
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'.
4th Sep 2020, 9:28 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 check it out. #include <stdio.h> int main() { char i; scanf("%hhd",&i); printf("%c=%hhd\n",i,i); return 0; }
8th Sep 2020, 5:04 AM
Muhammad Faheem Nasir
Muhammad Faheem Nasir - avatar