[英]How to print in hexadecimal,decimal and octal form a constant in C
I'm having some issues resolving one of my assignments.I have searched documentation on the web but I cannot find a concise answer. 我在解决我的一项任务时遇到一些问题。我在网上搜索了文档,但找不到简明的答案。
I have to print a constant eg123456789 in hexadecimal,decimal and octal form. 我必须以十六进制,十进制和八进制形式打印常量eg123456789。
I know that 0x is for hexadecimal,0 for decimal and nothing for octal.Yet I have no ideea how to print it.I have used the #define method to define my constant 我知道0x代表十六进制,0代表十进制,而八进制则没有。我还没有想法如何打印它。我已经使用#define方法定义了我的常数
#include <stdio.h>
#include <stdlib.h>
#define CONST 123456789
int main()
{
printf("%d",0xCONST);
return 0;
}
Can someone help? 有人可以帮忙吗?
Read up on the printf
format specifiers on the printf man page . 在printf手册页上阅读printf
格式说明符 。
Code: 码:
#include <stdio.h>
#define CONST 123456789
int main()
{
printf("%d = %#x = %#o\n", CONST, CONST, CONST);
return 0;
}
Compile and test: 编译测试:
$ gcc -Wall dec-hex-oct.c && ./a.out
123456789 = 0x75bcd15 = 0726746425
$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.