后面的sizeof()字符常量和函数名逻辑 [英] Logic behind sizeof() for character constants and function names

查看:105
本文介绍了后面的sizeof()字符常量和函数名逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 的以下code:

#include<stdio.h>
int main()
{
   char c='a';
   printf("%d %d",sizeof(c),sizeof('a'));
   return 0;
}

产生的结果 1 4 ?请解释什么逻辑?

此外,为什么的sizeof(主())结果 4 ,但的sizeof (主)的结果 1

Also, why does sizeof(main()) result in 4 but sizeof(main) results in 1:

#include<stdio.h>

int main()
{

   printf("%d %d\n",sizeof(main), sizeof(main()));
   return 0;
}

和中的 C ++ 的为什么的sizeof('A')导致1,但的sizeof(AV)的结果在4?

And in C++ why does sizeof('a') result in 1 but sizeof('av') results in 4?

推荐答案

字符常量的中的 C 的有型的 INT 的,虽然这是不是的 C ++的情况下的。从草案C99标准部分 6.4.4.4 字符常量的段落的 10 的说(的重点煤矿的):

Character constants in C are of type int, although this is not the case in C++. From the draft C99 standard section 6.4.4.4 Character constants paragraph 10 says (emphasis mine):

这是整型字符常量的 int类型即可。整型字符常量的值
  包含映射到单字节执行字符一个字符是
  PTED为整数的映射字符间$ P $重新presentation的数值。
  包含多个字符的整型字符常量的值(例如,
  'AB')[...]

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab')[...]

草案C ++标准部分 2.14.3 字符文字的段落的 1 的说(的重点煤矿的):

from the draft C++ standard section 2.14.3 Character literals paragraph 1 says (emphasis mine):

[...]一个普通字符文字包含在设置执行字符单个c字符重新presentable 具有char类型,[.. ]字面一个普通字符的包含不止一个C-char是一个多字符文字。一个多字符
  文字或一个普通的字符常量,其中包含一个单一的C-字符不会再在执行字符集presentable,有条件地支持, int类型,并具有实现定义的值。

[...]An ordinary character literal that contains a single c-char representable in the execution character set has type char,[...] An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal, or an ordinary character literal containing a single c-char not representable in the execution character set, is conditionally-supported, has type int, and has an implementation-defined value.

所以 AV 多字符文字的,将有 INT 的大小。

So av is a multicharacter literal and will have size of int.

有关问题的第二部分,的sizeof(主)不是有效的code,但编译器可能会选择仍然产生的结果将是实现定义的,从C99标准草案 6.5.3.4 sizeof操作符的段落的 1 的说

For the second part of the question, sizeof(main) is not valid code, although compiler may choose to still produce a result it will be implementation defined, from the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 1 says:

sizeof运算符不得应用于具有函数类型或一个前pression
  不完全型,[...]

The sizeof operator shall not be applied to an expression that has function type or an incomplete type, [...]

该草案C ++标准也有类似的措辞和两个 GCC 使用时警告此code在 -pedantic 标志,像这样的错误:

the draft C++ standard has similar wording and both gcc and clang warn about this code when using the -pedantic flag, with an error like this:

警告:'sizeof的无效应用函数类型[-pedantic]

warning: invalid application of 'sizeof' to a function type [-pedantic]

的sizeof(主()),因为的sizeof 是一个编译时操作人员和不评估它的参数,除了在可变长度数组的情况下,结果是返回类型的尺寸,其是<青霉> INT 在这种情况下。例如,我们可以看到活生生的例子

for sizeof(main()) since sizeof is a compile time operator and does not evaluate it's arguments except in the case of variable length arrays the result is the size of the return type which is int in this case. For example we can see live example:

long double func()
{
    return 1.0 ;
}

这是的sizeof(FUNC())收益 16

注意

在你的平台的sizeof(INT) 4 但体型的实现定义

On your platform sizeof(int) is 4 but the size is implementation defined.

注2

由于中的sizeof 结果的为size_t 的一个更便携的格式说明符的printf %祖

Since the result of sizeof is size_t a more portable format specifier for printf would be %zu.

这篇关于后面的sizeof()字符常量和函数名逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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