Rory Hughes
Rory Hughes

Reputation: 46

Uninitialised Enum in C

I have the unfortunate task of bringing some code written in C into C++.

I've come across an uninitialised enum in a struct with the following form.

enum theEnum {
  A = 1,
  B = 2,
  C = 3,
}

struct theCStruct{
  enum theEnum enuminstance;
}

theCStruct structInstance;

I know that in C++ this would be undefined, but as I'm finding out uninitialised variables in C structs default to 0 (at least for ints) rather than undefined.

In this case what will the default value of the enum be in C?

Upvotes: 1

Views: 76

Answers (1)

Bathsheba
Bathsheba

Reputation: 234685

uninitialised variables in C structs default to 0 (at least for ints) rather than undefined.

That is a myth, unless the struct instance has static or _Thread_local storage duration or is at global scope.

The two languages are identical in this respect.

As a rule of thumb, don't port, but interop. C has an extremely good API on common operating systems.

Upvotes: 5

Related Questions