数据类型的大小取决于什么? [英] On what does size of data types depend?

查看:118
本文介绍了数据类型的大小取决于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在linux中使用gcc编译器。我编译了一个小程序

int main()

{

printf(" char:%d \ n",sizeof(char));

printf(" unsigned char:%d \ n" ,sizeof(unsigned char));

printf(" short:%d \ n",sizeof(short));

printf(" unsigned short: %d \ n",sizeof(unsigned short));

printf(" int:%d \ n",sizeof(int));

printf (" unsigned int:%d \ n",sizeof(unsigned int));

printf(" long:%d \ n",sizeof(long));

printf(" unsigned long:%d \ n",sizeof(unsigned long));

printf(" long long:%d \ n",sizeof(很长));

printf(" unsigned long long:%d \ n",sizeof(uns)签长期

长));

}


结果是


char :1

unsigned char:1

短:2

unsigned short:2

int:4

unsigned int:4

long:4

unsigned long:4

long long:8

unsigned long long:8

我想知道的是,如果我在linux上运行的应用程序中使用int in $ $ $ b,那将会产生什么影响关于什么因素

数据类型的大小取决于。


谢谢

Sunil。

Hi all,

I am using gcc compiler in linux.I compiled a small program
int main()
{
printf("char : %d\n",sizeof(char));
printf("unsigned char : %d\n",sizeof(unsigned char));
printf("short : %d\n",sizeof(short));
printf("unsigned short : %d\n",sizeof(unsigned short));
printf("int : %d\n",sizeof(int));
printf("unsigned int : %d\n",sizeof(unsigned int));
printf("long : %d\n",sizeof(long));
printf("unsigned long : %d\n",sizeof(unsigned long));
printf("long long : %d\n",sizeof(long long));
printf("unsigned long long : %d\n",sizeof(unsigned long
long));
}

Result was

char : 1
unsigned char : 1
short : 2
unsigned short : 2
int : 4
unsigned int : 4
long : 4
unsigned long : 4
long long : 8
unsigned long long : 8
What i want to know is what will be the effect if i use int in
place of long in applications running on linux and also on what factors
does the size of datatypes depend.

Thanks
Sunil.

推荐答案

一般来说,你正确的代码不应该依赖于平台上的

类型的大小。


类型的大小取决于处理器架构和

编译器供应商。例如,16位MCU migth认为int为
2字节,长为4字节。


在64位处理器上,编译器供应商可能决定使用8个字节来代表一个int。


-

EventStudio系统设计器2.5 - http://www.EventHelix.com/EventStudio

系统使用PDF和Word EMF中的序列图进行设计

In general, the code you right should not depend upon the size of a
type on a platform.

The size of a type depends upon the processor architecture and the
compiler vendor. For example, a 16 bit MCU migth consider an int to be
2 bytes and a long to be 4 bytes.

On a 64 bit processor, a compiler vendor may decide to use 8 bytes to
represent an int.

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
System Design with Sequence Diagrams in PDF and Word EMF


Sunil写道:
Sunil wrote:
大家好,

我在linux中使用gcc编译器。我编译了一个小程序
int main()
{/> printf(" char:%d \ n",sizeof(char) ));
printf(unsigned char:%d \ n,sizeof(unsigned char));
printf(short:%d \ n,sizeof(short)) ;
printf(unsigned short:%d \ n,sizeof(unsi简短));
printf(" int:%d \ n",sizeof(int));
printf(" unsigned int:%d \ n",sizeof(unsigned int) ));
printf(long:%d \ n,sizeof(long));
printf(unsigned long:%d \ n,sizeof(unsigned long)) ;
printf(long long:%d \ n,sizeof(long long));
printf(unsigned long long:%d \ n,sizeof(unsigned long
long));
}


在我使用过的某些系统上,输出会声称

所有类型的大小都是零。提示:什么类型的

值%d期待,以及
产生的大小是什么类型的价值?


你也忽略了#include< stdio.h>并且

从main()返回一个值 - 后者在

C99中没有,但在C90中没有,并且在任何情况下都是值得怀疑的。

您正在使用gcc,如果您要求它们可以产生许多有用的

诊断:请求它们。

结果是

char:1
unsigned char:1
短:2
unsigned short:2
int:4
unsigned int:4 长:4
unsigned long:4
long long:8
unsigned long long:8

我想知道的是,如果我将会有什么影响在linux上运行的应用程序中使用int,以及在数据类型的大小取决于哪些因素的情况下使用int。
Hi all,

I am using gcc compiler in linux.I compiled a small program
int main()
{
printf("char : %d\n",sizeof(char));
printf("unsigned char : %d\n",sizeof(unsigned char));
printf("short : %d\n",sizeof(short));
printf("unsigned short : %d\n",sizeof(unsigned short));
printf("int : %d\n",sizeof(int));
printf("unsigned int : %d\n",sizeof(unsigned int));
printf("long : %d\n",sizeof(long));
printf("unsigned long : %d\n",sizeof(unsigned long));
printf("long long : %d\n",sizeof(long long));
printf("unsigned long long : %d\n",sizeof(unsigned long
long));
}
On some systems I have used, the output would claim
that all the types are of size zero. Hint: What type of
value does "%d" expect, and what type of value does sizeof
produce?

You''ve also neglected to #include <stdio.h> and to
return a value from main() -- the latter is all right in
C99 but not in C90, and is doubtful practice in any case.
You''re using gcc, which can produce a lot of helpful
diagnostics if you ask for them: ask for them.
Result was

char : 1
unsigned char : 1
short : 2
unsigned short : 2
int : 4
unsigned int : 4
long : 4
unsigned long : 4
long long : 8
unsigned long long : 8

What i want to know is what will be the effect if i use int in
place of long in applications running on linux and also on what factors
does the size of datatypes depend.




在您的系统上并使用您的编译器选择

编译时选项,`long 和'廉政似乎有

相同的尺寸。这表明他们可能也有相同的范围,但是你需要显示来自

< limits.h>的实际值。完全确定。


但即使它们具有相同的大小,相同的范围和相同的表示形式,它们仍然是不同的类型。您可以通过一个小测试程序向自己证明这一点:


int func(void);

long(* fptr )(void)= func;


你可能想知道为什么一个没有区别的差异

在这种情况下确实是一个区别。答案是

当前条件下你机器上的'int''和'long''看起来相同并不意味着它们看起来一样所有

机器,甚至在你的机器上有不同的编译器标志。

如果你打算永远不把你的程序移到另一台机器,

从不要升级你当前的机器,永远不要转移到64位,而且永远不要改变编译器,那么你可以忽略`int''和`之间区分

long''(除了上面我已经显示过的

之类的情况,编译器需要比你更谨慎
)。


另一方面,如果你认为你现在的系统

及其所有软件可能不是最后一个你想要的b $ b b使用,你不应该假装'int''和'long''

同样的事情。三个有用的参考:


第十诫
http://www.lysator.liu.se/c/ten-commandments.html

FAQ问题1.1
http://www.eskimo.com/~scs /C-faq/top.html


IAQ问题1.1
http://www.plethora.net/~seebs/faqs/c-iaq.html

(一定要阅读脚注)


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子



On your system and using your compiler with your choice
of compile-time options, `long'' and `int'' appear to have the
same size. This suggests that they probably also have the
same range, but you''d need to display the actual values from
<limits.h> to be completely certain.

But even if they have the same size, the same range, and
the same representation, they remain distinct types. You can
prove this to yourself with a small test program:

int func(void);
long (*fptr)(void) = func;

You may wonder why "A difference that makes no difference"
is, in this case, truly a difference. The answer is that the
fact that `int'' and `long'' look the same on your machine under
current conditions does not mean that they look the same on all
machines, or even on your machine with different compiler flags.
If you intend never to move your program to another machine,
never to upgrade your current machine, never to move to 64-bit
Linux, and never to change compilers, then you can ignore the
distinction between `int'' and `long'' (except in situations like
that I''ve shown above, where the compiler is required to be
more scrupulous than you).

On the other hand, if you think that your current system
and all its software might not be the very last one you ever
want to use, you should not pretend that `int'' and `long'' are
the same thing. Three useful references:

The Tenth Commandment
http://www.lysator.liu.se/c/ten-commandments.html

FAQ Question 1.1
http://www.eskimo.com/~scs/C-faq/top.html

IAQ Question 1.1
http://www.plethora.net/~seebs/faqs/c-iaq.html
(be sure to read the footnotes)

--
Eric Sosman
es*****@acm-dot-org.invalid




Sunil写道:

Sunil wrote:
我想知道的是,如果我在linux上运行的应用程序中长期使用int以及在什么因素上使用int会产生什么影响
datat的大小ypes依赖。
What i want to know is what will be the effect if i use int in
place of long in applications running on linux and also on what factors
does the size of datatypes depend.




大小取决于实现。顺便提一下,大小是以char为单位占空间的单位来衡量的,这不是保证占用8位空间的,而是经常这样做。 />

在大多数情况下,确切的空间量不应该是程序员:在谈论角色时使用char,

在谈论原始内存时,unsigned char,短或未签名的短信

,当节省空间很重要时,签名或未签名的char更多

节省,长和无符号长时间处理时大整数,长整数
long和unsigned long long,当整数可能很长时,并且当你想使用最多的东西时,
int和unsigned int

`自然''实现中的整数表示(即int或

unsigned int应该是你使用的那个,除非有理由

偏离) 。如果您将它们视为整数,请使用带符号的类型,如果将它们视为位模式,则使用

无符号类型,或者需要使用

