帮我用c语言写一段代码:函数名为void print_Data;形式参数为Data d;要求Print the content of a Data;ifferent types of data are printed in some proper way according to its different typeid.;并且已经知道typedef enum typeID { ANY, CHAR, SHORT, INT, LONG, USHORT, UINT, ULONG, FLOAT, DOUBLE, ADDR, STR, CHARS } TypeID
时间: 2023-02-06 13:20:25 浏览: 139
C语言中数据类型
这是一段 C 语言代码,用于打印出数据结构 Data 的内容。
```
#include <stdio.h>
typedef enum typeID { ANY, CHAR, SHORT, INT, LONG, USHORT, UINT, ULONG, FLOAT, DOUBLE, ADDR, STR, CHARS } TypeID;
typedef struct Data {
TypeID type;
union {
char c;
short s;
int i;
long l;
unsigned short us;
unsigned int ui;
unsigned long ul;
float f;
double d;
void *addr;
char *str;
char chars[];
} value;
} Data;
void print_Data(Data d) {
switch (d.type) {
case CHAR:
printf("Char: %c\n", d.value.c);
break;
case SHORT:
printf("Short: %hd\n", d.value.s);
break;
case INT:
printf("Int: %d\n", d.value.i);
break;
case LONG:
printf("Long: %ld\n", d.value.l);
break;
case USHORT:
printf("Unsigned short: %hu\n", d.value.us);
break;
case UINT:
printf("Unsigned int: %u\n", d.value.ui);
break;
case ULONG:
printf("Unsigned long: %lu\n", d.value.ul);
break;
case FLOAT:
printf("Float: %f\n", d.value.f);
break;
case DOUBLE:
printf("Double: %lf\n", d.value.d);
break;
case ADDR:
printf("Address: %p\n", d.value.addr);
break;
case STR:
printf("String: %s\n", d.value.str);
break;
case CHARS:
printf("Chars: %s\n", d.value.chars);
break;
default:
printf("Invalid type\n");
break;
}
}
```
使用方法:
```
Data d;
d.type = INT;
d.value.i = 123;
print_Data(d);
```
输出:
```
Int: 123
```
阅读全文