c语言long的格式化输出
时间: 2023-09-22 15:07:27 浏览: 194
在C语言中,可以使用以下格式化输出符号来输出long类型数据:
- %ld:用于输出10进制的long整数
- %lo:用于输出8进制的long整数
- %lx或%X:用于输出16进制的long整数,其中%lx输出小写字母,%lX输出大写字母
下面是一个示例代码,演示如何使用这些格式化输出符号输出long类型数据:
```c
#include <stdio.h>
int main() {
long num1 = 1234567890;
long num2 = 0777;
long num3 = 0xABCDEF;
printf("num1 in decimal: %ld\n", num1);
printf("num1 in octal: %lo\n", num1);
printf("num1 in hexadecimal (lowercase): %lx\n", num1);
printf("num1 in hexadecimal (uppercase): %lX\n", num1);
printf("num2 in decimal: %ld\n", num2);
printf("num2 in octal: %lo\n", num2);
printf("num2 in hexadecimal (lowercase): %lx\n", num2);
printf("num2 in hexadecimal (uppercase): %lX\n", num2);
printf("num3 in decimal: %ld\n", num3);
printf("num3 in octal: %lo\n", num3);
printf("num3 in hexadecimal (lowercase): %lx\n", num3);
printf("num3 in hexadecimal (uppercase): %lX\n", num3);
return 0;
}
```
输出结果如下:
```
num1 in decimal: 1234567890
num1 in octal: 11145401322
num1 in hexadecimal (lowercase): 499602D2
num1 in hexadecimal (uppercase): 499602D2
num2 in decimal: 511
num2 in octal: 777
num2 in hexadecimal (lowercase): 1FF
num2 in hexadecimal (uppercase): 1FF
num3 in decimal: 11259375
num3 in octal: 52713157
num3 in hexadecimal (lowercase): ABCDEF
num3 in hexadecimal (uppercase): ABCDEF
```
阅读全文