大块上的额外范围积极的结局,或者程序的逻辑

是这样的,盲目地将负数转换为大的积极的

整数是正确的事情!


C标准确保这些类型的每个最小值范围:看看那些,并决定何时需要节省空间

与你的整数在翻译时可能变得很大的时候相比,最后一段是b $ b。但是不要盲目地应用这些规则......经验

教你什么可能是最好的数据类型。 C99还可以让你对整数类型进行更精细的控制:看看它们。在罕见的

情况下,位域也可能有用。


不要无偿地放入`linux''依赖项:而不是所有linux

平台无论如何都会有完全相同的行为。即使它们是相同的大小,int和long也是不可互换的,如果你在没有改变的情况下将整数更改为long,那么程序将经常变得不正确。 />
其他任何东西。虽然这会使行为未定义,但在当前实现中,除了来自编译器的警告之外,它不可能产生

的差异。但为什么要花费

的机会?


请注意,sizeof为你提供了内存占用的空间:它可以是

一个不能有效使用所有空间的实现,所以如果你需要精确的范围,

会使用像CHAR_MAX这样的宏。它也可能不会在内存中使用相同的表示形式,因为不同类型的

相同的大小(例如,没有任何条款可以使用小端进行整理

和big endian for longs,只要编译器注意做正确的位操作;我知道没有实现那个

呢)。实现可以进一步要求两种类型的不同对齐(因此,它可能决定用于int的2字节对齐和

长度为4字节对齐:计划使用用于
整数的16位总线操作和多头的32位操作;我再次知道没有实现

这样做。


简而言之,您提出的问题应该是不应该。 C是

试图提供一个抽象级别:程序员和实现之间的分工。程序员描述了他/她想要什么,以及一些低级别的东西,比如空间或范围是否更重要,实现照顾硬件并制作

程序根据规范运行。该标准提供了

用于明确通信的语言。你的问题是

越过边界,违反了高级别的一个高级别的

langauges。


当然,那里在您需要了解您的精确硬件

以及您的实现如何将代码映射到它的情况下。你的

问题的措辞似乎暗示你不是那种情况。



The size depends on the implementation. Incidentally, the size is
measured in units of the space occupied by a char, which is not
guaranteed to take 8 bits of space, though it often does.

In most cases, the exact amount of space something takes should not
concern the programmer: use char when talking about characters,
unsigned char when talking about raw memory, short or unsigned short
when space savings is important, signed or unsigned char for even more
savings, long and unsigned long when dealing with large integers, long
long and unsigned long long when the integers may be really long, and
int and unsigned int when you want to use whatever is the most
`natural'' integer representation in the implementation (i.e. int or
unsigned int ought to be the one you use unless there is reason to
deviate). Use the signed types if you think of them as integers, use
the unsigned types if you treat them as bit patterns, or need to use
the extra range on the large positive end, or the logic of the program
is such that blindly converting negative numbers to large positive
integers is the `right thing'' to do!

The C standard does guarantee some minimum range of values for each of
these types: look at those, and decide when you want space savings
versus when your integers may become large in magnitude in interpreting
the last paragraph. But don''t apply the rules blindly ... experience
teaches you what is likely to be the best data type. C99 also lets you
more fine grained control over integral types: look at them. In rare
cases, bitfields might also be useful.

Do not gratuitously put in `linux'' dependencies: and not all linux
platforms will have the exact same behaviour anyway. Even if they are
the same size, int and long are not interchangeable, a program will
often become `incorrect'' if you change ints to longs without changing
anything else. Evn though this makes the behaviour undefined, in
practice, on current implementations, it is not likely to create a
difference except in warnings from the compiler. But why take the
chance?

Note that sizeof gives you the space occupied in memory: and it is
possible for an implementation to not effectively use all the space, so
use the macros like CHAR_MAX if you need the exact ranges. It may also
not use the same representation in memory for different types of the
same size (For example, there is no bar to using little endian for ints
and big endian for the longs, as long as the compiler takes care to do
the bit manipulations properly; no implementation I know of does that
yet). The implementation can further require different alignments for
the two types (thus, it might decide on a 2 byte alignment for ints and
4 byte alignment for longs: planning on using 16 bit bus operations for
ints and 32 bit operations for longs; again I know of no implementation
that does that).

In short, you are asking questions which you `should'' not be. C is
trying to provide a level of abstraction: a division of labour between
the programmer and the implementation. The programmer describes what
he/she wants and some low level things like whether space or range is
more important, the implementation takes care of the hardware and makes
the program run according to the specification. The standard provides
the language for unambiguous communication. Your questions are
crossing the border, violating one of the raisons de etre of high level
langauges.

Sure, there are occasions when you need to know your precise hardware
and how your implementation maps your code to it. The phrasing of your
question seems to suggest you are not in that situation, though.


这篇关于数据类型的大小取决于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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