scanf 输入十六进制 |
您所在的位置:网站首页 › 从键盘输入一个十六进制数,将其转化为十进制数输出 › scanf 输入十六进制 |
scanf 输入十六进制 Here, we have to declare an unsigned int variable and input a value in hexadecimal format. 在这里,我们必须声明一个无符号的int变量,并以十六进制格式输入一个值。 To input a value in hexadecimal format – we use "%x" or "%X" format specifier and to print the value in hexadecimal format – we use same format specifier "%x" or "%X". 要以十六进制格式输入值-我们使用“%x”或“%X”格式说明符并以十六进制格式打印值-我们使用相同格式的说明符“%x”或“%X” 。 "%x" – prints value with lowercase alphabets (a to f) “%x” –用小写字母(a至f)打印值 "%X" – prints value with uppercase alphabets (A to F) “%X” –用大写字母(A到F)打印值 Note: In scanf(), you can use both of the format specifiers "%x" or "%X" – it does not affect to user input, but in the printf() – "%x" or "%X" matters for printing alphabets in hexadecimal value (a to f or A to F). 注意:在scanf()中 ,可以同时使用格式说明符“%x”或“%X” –它不会影响用户输入,但是在printf()中 – “%x”或“%X”很重要用于以十六进制值(a到f或A到F)打印字母。 Program 1: 程序1: #include int main(void) { unsigned int value; //input "123afc" printf("Enter hexadecimal value without \"0x\": "); //using %x (small x) scanf("%x", &value); printf("value = 0x%x or 0X%X\n", value, value); //input "123AfC" printf("Enter hexadecimal value without \"0X\": "); //using X (capital x) scanf("%X", &value); printf("value = 0x%x or 0X%X\n", value, value); return 0; }Output 输出量 Enter hexadecimal value without "0x": 123afc value = 0x123afc or 0X123AFC Enter hexadecimal value without "0X": 123AFC value = 0x123afc or 0X123AFCProgram 2: Testing program with invalid hexadecimal value 程序2:测试程序的十六进制值无效 #include int main(void) { unsigned int value; //testing with invalue value //while input, we are using alphabets //which are greater than F //as we know, hexadecimal allowes only //A to F / a to f - which are equivelant //to 10 to 15 printf("Enter a hexadecimal value: "); scanf("%x", &value); printf("value = %x\n", value); return 0; }Output 输出量 Enter a hexadecimal value: 123apd value = 123aExplanation: 说明: In the hexadecimal value 123apd, "p" is not a hexadecimal digit, thus, the input is valid/acceptable till valid digits. Given input 123pad is considered as 123a. 在十六进制值123apd中 , “ p”不是十六进制数字,因此输入直到有效数字都是有效/可接受的。 给定输入123pad被视为123a 。 翻译自: https://www.includehelp.com/c-programs/input-a-hexadecimal-value-using-scanf.aspx scanf 输入十六进制 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |