c语言编程文字游戏_C编程语言中的十六进制(十六进制)文字

本文介绍了C编程语言中如何使用十六进制文字,包括它们的表示方式、在表达式中的使用、赋值给变量以及以不同格式打印和输入十六进制数。同时,还提到了使用无效数字时的编译错误。
摘要由CSDN通过智能技术生成

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语言编程文字游戏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值