1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <stdio.h>
#include <stdarg.h>
typedef enum {EstInt, EstDouble, EstString , EstPFloat, EstAutre} Type;
void maFonction(Type format, ...)
{
va_list ap;
int arg;
va_start(ap, format);
switch(format)
{
case EstInt :
{
int arg = va_arg(ap, int);
printf("type int %d\n",arg);
break;
}
case EstDouble :
{
double arg = va_arg(ap, double);
printf("type double %f\n",arg);
break;
}
case EstString :
{
void* arg = va_arg(ap, void*);
printf("type string %s\n",arg);
break;
}
case EstPFloat :
{
void* arg = va_arg(ap, void*);
printf("type float %f\n",*(float*)arg);
break;
}
default :
printf("type inconnu\n",arg);
}
va_end(ap);
}
int main(void)
{
float f = 0.9876f;
maFonction(EstInt, 12345);
maFonction(EstDouble, 1.2345);
maFonction(EstString,"abcde");
maFonction(EstPFloat,&f);
return 0;
} |
Partager