CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

typedef enum

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
joseperri



Joined: 24 May 2019
Posts: 5

View user's profile Send private message AIM Address

typedef enum
PostPosted: Fri May 24, 2019 7:26 pm     Reply with quote

hello, the following code should not generate a compile warning? since the assigned value is not within the range of the enum (100>ERROR_EXISTENTE)
My compiler is 5.076.
Thank you
José
Code:

 typedef enum {
    OK_GRABADO,
    OK_BORRADO,
    ERROR_EXP_TPO,
    ERROR_USER_CANCEL,
    ERROR_MAX_CANT,
    ERROR_EXISTENTE,
} var_t;

var_t i;


void main(void){
    i=100;
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19529

View user's profile Send private message

PostPosted: Fri May 24, 2019 10:42 pm     Reply with quote

No. In C, there is no testing of number validity. You can perfectly well
put a million into an int16. All that happens is that the part of the number
that fits is stored.
joseperri



Joined: 24 May 2019
Posts: 5

View user's profile Send private message AIM Address

PostPosted: Sat May 25, 2019 5:58 pm     Reply with quote

Thank you very much for the prompt response, if you allow me I ask you another question in the same line. The following code defines two types of data: colors_t and shapes_t, if I make an erroneous assignment using eg a variable myShape generates a warning which warns against an error but if the erroneous assignment is made within the object structure the compiler does not generate no warning The only object for which I want to work in this way is to avoid errors of assigning values to variables that are usually within structures.
Again, thanks for any advice or support
José
Code:

typedef enum{
    RED,
    BLUE,
} colors_t;

typedef enum {
    CIRCLE,
    SQUARE,
    RECTANGLE,
    TRIANGLE,
} shapes_t;

struct{
    colors_t color;
    shapes_t shape;
}object;

shapes_t myShape;
colors_t myColor;


void main(void){
    object.color=RED;
    object.shape=CIRCLE;
    object.shape=RED;//SHOULD NOT GENERATE A WARNING?
   
    myColor=RED;
    myShape=CIRCLE;
    myShape=RED;//GENERATE A WARNING ENUM IS NOT A CORRECT TYPE
   
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19529

View user's profile Send private message

PostPosted: Sat May 25, 2019 11:42 pm     Reply with quote

No. Neither will warn in any way.

Understand that an enum, is not a type.
Your color enum, is basically equivalent to just:

#define RED 1
#define BLUE 2

shapes_t, and colors_t, are just int's.

myShape=RED;

Is just saying put the integer '1' into the int variable. Not illegal in any way.

C requires _you_ to check your values and types....

Use a LINT if you want stricter checking. LINT was a Unix program written
many years ago, to catch 'bits of fluff' in C programming. So to look for
things like this that the compiler will not spot, but which might be an
issue. There are quite a few implementations around.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group