为什么的sizeof(int)的==的sizeof(长)? [英] Why is the sizeof(int) == sizeof(long)?

查看:655
本文介绍了为什么的sizeof(int)的==的sizeof(长)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面列出的程序下,sizeof(int)和sizeof的(长)是我的机器(包括等于4个字节(或32位))在平等的。长,据我所知,是8个字节。它是否正确?我有一个64位计算机

 的#include<&stdio.h中GT;
#包括LT&;&limits.h中GT;诠释主要(无效){
    的printf(的sizeof(短)=%d个\\ N的sizeof(短));
    的printf(的sizeof(int)的=%d个\\ N的sizeof(INT));
    的printf(的sizeof(长)=%d个\\ N的sizeof(长));
    的printf(的sizeof(浮动)=%d个\\ N的sizeof(浮动));
    的printf(的sizeof(双)=%d个\\ N的sizeof(双));
    的printf(的sizeof(长双)=%d个\\ N的sizeof(长双));
    返回0;
}


解决方案

  

有一个很长,据我所知,是8个字节。这是正确的?


没有,这是不正确的。在C和C ++规范仅指出长必须大于或等于32位。 INT 可以更小,但在许多平台上,C和C ++, INT 都是32位。

这是一个很好的理由 preFER固定宽度整数类型的int64_t 如果他们提供给你,你正在使用C99或为您提供一个equivelent类型的框架。

In the program listed below, the sizeof(int) and sizeof(long) are equal on my machine (both equal 4 bytes (or 32 bits)). A long, as far as I know, is 8 bytes. Is this correct? I have a 64-bit machine

#include <stdio.h>
#include <limits.h>

int main(void){
    printf("sizeof(short) = %d\n", sizeof(short));
    printf("sizeof(int) = %d\n", sizeof(int));
    printf("sizeof(long) = %d\n", sizeof(long));
    printf("sizeof(float) = %d\n", sizeof(float));
    printf("sizeof(double) = %d\n", sizeof(double));
    printf("sizeof(long double) = %d\n", sizeof(long double));
    return 0;
}

解决方案

A long, as far as I know, is 8 bytes. Is this correct?

No, this is not correct. The C and C++ specifications only state that long must be greater than or equal to 32 bits. int can be smaller, but on many platforms, in C and C++, long and int are both 32 bits.

This is a very good reason to prefer fixed width integer types such as int64_t if they're available to you and you're using C99 or a framework which provides you an equivelent type.

这篇关于为什么的sizeof(int)的==的sizeof(长)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