enums and switch vs function calls

Liste des GroupesRevenir à cl c 
Sujet : enums and switch vs function calls
De : richard.nospam (at) *nospam* gmail.invalid (Richard Harnden)
Groupes : comp.lang.c
Date : 05. Apr 2025, 11:29:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vsr0ml$1to9m$1@dont-email.me>
User-Agent : Mozilla Thunderbird
If I have:
enum colour {RED, GREEN, BLUE}
int colour_to_hex(enum colour colour)
{
     switch (colour)
     {
         RED: return 0xff0000;
         GREEN: return 0x00ff00;
         // BLUE: return 0x0000ff;
     }
     ...
}
... then the compiler will warn me that I've missed a case in the switch statement.  Which is good and very helpful.
But if I do:
int hex = colour_to_hex(10);
... then the compiler doesn't complain that 10 is not in the enum.
Why? Surely the compiler can tell.

Date Sujet#  Auteur
5 Apr 25 * enums and switch vs function calls12Richard Harnden
5 Apr 25 +* Re: enums and switch vs function calls9Stefan Ram
5 Apr 25 i+- Re: enums and switch vs function calls1David Brown
5 Apr 25 i+- Re: enums and switch vs function calls1Stefan Ram
6 Apr 25 i`* Re: enums and switch vs function calls6Tim Rentsch
6 Apr 25 i +- Re: enums and switch vs function calls1David Brown
7 Apr 25 i +- Re: enums and switch vs function calls1Tim Rentsch
25 Apr 25 i `* Re: enums and switch vs function calls3Ben Bacarisse
25 Apr 25 i  `* Re: enums and switch vs function calls2Ben Bacarisse
4 May 25 i   `- Re: enums and switch vs function calls1Tim Rentsch
5 Apr 25 `* Re: enums and switch vs function calls2Thiago Adams
6 Apr 25  `- Re: enums and switch vs function calls1David Brown

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal