您的位置:首页 > 其它

关于一个共用体字符串输入的问题

2014-01-07 17:19 253 查看
# include <stdio.h>

# include <conio.h>

# include <stdlib.h>

union computerInfo

{

char band[20];

float price;

};

void main(void)

{

union computerInfo comp1 = {"Asus X80"};

//union computerInfo comp1 ;

int type = 0;

puts("Please input the type of the computers:");

scanf("%d",&type);

switch(type)

{

case 0:

puts("please input the band of the computer");

scanf("%s",&comp1.band)(1);

//scanf("%s",comp1.band)(2);

//gets(&comp1.band)(3);

//gets(comp1.band)(4);

问题出现在这里: 在这里(1)和(2)两个表达式都是可以执行的,为什么呢?按道理来说,(1)是不对;(2)是正确的,但是不能输入空格隔开的单词;(3)是错的,编译的时候就会通知有错误;(4)编译可以通过,但是程序执行的时候,不需要输入,直接就略过了输入品牌的这一步为什么?

printf("the type of the computer is: %s\n",comp1.band);

break;

case 1:

puts("please input the price of the computer");

scanf("%f",&comp1.price);

printf("The price of the computer is %f\n",comp1.price);

break;

default:

puts("The input type is wrong!");

break;

}

}

问题就在上面,求大侠解答。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: