enum

  • Thread starter sangeeta chowdhary
  • Start date
S

sangeeta chowdhary

I am not able to understand why this code giving output 4 in gcc?
According to my knowledge,enum members are integers and they are
automatically assigned
integer values in sequence starting from zero,if we initialize any
enum variable then followed members start from initialized value plus
1.
Its means ,enum members are given memory then how come i am getting 4
always for any set of members?

#include<stdio.h>


int main()
{
enum value{val1=100,val2,val3,val4,val5,val6,val7,val8,val9,val10}
var;
printf("%d\n",sizeof(enum value));
return 0;
}
 
I

Ian Collins

I am not able to understand why this code giving output 4 in gcc?
According to my knowledge,enum members are integers and they are
automatically assigned
integer values in sequence starting from zero,if we initialize any
enum variable then followed members start from initialized value plus
1.
Its means ,enum members are given memory then how come i am getting 4
always for any set of members?

#include<stdio.h>


int main()
{
enum value{val1=100,val2,val3,val4,val5,val6,val7,val8,val9,val10}
var;
printf("%d\n",sizeof(enum value));
return 0;
}

An enum "members" are integer values, not integers. The size of an enum
is the size of an integer.
 
S

sandeep

sangeeta said:
I am not able to understand why this code giving output 4 in gcc?
According to my knowledge,enum members are integers and they are
automatically assigned
integer values in sequence starting from zero,if we initialize any enum
variable then followed members start from initialized value plus 1.
Its means ,enum members are given memory then how come i am getting 4
always for any set of members?

#include<stdio.h>


int main()
{
enum value{val1=100,val2,val3,val4,val5,val6,val7,val8,val9,val10}
var;
printf("%d\n",sizeof(enum value));
return 0;
}

Think about this code which is the same as yours (NB %zu for size_t in
printf)

#include<stdio.h>

int main()
{
enum value{val1=100,val2,val3,val4,val5,val6,val7,val8,val9,val10}
var;
enum value x; // x is an int under the hood
printf("%zu\n",sizeof(x));
return 0;
}

The sizeof function of a type, gives the size of a variable of that type,
not how many possible values the enum can take on.
 
E

Eric Sosman

The values in enum are not an array, you cannot take its size. The
enum statement generates no code and allocates no storage.

Just to clarify: The declaration as shown does in fact arrange
for storage to be reserved for the variable `var', and generates any
code that may be necessary to allocate that storage each time main()
is entered and to discard it when main() returns.

What Geoff's talking about is storage for the ten declared values
of the enum: No such storage is needed, since the values are merely
values and not objects. There's no difference between writing 'val3'
and `102': No (program-detectable) storage is set aside for either.
 
S

sangeeta chowdhary

     Just to clarify: The declaration as shown does in fact arrange
for storage to be reserved for the variable `var', and generates any
code that may be necessary to allocate that storage each time main()
is entered and to discard it when main() returns.

     What Geoff's talking about is storage for the ten declared values
of the enum: No such storage is needed, since the values are merely
values and not objects.  There's no difference between writing 'val3'
and `102': No (program-detectable) storage is set aside for either.

Ok,but if we print any member of enum var, var1,var2..... in this
case,some value will be printed.
If no memory is allocated then how these values are retrieved ?
 
N

Nick Keighley

     Just to clarify: The declaration as shown does in fact arrange
for storage to be reserved for the variable `var', and generates any
code that may be necessary to allocate that storage each time main()
is entered and to discard it when main() returns.
     What Geoff's talking about is storage for the ten declared values
of the enum: No such storage is needed, since the values are merely
values and not objects.  There's no difference between writing 'val3'
and `102': No (program-detectable) storage is set aside for either.

don't quote sigs (the bit after "-- "
Ok,but if we print any member of enum var, var1,var2..... in this
case,some value will be printed.
If no memory is allocated then how these values are retrieved ?

but there's no memory reserved if you print 102. Not in a conventional
compiled environment anyway.

Either way it compiles to something like
load d0 LIT(102)
call print

the constant takes up some room in the executable but no data storage
 
E

Eric Sosman

[... named constants of an enum ...]
What Geoff's talking about is storage for the ten declared values
of the enum: No such storage is needed, since the values are merely
values and not objects. There's no difference between writing 'val3'
and `102': No (program-detectable) storage is set aside for either.

Ok,but if we print any member of enum var, var1,var2..... in this
case,some value will be printed.
If no memory is allocated then how these values are retrieved ?

That's what I meant by "(program-detectable)". The machine has
some way to generate the value 102. We don't know how it's done, and
different machines do it differently. Many machines can incorporate
some values as "immediate operands" in the instruction stream -- if
so, they "reside in memory" but not in memory the C program can see.
Or perhaps the compiler stores a 102 in some nameless piece of memory
and emits an instruction to load from that location; again, the value
resides in memory but is not visible by the C program. Many machines
can generate the value zero (and perhaps a few other special values)
without storing them anywhere at all, by using an STZ "store zero"
instruction, or by XOR-ing a register with itself, for example. In
all cases, it's the compiler's business to cause the value to appear
when needed; if (*if*) extra memory is set aside for the purpose, any
such extra memory is invisible to the program.

Ponder this: In your enum example, the named constant `val3'
has the value 102. Therefore, the expression `val3 / 3' has the
value 34. If you write `val3 / 3' in your program, do you imagine
that the value 102 must actually be stored somewhere? Or can the
compiler precalculate the division and store a 34 somewhere instead?
The fact that `val3' is a synonym for 102 doesn't mean that a 102
resides somewhere in memory, and certainly not in memory C can reach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top