473,421 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

What does "Uint32 enum {a = 100, b = 200};" mean?

Hi, all.

I see a very strange fragment code today.

Uint32 enum
{
a = 100;
b = 200;
};
NOTE: Uint32 is defined to be 'unsigned' in other source files.

I'm sure that C standard does NOT premit to define an enumeration specifier
like this. In addition, I did not find that GNU CC has such extension to the
C Language, although this code is compiled by gcc. Therefore, I'm totally
confused by this fragment code.

Could someone explain it for me? Thanks.

Best Regards,

Xiangliang Meng
Nov 14 '05 #1
9 6184
"Xiangliang Meng" <xi*************@hotmail.com> wrote:
I see a very strange fragment code today.
Strange indeed.
Uint32 enum
{
a = 100;
b = 200;
};
NOTE: Uint32 is defined to be 'unsigned' in other source files.


That is a syntax error. If there's a typedef in front of it, it defines
Uint32 as the enum in question, which would be strange in itself; since
there isn't, it simply is not valid C.

Richard
Nov 14 '05 #2
In <cd**********@zcars0v6.ca.nortel.com> "Xiangliang Meng" <xi*************@hotmail.com> writes:
I see a very strange fragment code today.

Uint32 enum
{
a = 100;
b = 200;
};
NOTE: Uint32 is defined to be 'unsigned' in other source files.

I'm sure that C standard does NOT premit to define an enumeration specifier
like this. In addition, I did not find that GNU CC has such extension to the
C Language, although this code is compiled by gcc. Therefore, I'm totally
confused by this fragment code.

Could someone explain it for me? Thanks.


As such, it is entirely useless, because it doesn't contain a tag and it
doesn't declare any object. As for the syntactic issue, let's ask the
compilers (after replacing Uint32 by unsigned int):

fangorn:~/tmp 391> gcc -ansi -pedantic test.c
test.c:1: warning: useless keyword or type name in empty declaration
fangorn:~/tmp 392> pgcc test.c
PGC-W-0114-More than one type specified (test.c: 1)
PGC-W-0150-Useless declaration (test.c: 1)
PGC/x86 Linux/x86 3.3-2: compilation completed with warnings
fangorn:~/tmp 393> icc test.c
test.c(1): error: invalid combination of type specifiers
unsigned int enum {a = 100, b = 200};
^

compilation aborted for test.c (code 2)

No compiler made any sense out of it, so how could I? ;-) Speculating:

4 Each enumerated type shall be compatible with char, a signed
integer type, or an unsigned integer type. The choice of type is
implementation-defined, but shall be capable of representing
the values of all the members of the enumeration.

So, it looks like an extension allowing the programmer to choose the
integer type himself, rather than leaving the choice up to the compiler.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3
>"Xiangliang Meng" <xi*************@hotmail.com> wrote:
Uint32 enum { a = 100; b = 200; };
NOTE: Uint32 is defined to be 'unsigned' in other source files.

In article <40****************@news.individual.net>
Richard Bos <rl*@hoekstra-uitgeverij.nl> writes:That is a syntax error. If there's a typedef in front of it, it defines
Uint32 as the enum in question ...


It is indeed a syntax error, but adding a "typedef" would not help
either, because the syntax for "typedef" is:

First, write an ordinary variable or function declarations.

int a;
char *b;
double c[2], d;

Each of these declares (and usually defines) the given identifier
as an object of the given type -- "a" has type "int", "b" has
type "char *", "c" has type "double [2]", and "d" has type
"double".

Next, stick a typedef in front:

typedef int a;
typedef char *b;
typedef double c[2], d;

(The last form is one that often surprises people.) These
change the lines from meaning "declare <name> as <object> with
type <type>" to "declare <name> as synonym for type <type>".
Hence, "a" is now a synonym for "int", "b" is now a synonym
for "char *", "c" is now a synonym for "double [2]", and "d"
is now a synonym for "double".

