🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fr.comp.lang.c
Article #15674

Re: Syntaxe enum

#15674
From: Eric Levenez
Date: Mon, 05 Sep 2005 16:54
79 lines
2626 bytes
Le 5/09/05 18:34, dans <431c738f$0$11084$626a14ce@news.free.fr>, ��David
Noyelle�� <widax@free.fr> a �crit�:

> je cherche la syntaxe pour mettre en place un enum avec des valeurs int
> allant de '-1' a '-14'. Cette valeur est en fait le code de retour d'une
> fonction, et ca me semble un peu long avec un switch...
> 
> par ex:
> 
> enum TYPE_ERREUR_1 {-1, -2, -3, -4, -5, -6, -7};
> enum TYPE_ERREUR_2 {-1, -2, -3, -4, -5, -6, -7};
> ................
> 
> int_retour = maFonction();
> 
> switch(int_retour)
> {
>   case TYPE_ERREUR_1:
>     ................
>     break;
>   case TYPE_ERREUR_2:
>     ................
>     break;
> }

Voici un exemple (les constantes de l'enum sont auto-incr�ment�es de 1 en
1):

#include <stdio.h>

enum type_erreur {
        TYPE_ERREUR_14 = -14,
        TYPE_ERREUR_13,
        TYPE_ERREUR_12,
        TYPE_ERREUR_11,
        TYPE_ERREUR_10,
        TYPE_ERREUR_9,
        TYPE_ERREUR_8,
        TYPE_ERREUR_7,
        TYPE_ERREUR_6,
        TYPE_ERREUR_5,
        TYPE_ERREUR_4,
        TYPE_ERREUR_3,
        TYPE_ERREUR_2,
        TYPE_ERREUR_1
};

enum type_erreur fct(void)
{
        return TYPE_ERREUR_2;   // -2
}

int main(void)
{
        enum type_erreur ret;

        ret = fct();

        switch (ret) {
        case TYPE_ERREUR_1 :
                (void)printf("Traitement -1\n");
                break;

        case TYPE_ERREUR_2 :
                (void)printf("Traitement -2\n");
                break;

        // ...

        default :
                (void)printf("Autre traitement %d\n", (int)ret);
        }
        return 0;
}


-- 
�ric L�v�nez -- <http://www.levenez.com/>
Unix is not only an OS, it's a way of life.
Technical Details
Message-ID: <BF4244E3.4461C%new@levenez.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!proxad.net!infeed-2.proxad.net!news14-e.free.fr!not-for-mail
References: <431c738f$0$11084$626a14ce@news.free.fr>

Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,4e37a5742151792c
X-Google-Attributes: gidf9822,public
X-Google-Language: FRENCH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!proxad.net!infeed-2.proxad.net!news14-e.free.fr!not-for-mail
User-Agent: Microsoft-Entourage/11.1.0.040913
Date: Mon, 05 Sep 2005 18:54:27 +0200
Subject: Re: Syntaxe enum
From: Eric Levenez <new@levenez.com>
Newsgroups: fr.comp.lang.c
Message-ID: <BF4244E3.4461C%new@levenez.com>
References: <431c738f$0$11084$626a14ce@news.free.fr>
Mime-version: 1.0
Content-type: text/plain;
 charset="ISO-8859-1"
Content-transfer-encoding: 8bit
Lines: 80
Organization: Guest of ProXad - France
NNTP-Posting-Date: 05 Sep 2005 18:54:27 MEST
NNTP-Posting-Host: 82.244.40.208
X-Trace: 1125939267 news14-e.free.fr 19732 82.244.40.208:50418
X-Complaints-To: abuse@proxad.net