关于某个圆括号里面的float的问题~
程序代码:
// union2.c--联合的典型用法 #include <stdio.h> #define CHARACTER 'c' #define INTEGER 'I' #define FLOAT 'F' struct generic_tag{ char type; union shared_tag{ char c; int i; float f; }shared; }; void print_function(struct generic_tag generic); int main(void) { struct generic_tag var; var.type = CHARACTER; var.shared.c = '$'; print_function(var); var.type = FLOAT; var.shared.f = (float) 12345.67890; print_function(var); var.type = 'x'; var.shared.i = 111; print_function(var); return 0; } void print_function(struct generic_tag generic) { printf("\n\nThe generic value is..."); switch (generic.type) { case CHARACTER: printf("%c", generic.shared.c); break; case INTEGER: printf("%d", generic.shared.i); break; case FLOAT: printf("%f", generic.shared.f); break; default: printf("an unknown type: %c\n", generic.type); break; } }
问题在于
var.shared.f = (float) 12345.67890;
中为什么要加上float
有什么必要吗?是强制换格式?