簡體   English   中英

有人可以解釋為什么 sizeof() 使用一元運算符返回這些值嗎?

[英]Can someone explain why sizeof() returns these values with unary operators?

#include <stdio.h>

int main() {
    int a;
    char b;
    short int c;
    double d;
    printf("%d %d %d %d\n", sizeof(a), sizeof(b), sizeof(c), sizeof(d));
    printf("%d %d %d %d\n", sizeof(+a), sizeof(+b), sizeof(+c), sizeof(+d));
    printf("%d %d %d %d\n", sizeof(-a), sizeof(-b), sizeof(-c), sizeof(-d));
    return 0;
}

32位編譯器output:

4 1 2 8
4 4 4 8
4 4 4 8

如果我更改sizeof()內的符號,則 output 是相同的,例如sizeof(-a) 我想知道為什么會這樣。 +-運算符是否促進數據類型?

是的,一元+-運算符 integer 將小型 integer 類型提升為int C17 6.5.3.3:

一元 + 運算符的結果是其(提升的)操作數的值。 對操作數執行 integer 提升,結果具有提升類型。

一元 - 運算符的結果是其(提升的)操作數的負數。 對操作數執行 integer 提升,結果具有提升類型。

關於 integer 提升的詳細信息,請參見隱式類型提升規則


您可以使用_Generic來找出任何表達式的實際類型:

#include<stdio.h>

#define type(x) _Generic((x),    \
  int:   puts(#x " is int"),     \
  short: puts(#x " is short"),   \
  char:  puts(#x " is char") );  \

int main (void)
{
  int a;
  char b;
  short int c;
  
  type(a); type(+a);
  type(b); type(+b);
  type(c); type(+c);
}

Output:

a is int
+a is int
b is char
+b is int
c is short
+c is int

就是這樣。 進行計算時(例如 + 或 -),較短的 integer 格式被提升為基本大小,即 int,長度為 4 個字節。

當在簡單算術表達式中用作 arguments 時,小整數類型將提升為intunsigned int 因此sizeof(b)1 ,即char的大小,但sizeof(+b)4 ,即平台上int的大小。

但請注意, printf需要一個用於%d轉換規范的int參數。 sizeof()具有size_t類型,這是一種可能與unsigned int不同的無符號類型。 您應該使用%zu或將sizeof()表達式轉換為(int)

這是一個示例程序用於說明:

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#define typeof(X)  _Generic((X),                                        \
                   long double: "long double",                          \
                   double: "double",                                    \
                   float: "float",                                      \
                   unsigned long long int: "unsigned long long int",    \
                   long long int: "long long int",                      \
                   unsigned long int: "unsigned long int",              \
                   long int: "long int",                                \
                   unsigned int: "unsigned int",                        \
                   int: "int",                                          \
                   unsigned short: "unsigned short",                    \
                   short: "short",                                      \
                   unsigned char: "unsigned char",                      \
                   signed char: "signed char",                          \
                   char: "char",                                        \
                   bool: "bool",                                        \
                   __int128_t: "__int128_t",                            \
                   __uint128_t: "__uint128_t",                          \
                   default: "other")

int main() {
    int a;
    char b;
    short int c;

#define TEST(x)  printf("%8s has type %s and size %zu\n", #x, typeof(x), sizeof(x))
    TEST(a);
    TEST(+a);
    TEST(b);
    TEST(+b);
    TEST(c);
    TEST(+c);
    TEST('0');
    TEST(*"");
    TEST(0);
    TEST(0L);
    TEST(0LL);
    TEST(0.F);
    TEST(0.);
    TEST(0.L);
    TEST(sizeof(a));

    return 0;
}

Output 在 64 位 OS/X 上:

       a has type int and size 4
      +a has type int and size 4
       b has type char and size 1
      +b has type int and size 4
       c has type short and size 2
      +c has type int and size 4
     '0' has type int and size 4
     *"" has type char and size 1
       0 has type int and size 4
      0L has type long int and size 8
     0LL has type long long int and size 8
     0.F has type float and size 4
      0. has type double and size 8
     0.L has type long double and size 16
sizeof(a) has type unsigned long int and size 8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM