c语言编程文字游戏
C中的十六进制文字 (Hexadecimal literals in C)
Hexadecimal numbers are the technique to represent the numbers which uses the base-16 number; it uses 16 alphanumeric digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A/a, B/b, C/c, D/d, E/e, F/f. (Read: Computer number systems)
十六进制数字是一种表示使用以16为底的数字的数字的技术。 它使用16个字母数字数字0、1、2、3、4、5、6、7、8、9,A / a,B / b,C / c,D / d,E / e,F / f。 (阅读: 计算机号码系统 )
In C programming language, we can use hexadecimal literals in any expressions; we can assign hexadecimal numbers to the variables. To use hexadecimal literals, we use 0X or 0x as a prefix with the number. For example 0x10 is a hexadecimal number, which is equivalent to 16 in the decimal number system.
在C语言中,我们可以在任何表达式中使用十六进制文字 。 我们可以为变量分配十六进制数。 要使用十六进制文字,我们使用0X或0x作为数字的前缀。 例如, 0x10是十六进制数字,在十进制数字系统中等效于16 。
Printing hexadecimal number in decimal format
以十进制格式打印十六进制数字
To print a hexadecimal number in decimal format, we use %X or %x format specifier.
要以十进制格式打印十六进制数字,我们使用%X或%x格式说明符。
Printing hexadecimal number in hexadecimal format
以十六进制格式打印十六进制数
To print an hexadecimal number or other type of numbers in hex format, we use %X or %x format specifier.
要以十六进制格式打印十六进制数字或其他类型的数字,我们使用%X或%x格式说明符。
#include <stdio.h>
int main(){
//printing hexadecimal number in decimal format
printf("%d\n", 0x10);
//printing hexadecimal number in hexadecimal format
printf("%X\n", 0x10);
//printing other format number in hexadecimal format
printf("%X\n", 16);
return 0;
}
Output
输出量
16
10
10
Using hexadecimal literals in the expressions
在表达式中使用十六进制文字
#include <stdio.h>
int main(){
//adding two hexadecimal numbers
printf("%d\n", (0x10+0x20));
//adding hexadecimal, decimal numbers
printf("%d\n", (0x10+0x20+30+40));
return 0;
}
Output
输出量
48
118
Assigning hexadecimal literals to the variables
将十六进制文字分配给变量
#include <stdio.h>
int main(){
int a = 0x10;
int b = 0x76541;
printf("a in hexadecimal: %x, and in decimal: %d\n", a, a);
printf("b in hexadecimal: %x, and in decimal: %d\n", b, b);
return 0;
}
Output
输出量
a in hexadecimal: 10, and in decimal: 16
b in hexadecimal: 76541, and in decimal: 484673
Input a number in hexadecimal format
以十六进制格式输入数字
To input a number in an hexadecimal format, we use %X or %x format specifier in the scanf() function
要以十六进制格式输入数字,我们在scanf()函数中使用%X或%x格式说明符
#include <stdio.h>
int main(){
int a;
printf("Enter value of a in hexadecimal: ");
scanf("%x", &a);
printf("a in hex: %X, and in decimal: %d\n", a, a);
return 0;
}
Output
输出量
Enter value of a in hexadecimal: 0192Af
a in hex: 192AF, and in decimal: 103087
Error while using an invalid digit with hexadecimal literal
使用带十六进制文字的无效数字时出错
If we use any other digits except except 0-9 and A-F or a-f, program returns a compilation error "invalid digit 'x' in integer constant"
如果我们使用除0-9和AF或af之外的任何其他数字,程序将返回编译错误“整数常量中的无效数字'x'”
#include <stdio.h>
int main(){
int a = 0x10A;
int b = 0x129AG;
printf("a in hexadecimal: %X, and in decimal: %d\n", a, a);
printf("b in hexadecimal: %X, and in decimal: %d\n", b, b);
return 0;
}
Output
输出量
prog.c: In function 'main':
prog.c:3:13: error: invalid suffix "G" on integer constant
int b = 0x129AG;
^
Read more...
阅读更多...
翻译自: https://www.includehelp.com/c/hexadecimal-literals.aspx
c语言编程文字游戏