Now, if we do this for the "enum" above -- even after correcting
other problems like the improper semicolons -- it has the wrong
form:

/* should be: typedef <existing-type> <new-alias> */
typedef new_alias enum ...; /* wrong -- backwards! */

To give it the right form, the identifier has to come after the
"enum" sequence:

typedef enum optional_tag { a = 100, b = 200 } new_alias;

People get this backwards often, I speculate, from a perceived
(but false) symmetry with "#define":

#define DataType double /* new alias, then existing type */
typedef double DataType; /* existing type, then new alias */

Given the other syntax problems in Xiangliang Meng's original
posting, I suspect he was copying the "mystery declaration" from
(fuzzy) memory rather than the actual C source that used it.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4
"Xiangliang Meng" <xi*************@hotmail.com> writes:
I see a very strange fragment code today.

Uint32 enum
{
a = 100;
b = 200;
};
NOTE: Uint32 is defined to be 'unsigned' in other source files.

I'm sure that C standard does NOT premit to define an enumeration specifier
like this. In addition, I did not find that GNU CC has such extension to the
C Language, although this code is compiled by gcc. Therefore, I'm totally
confused by this fragment code.

Could someone explain it for me? Thanks.


No, gcc doesn't accept the above code. The semicolons are incorrect;
they should be commas (the second one is optional).

If you're going to post a code sample, please don't try to re-type it;
cut-and-paste the *exact* code that you've compiled. Otherwise, we
can't tell which errors are typos and which are relevant to your
question.

If I compile the following with gcc 3.3.3:

typedef unsigned Uint32;
Uint32 enum
{
a = 100,
b = 200
};

I get:

tmp.c:6: warning: useless keyword or type name in empty declaration

(I'm not sure why it's just a warning rather than an error message,
but the standard doesn't distinguish between different kinds of
diagnostics. I get the same warning with "int int;".)

If I change it to:

typedef unsigned Uint32;
Uint32 enum
{
a = 100,
b = 200
} obj;

I get:

