English 中文(简体)
C/C++ 操作员大小: 为什么( a) 大小会返回不同的值? [重复]
原标题:C/C++ sizeof operator: Why does sizeof( a ) return different values? [duplicate]
  • 时间:2012-05-23 03:46:12
  •  标签:
  • c++
  • c
  • memory
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Size of character ( a ) in C/C++

我是C的初学者 被这个弄糊涂了

C: 我尝试使用 “%zu” 修饰符打印 C 的大小( a), 并打印了值 4 。

C+++:打印大小(a),在C++中使用 cout,和印刷(使用上述格式),都打印了一个值 1。

I believe the correct value should be 1, since a will be taken as a char. Why doesn t it return 4 in C? Are the sizeof operations of both different in both the languages? If so, what s the difference, and why does it return a different value? I used the gcc compilers in both cases.

最佳回答

在 C 中, < code> a 是字符常数, 被作为整数处理, 大小为 4, 在 C++ 中, 它被作为 < code>char 处理。 这是问题在此的重复 :

< a href=" "https://stackoverflow.com/ questions/2172943/ size-of-criacter-a-in-c-c" C/C/a> 字符大小(a)

问题回答

In C a character literal (constant) has type int. So, consider the following program

#include <stdio.h>

main(int argc, char *argv[])
{
  printf("%zu
", sizeof( a ));
  printf("%zu
", sizeof( ab ));
  printf("%zu
", sizeof( abc ));
  printf("%zu
", sizeof( abcd ));

  printf("%u
",  a );
  printf("%u
",  ab );
  printf("%u
",  abc );
  printf("%u
",  abcd );

  printf("%x
",  a );
  printf("%x
",  ab );
  printf("%x
",  abc );
  printf("%x
",  abcd );

  printf("%c
",  a );
  printf("%c
",  ab );
  printf("%c
",  abc );
  printf("%c
",  abcd );
}

The first four statements all consider the literals as one character constant and they all print 4 == sizef(int), at least on gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3. Note that this compiler prints several warnings for the above program:

warning: multi-character character constant

Basically, a character literal specifies the four bytes making up an int, from left to right, higher-order byte first. The missing leading bytes are filled with 0. So, on my machine the second and third group of printf statements print

97
24930
6382179
1633837924
61
6162
616263
61626364

In the hexadecimal output you see the layout of the four characters in the literal (the ASCII codes from left to right): the a is mapped to the highest-order byte 0x61).

最后,第四组指纹:

a
b
c
d

i.e. the character literals are pushed on the stack as integers, but printf only prints the lowest byte of that int as a char.

C++ behaves in a similar way, but one-byte character literals are considered of type char, not int. The program

#include <iostream>

using namespace std;

main(int argc, char *argv[])
{
  cout << sizeof( a ) << endl;
  cout << sizeof( ab ) << endl;
  cout << sizeof( abc ) << endl;
  cout << sizeof( abcd ) << endl;

  cout <<  a  << endl;
  cout <<  ab  << endl;
  cout <<  abc  << endl;
  cout <<  abcd  << endl;
}

will compile using GCC and give a similar warning. Its output is different from that of C:

1
4
4
4
a
24930
6382179
1633837924

So one-byte character literals are treated as char, while multi-byte literals are treated as int.

<强度 > 说明

I ran my tests on a 32-bit Linux system on which an int has 4 bytes. It would be interesting to see what happens on other systems, e.g. on a 64-bit system.

<强 > EDIT

Fixed answer (thanks for the hint): character literals have type int in C, they are not cast to int.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签