是C“int"的大小吗?2 字节还是 4 字节? [英] Is the size of C "int" 2 bytes or 4 bytes?

查看:26
本文介绍了是C“int"的大小吗?2 字节还是 4 字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 中的整数变量是占用 2 个字节还是 4 个字节?它取决于哪些因素?

Does an Integer variable in C occupy 2 bytes or 4 bytes? What are the factors that it depends on?

大部分教科书都说整型变量占用2个字节.但是当我运行一个程序打印一个整数数组的连续地址时,它显示了 4 的差异.

Most of the textbooks say integer variables occupy 2 bytes. But when I run a program printing the successive addresses of an array of integers it shows the difference of 4.

推荐答案

我知道它等于 sizeof(int).int 的大小实际上取决于编译器.过去,当处理器是 16 位时,int 是 2 个字节.如今,在 32 位和 64 位系统上,它通常是 4 个字节.

I know it's equal to sizeof(int). The size of an int is really compiler dependent. Back in the day, when processors were 16 bit, an int was 2 bytes. Nowadays, it's most often 4 bytes on a 32-bit as well as 64-bit systems.

不过,使用 sizeof(int) 是获取执行程序的特定系统的整数大小的最佳方法.

Still, using sizeof(int) is the best way to get the size of an integer for the specific system the program is executed on.

修正了在大多数 64 位系统上 int 为 8 个字节的错误声明.例如,在 64 位 GCC 上为 4 个字节.

Fixed wrong statement that int is 8 bytes on most 64-bit systems. For example, it is 4 bytes on 64-bit GCC.

这篇关于是C“int"的大小吗?2 字节还是 4 字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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