why it's not ar error....

A

asit

i compiled it in GCC compiler...but it is not showing any error.
(enumerated variables should hold only enumerated constant). please
help.. !!!
#include <stdio.h>

int main()
{
enum day {mon=1000, tue, wed, thurs, fri, sat, sun };
printf("mon : %d\n",mon);
enum day day1;
day1=wed;
printf("day1 : %u----%d\n",&day1,day1);
day1=100;
printf("day1 : %u----%d\n",&day1,day1);
return 0;
}


thanx
 
H

Harald van Dijk

i compiled it in GCC compiler...but it is not showing any error.
(enumerated variables should hold only enumerated constant). please
help.. !!!

Enumeration types are simple integer types in C. They may hold any value
within the enum's range, so when you say that 1000 is a valid value, 100
must also be a valid value. If you want to check that an integer is
between 1000 and 1006 (inclusive), you'd have to write the check
yourself. If you want to check that an enum day is between mon and sun,
you'll have to write the check yourself too.
#include <stdio.h>

int main()
{
enum day {mon=1000, tue, wed, thurs, fri, sat, sun };
printf("mon : %d\n",mon);
enum day day1;
day1=wed;
printf("day1 : %u----%d\n",&day1,day1);
day1=100;
printf("day1 : %u----%d\n",&day1,day1);
return 0;
}

However, if you're using GCC, and you didn't get complaints about this
code, you should increase the warning level. You're telling printf you
want to print an unsigned int, and you pass it a pointer. This is bogus,
and GCC will warn about that if you ask it to. Since you didn't enable
warnings, you really shouldn't be asking why some code is accepted in the
first place. Even if it were invalid, if you don't let your compiler help
you, it simply won't help you.
 
C

CBFalconer

asit said:
i compiled it in GCC compiler...but it is not showing any error.
(enumerated variables should hold only enumerated constant).

Not so. Any value of the 'integer type' selected is valid.
From section 6.7.2.2:

[#4] Each enumerated type shall be compatible with an
integer type. The choice of type is
implementation-defined,99) but shall be capable of
representing the values of all the members of the
enumeration. The enumerated type is incomplete until after
the } that terminates the list of enumerator declarations.
 
H

Harald van Dijk

Please direct replies to the group whenever possible. Thanks.

i compiled it in GCC compiler...but it is not showing any error.
[...]
printf("day1 : %u----%d\n",&day1,day1);

However, if you're using GCC, and you didn't get complaints about this
code, you should increase the warning level. You're telling printf you
want to print an unsigned int, and you pass it a pointer. This is bogus,
and GCC will warn about that if you ask it to. Since you didn't enable
warnings, you really shouldn't be asking why some code is accepted in the
first place. Even if it were invalid, if you don't let your compiler help
you, it simply won't help you.

Can u please tell me how to enable such type of warning in gcc ??

The minimal options to get it to act as a standard C compiler are
-ansi and -pedantic. Additional warnings can be enabled with the -W*
options. To get a suitable starting set of warnings, which would also
warn about this specific example, you can use -Wall.
Pointer content is an address which is an unsigned integer. Am i
right ??

No. A pointer is a pointer. A pointer must contain an address, but some
implementations make it contain more than that. And even on
implementations where a pointer contains nothing more than an address,
the address may be smaller or larger than unsigned int.
what format specifier you will suggest me to print address ??

The documentation for printf and fprintf should tell you that you can
print pointers by passing a void * pointer, and using the %p format
specifier.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top