c - 如何使用 printf 格式化(有符号)长整型?
问题描述
如何使用 printf 格式化(签名的)long long int?
#include <stdio.h>
int main() {
long long int num = 123456766666666890; //FYI: fits in 29 bits
int RegularInteger = 4;
printf("My number is %d bytes wide and its value is %d. A normal number is %d.\n", sizeof(num), num, RegularInteger);
return 0;
}
输出:
My number is 8 bytes wide and its value is 1917383562. A normal number is 4.
我没有得到想要的输出。
解决方案
启用所有警告,一个好的编译器会警告不匹配的说明符并节省时间。
使用匹配的说明符。
使用 C99 或更高版本
#include <stdio.h>
int main() {
long long int num = 123456766666666890; // A 57 bit number
int RegularInteger = 4;
// printf("My number is %d bytes wide", sizeof(num));
printf("My number is %zu bytes wide", sizeof(num));
// printf("and its value is %d.", num);
printf("and its value is %lld.", num);
printf(" A normal number is %d.\n", RegularInteger);
return 0;
}
推荐阅读
- ruby-on-rails - 为什么 User.count 返回 0,直到 bin/spring 在 Ruby on Rails 中重新启动?
- laravel - 在 laravel 中设置的变量没有值
- webpack - 为什么将 babel preset-env 与 `useBuiltIns: "usage"` 一起使用会导致 Harmony-module 出错?
- php - 我可以使用 WP_List_Table 类在页面上呈现表格吗?
- c++ - C++ 入门 5 版:消息和文件夹类
- android - AppWidgetManager.getInstalledProviders() 不会返回所有已安装的小部件
- conda - 获得 conda 环境的完整解决方案
- c++ - FindPackageHandleStandardArgs.cmake 处的 CMake 错误:137(消息):找不到 Boost(缺少:正则表达式)(找到合适的版本“1.72.0”,
- python - 将主机自定义为“0.0.0.0”时,Hello World Flask 应用程序停止工作
- java - 交换变量的用户整数输入,使其递增和递减