scanf 输入十六进制_在C语言中使用scanf()输入一个十六进制值
scanf 輸入十六進(jìn)制
Here, we have to declare an unsigned int variable and input a value in hexadecimal format.
在這里,我們必須聲明一個(gè)無符號(hào)的int變量,并以十六進(jìn)制格式輸入一個(gè)值。
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".
要以十六進(jìn)制格式輸入值-我們使用“%x”或“%X”格式說明符并以十六進(jìn)制格式打印值-我們使用相同格式的說明符“%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()中 ,可以同時(shí)使用格式說明符“%x”或“%X” –它不會(huì)影響用戶輸入,但是在printf()中 – “%x”或“%X”很重要用于以十六進(jìn)制值(a到f或A到F)打印字母。
Program 1:
程序1:
#include <stdio.h>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 0X123AFC .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Program 2: Testing program with invalid hexadecimal value
程序2:測試程序的十六進(jìn)制值無效
#include <stdio.h>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 15printf("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.
在十六進(jìn)制值123apd中 , “ p”不是十六進(jìn)制數(shù)字,因此輸入直到有效數(shù)字都是有效/可接受的。 給定輸入123pad被視為123a 。
翻譯自: https://www.includehelp.com/c-programs/input-a-hexadecimal-value-using-scanf.aspx
scanf 輸入十六進(jìn)制
總結(jié)
以上是生活随笔為你收集整理的scanf 输入十六进制_在C语言中使用scanf()输入一个十六进制值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java ObjectInputStre
- 下一篇: c#五个自然日 工作日计算_C#程序输入