我想size_t在C中打印出一个类型的变量,但似乎size_t在不同的体系结构上别名为不同的变量类型.例如,在一台计算机(64位)上,以下代码不会抛出任何警告:
size_t size = 1;
printf("the size is %ld", size);
Run Code Online (Sandbox Code Playgroud)
但在我的另一台机器(32位)上面的代码会产生以下警告消息:
警告:格式'%ld'需要类型'long int*',但参数3的类型为'size_t*'
我怀疑这是由于指针大小的不同,所以在我的64位机器size_t上别名为a long int("%ld"),而在我的32位机器size_t上别名为另一种类型.
是否有专门用于的格式说明符size_t?
c size-t platform-independent format-specifiers format-string
Size_t被定义为一个unsigned整数,但它的大小取决于你是在32位还是64位机器上.什么是正确和便携的打印方式size_t?
在64位系统上,sizeof(unsigned long)取决于系统实现的数据模型,例如,它在LLP64(Windows)上为4个字节,在LP64(Linux等)上为8个字节.什么是sizeof(size_t)应该是什么?它是否随数据模型而变化sizeof(long)?如果是这样,怎么样?
参考文献:
我想在C++中使用Microsoft Visual Studio 2010 打印出size_t变量的值printf(我想使用printf而不是<<在这段特定的代码中,所以请不要回答告诉我应该使用它<<).
根据帖子
正确的平台无关方式是使用%zu,但这似乎在Visual Studio中不起作用.Visual Studio文档
http://msdn.microsoft.com/en-us/library/vstudio/tcxf1dw6.aspx
告诉我,我必须使用%Iu(使用大写i,而不是小写l).
微软是否不遵守这些标准?或者自C99以来标准是否已更改?或者C和C++之间的标准是不同的(这对我来说似乎很奇怪)?
在跨平台的c/c ++项目(Win32,Linux,OSX)中,我需要使用*printf函数来打印一些size_t类型的变量.在某些环境中,size_t是8个字节,而在其他环境中它们是4.在glibc上我有%zd,在Win32上我可以使用%Id.有一种优雅的方式来处理这个问题吗?
我一直收到编译警告,但我不知道如何解决它:
'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [
Run Code Online (Sandbox Code Playgroud)
程序运行正常,但我仍然收到编译警告:
/* Sizeof.c--Program to tell byte size of the C variable */
#include <stdio.h>
int main(void) {
printf("\nA Char is %d bytes", sizeof( char ));
printf("\nAn int is %d bytes", sizeof( int ));
printf("\nA short is %d bytes", sizeof( short ));
printf("\nA long is %d bytes", sizeof( long ));
printf("\nA long long is %d bytes\n", sizeof( long long ));
printf("\nAn unsigned Char is %d …Run Code Online (Sandbox Code Playgroud) 我试图理解C中的结构的内存分配,但我坚持下去.
struct Person {
char *name;
int age;
int height;
int weight;
};
struct Person *Person_create(char *name, int age, int height, int weight)
{
struct Person *who = malloc(sizeof(struct Person));
assert(who != NULL);
who->age = age;
who->height = height;
who->weight = weight;
who->name = strdup(name);
return who;
}
int main(int argc, char *argv[])
{
struct Person *joe = Person_create("ABC", 10, 170, 60);
printf("Size of joe: %d\n", sizeof(*joe));
printf("1. Address of joe \t= %x\n", joe);
printf("2. Address of Age \t= %x\n", …Run Code Online (Sandbox Code Playgroud) 我按照一些类似的帖子来修改代码,但是仍然会生成警告.
$ g++ ncfile.c -o ncfile -g -lnetcdf
ncfile.c: In function ‘int main(int, char**)’:
ncfile.c:363: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:363: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:364: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:364: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
Run Code Online (Sandbox Code Playgroud)
在第363和364行的那个区块中:
for(i = 0; i < ndimsp; i++) {
char * temp_name;
size_t temp_len; …Run Code Online (Sandbox Code Playgroud) 我有一个代码库,旨在在没有警告的情况下进行编译,并在多种架构上运行而不会出现任何故障,所有 x86:MSDOS、Windows 32 控制台模式、Windows 32 GUI 模式、Linux 32 和 Linux 64。
\n\n在添加对 Linux 64 的支持之前,这很容易。几乎所有数据都被声明为intBYTE、WORD 和 DWORD 的类型定义:
typedef unsigned char BYTE;\ntypedef unsigned short WORD;\ntypedef unsigned long DWORD;\nRun Code Online (Sandbox Code Playgroud)\n\n添加 64 位 gcc 支持后,DWORD 需要进行一些调整才能保持 32 位值,因为它表示存储的数据:
\n\n// to compile DWORDs as 32 bits on 64-bit machines:\n#if __x86_64__\n typedef unsigned int DWORD;\n#else\n typedef unsigned long DWORD;\n#endif\nRun Code Online (Sandbox Code Playgroud)\n\n这在所有环境下都运行良好:
\n\nDWORD data;\nprintf ("%lu", data);\nRun Code Online (Sandbox Code Playgroud)\n\n但是,gcc -Wall现在抱怨格式转换:
warning: format \xe2\x80\x98%ld\xe2\x80\x99 expects argument of …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include<stdio.h>
int main()
{
printf("The 'int' datatype is \t\t %lu bytes\n", sizeof(int));
printf("The 'unsigned int' data type is\t %lu bytes\n", sizeof(unsigned int));
printf("The 'short int' data type is\t %lu bytes\n", sizeof(short int));
printf("The 'long int' data type is\t %lu bytes\n", sizeof(long int));
printf("The 'long long int' data type is %lu bytes\n", sizeof(long long int));
printf("The 'float' data type is\t %lu bytes\n", sizeof(float));
printf("The 'char' data type is\t\t %lu bytes\n", sizeof(char));
}
Run Code Online (Sandbox Code Playgroud)
哪些输出:
The 'int' datatype is 4 bytes
The …Run Code Online (Sandbox Code Playgroud)