473,769 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum type int or unsigned int?

Hi all,

Can the compiler chose the type of an enum to be signed or unsigned
int? I thought it must be int; looks like it changes based on the
assigned values. Below if I don't initialize FOO_STORE to be, say -10,
I get a warning about unsigned comparison and I'm seeing an infinite
loop.

If I do initialize FOO_STORE = -10, I don't see any warnings. No
infinite loop.

Isn't the type of FOO_xyz required to be an 'int'?
Can the compiler choose its type to be unsigned int?

--------
#include <stdio.h>

typedef enum {
FOO_STORE , /* if = -10 is added, no warnings. works fine */
FOO_HASHTABLE,
FOO_STATE,
FOO_CONFIG,
FOO_MAX
} foo_subobj_type _en;
int main(void) {
foo_subobj_type _en sub_obj;

for (sub_obj = FOO_MAX-1; sub_obj >= FOO_STORE; --sub_obj) {
printf("%d\n", sub_obj);
}
return 0;
}
gcc --version gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-52)
gcc -Wall -W -ansi -pedantic enum.c enum.c: In function `main':
enum.c:15: warning: comparison of unsigned expression >= 0 is always
true


Thanks,
Karthik

Apr 13 '06
10 23610
CBFalconer schrieb:
Chris Torek wrote:

... snip ...
The absolute guaranteed method is a "test at bottom" style loop,
for which C lacks a convenient construct. If it had one, it might
read:

for_until_test_ at_bottom (sub_obj = FOO_MAX - 1;
sub_obj == FOO_STORE; --sub_obj) {
...
}


I strongly disagree. That is the purpose of the do loop:

do {
/* whatever */
} while (appropriate);


I am not sure whether you understood Chris Torek's point;
sub_obj = InitValue + 1;
do {
--sub_obj;
....
} while (!(sub_obj == FOO_STORE));
is not exactly convenient; if the starting point (InitValue)
were FOO_MAX instead of FOO_MAX - 1, and FOO_MAX coincided with
sub_obj's type maximum, then the addition of one would not be
possible and you'd need
sub_obj = InitValue;
first = 1;
do {
if (!first)
--sub_obj;
else
first = 0;
....
} while (!(sub_obj == FOO_STORE));
which is not exactly convenient. Of course, you still can express
everything with every kind of loop (plus some stuff). In this
case, tricks with the for loop continuation condition may lead to
cleaner code than the do--while variant. ("first || !(sub_obj ==
FOO_STORE)")
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
May 2 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1707
by: Kaila | last post by:
I'm using metroworks codewarrior and can't solve a redeclariotin of type problem I have several classes and each of them need to have a custom type called enum X now what I was thinking is to put the type in a seperate file and simply attach it as a librabry to my main as follows: In main.cpp
2
4294
by: Voronkov Konstantin | last post by:
Thank you for answer, but I still did not got *how* to make serialization of enum type. Can you provide more instructions or hint, please? My task is to serialize enum to something like byte array, and then load enum from that byte array to the same enum type on other platform. One way to do I see is:
3
2698
by: Jerry | last post by:
Hi, I found an enum type, for example: enum Alignment { Top, Bottom } is derived Enum, but Enum is a struct. However,
2
1819
by: tzvika.visman | last post by:
I write a MC++ wrapper for our company internal SDK that wrote in C++ native code for writing application with this SDK over C# and other .NET languages and most of my SDK API function return a status code that define as a enum type. Do I have other option then duplicate the enum from the c++ to the C# code and write a MC++ class that look something like this: public __gc class ManagedDokStatus {
13
12392
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
2
1872
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product Public Enum Status psNormal psCharged End Enum
7
9837
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
2
2484
by: NullQwerty | last post by:
Hey folks, So, I've got three enum types: enum enum1 enum enum2 enum enum3 And then I've got a function overloaded three times to accept each enum type: private void func(enum1 myenum){}
10
3791
by: yu_kuo | last post by:
I got series warning when using write enum type to a ostream using operator<<, like in call to `std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) ' A.cpp:60: warning: passing `NAME_ENUM' chooses `int' over `long int' Compiler gcc 3.4.3 When deserialize from an istream, I have to first using an int, and
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.