scanf 输入十六进制
Here, we will declare an unsigned char variable and input different format's value like decimal format, octal format and hexadecimal format.
在这里,我们将声明一个无符号的char变量,并输入不同格式的值,例如十进制格式,八进制格式和十六进制格式。
To input and print decimal value – we use "%d" format specifier
输入和打印十进制值–我们使用“%d”格式说明符
To input and print octal value – we use "%o" format specifier
输入和打印八进制值–我们使用“%o”格式说明符
To input and print hexadecimal value – we use "%x" format specifier
输入和打印十六进制值–我们使用“%x”格式说明符
Program:
程序:
#include <stdio.h>
int main(void)
{
//data range of unsigned char is in,
//1) decimal format 0 to 255
//2) octal format 0 to 377
//3) hexadecimal format 0 to ff
unsigned char var;
printf("Enter decimal value b/w 0 to 255: ");
scanf("%d", &var);
printf("var = %d\n", var);
printf("Enter octal value b/w 0 to 377: ");
scanf("%o", &var);
printf("var = %o\n", var);
printf("Enter hexadecimal value b/w 0 to ff: ");
scanf("%x", &var);
printf("var = %x\n", var);
return 0;
}
Output
输出量
Enter decimal value b/w 0 to 255: 198
var = 198
Enter octal value b/w 0 to 377: 172
var = 172
Enter hexadecimal value b/w 0 to ff: f9
var = f9
翻译自: https://www.includehelp.com/c-programs/input-decimal-octal-and-hexadecimal-values-in-character-variables.aspx
scanf 输入十六进制