what is the size of an enum type data in C++?

前端 未结 8 558
旧巷少年郎
旧巷少年郎 2020-12-01 02:51

This is a C++ interview test question not homework.

#include 
using namespace std;
enum months_t { january, february, march, april, may, jun         


        
8条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 03:22

    I like the explanation From EdX (Microsoft: DEV210x Introduction to C++) for a similar problem:

    "The enum represents the literal values of days as integers. Referring to the numeric types table, you see that an int takes 4 bytes of memory. 7 days x 4 bytes each would require 28 bytes of memory if the entire enum were stored but the compiler only uses a single element of the enum, therefore the size in memory is actually 4 bytes."

提交回复
热议问题