tmp.c:6: error: two or more data types in declaration of `obj'

It looks like an attempt to create an enumeration type with a specific
representation, but it's not something that gcc supports.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
"Xiangliang Meng" <xi*************@hotmail.com> writes:
I see a very strange fragment code today.

Uint32 enum
{
a = 100;
b = 200;
};
NOTE: Uint32 is defined to be 'unsigned' in other source files.

I'm sure that C standard does NOT premit to define an enumeration specifier
like this. In addition, I did not find that GNU CC has such extension to the
C Language, although this code is compiled by gcc. Therefore, I'm totally
confused by this fragment code.

Could someone explain it for me? Thanks.


No, gcc doesn't accept the above code. The semicolons are incorrect;
they should be commas (the second one is optional).


Note that he got it right in the subject line.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
Hi, all

Several points to address:

1. I could not copy and paste the original codes, due to company's policy.
";" should be "," in the content.

2. I consult an super expert on C++ in my company on this fragement code.
His opinion is:
" The original developer was hoping to control the underlying integral type
used to represent the enum. There is no official way to do this in the C/C++
standard. This tends to be a problem in C++ designs that overload methods
based on the signedness of integral types. This is considered poor design.

The 99r1 C compiler warns about this fragment. The 99r1 C++ compiler quietly
accepts the fragment (which I believe is a bug).

The gcc-3.3.1 C compiler warns about this fragment. The gcc-3.3.1 C++
compiler generates an error for this fragment."

3. I sent an email to one of their creators, but he could not remember why
he did that. Those codes were created ten years ago.

4. How those enumeration constant are used?
They defined some constant configuration parameters as these enumeration
constants. So they did not use the enum type to define any local variable,
but took them as constant and used them directly.

Thank all of you very much.

Best Regards,

Xiangliang Meng
Nov 14 '05 #7
Xiangliang Meng wrote:

2. I consult an super expert on C++ in my company on this fragement code.
His opinion is:
" The original developer was hoping to control the underlying integral type
used to represent the enum. There is no official way to do this in the C/C++
standard.


He is not even a moderate literate in C or C++, much less a "super
expert." A "super expert" knows that not only are C and C++ different
languages, they have completely different standards. There is no such
thing as "C/C++"; there is certainly no such thing as "the C/C++
standard". Rather, there are C and C++ standards. Don't believe
anything that comes out of his mouth.
Nov 14 '05 #8
Hi, Martin.

I think he just wanted to say "in the C standard or in the C++ standard".

Best Regards,

Xiangliang Meng

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2m************@uni-berlin.de...
Xiangliang Meng wrote:

2. I consult an super expert on C++ in my company on this fragement code. His opinion is:
" The original developer was hoping to control the underlying integral type used to represent the enum. There is no official way to do this in the C/C++ standard.


He is not even a moderate literate in C or C++, much less a "super
expert." A "super expert" knows that not only are C and C++ different
languages, they have completely different standards. There is no such
thing as "C/C++"; there is certainly no such thing as "the C/C++
standard". Rather, there are C and C++ standards. Don't believe
anything that comes out of his mouth.

Nov 14 '05 #9
Martin Ambuhl <ma*****@earthlink.net> writes:
Xiangliang Meng wrote:
2. I consult an super expert on C++ in my company on this fragement code.
His opinion is:
" The original developer was hoping to control the underlying
integral type used to represent the enum. There is no official way
to do this in the C/C++ standard.


He is not even a moderate literate in C or C++, much less a "super
expert." A "super expert" knows that not only are C and C++ different
languages, they have completely different standards. There is no such
thing as "C/C++"; there is certainly no such thing as "the C/C++
standard". Rather, there are C and C++ standards. Don't believe
anything that comes out of his mouth.


That may a little harsh. In the jargon of this newsgroup, "C/C++"
refers to a mythical language with some undefined relationship to C
and/or C++, with the side effect of marking the person using the term
as ignorant. Out in the real world, "the C/C++ standard" could
conceivably mean "the C standard or the C++ standard".

It's clumsy and incorrect, but it may be just informal rather than
incurably ignorant.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

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

Similar topics

4
by: David | last post by:
Hi, Buddy, a newbie's question for you guys, In C++, some functions have a return value type "result", what does this mean, I searched on web, but no hint. thanks a lot David
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
1
by: Frank Perry | last post by:
I have a utility that monitors whether certain third party GU applications are running and if not, it starts them up. I would lik to get rid of this utility and instead just have a Windows Service...
6
by: tingjun.li | last post by:
There is a demo program: ------------------------------------------------- const char* WS = " \t\n"; const int n_WS = strlen(WS); char* s1 = "This sentence contains five words."; char* s2 =...
1
by: new2php | last post by:
Hi,I'm new to PHP...I currently doing a PHP project....I don't understand this code..can someone please kindly explain to me..Thanks select * from category c left join category2 c2 on...
3
by: Jurgen Haan | last post by:
Hi hi. I'd like to gain some knowledge on packages. We're getting an error "SQL0805N Package "NULLID.SYSLH214 0X5359534C564C3031" was not found. SQLSTATE=51002" On our production database....
3
by: Alexander Eisenhuth | last post by:
Hello, Ctrl+C is not passed to the interpreter (i guess it) while I'm executing a script. Instead i get: forrtl: error (200): program aborting due to control-C event If I start python in...
3
by: Mike Hamilton | last post by:
Hi newsgroup, I have a C# code that I tried to convert to VB.NET using a snippet converter. The converter produced something I don't understand, namely: <Config("Camera", "cameraType")_...
3
by: phpuser123 | last post by:
<html> <head> <script type="text/javascript" src=ajax.js></script> </script> <style type='text/css'> </style> </head> <body...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
1
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.