Search code examples
cenumsdeclarationcompiler-warnings

warning for initialized enum variable : type defaults to 'int' in declaration of 'etatCourant' [-Wimplicit-int]


I'm having a warning when I tried to initialize my enum variable. Here is the code :

enum etatCourant {REPOS, MARCHE_AV, MARCHE_AR, ERREUR};
etatCourant = REPOS; 

Here is the warning : type defaults to 'int' in declaration of 'etatCourant' [-Wimplicit-int]

Am I missing something?

The code works as expected but I just don't like warnings. I try to avoid them as much as possible


Solution

  • You are trying to declare a variable with the name that coincides with the enumeration tag name without specifying a type specifier in the declaration of the variable

    enum etatCourant {REPOS, MARCHE_AV, MARCHE_AR, ERREUR};
    etatCourant = REPOS; 
    ^^^^^^^^^^^
    

    So according to the compiler message it supposes that by default it will have the type int.

    If you want to declare a variable of the enumeration type you need to write for example

    enum etatCourant {REPOS, MARCHE_AV, MARCHE_AR, ERREUR};
    enum etatCourant courant = REPOS; 
    

    In this case the variable courant will have the enumeration type enum etatCourant.

    Or the declaration may be rewritten also like

    enum etatCourant etatCourant = REPOS; 
    

    though such a declaration can confuse readers of your code.

    The code works as expected but I just don't like warnings. I try to avoid them as much as possible

    The code works as expected because in C enumerations belong to integer types and enumerators have the type int.