C语言无法在函数外部对全局变量进行赋值操作

好久没有写博客了,今天写一个简短的博客,这是我在学习UCOSIII源码的时候遇到的问题,但是由于源码不太好直白地说明问题,所以我这里用一些其它的例子来介绍。

下面是一个main.c文件的内容

typedef struct{
	unsigned int count;
	float unit_price;
} apple_struct;

apple_struct apple1;
apple1 = {10, 2.1};

int main(void)
{
		printf("apple count is : %d \n", apple1.count);
		printf("apple unit_price is : %f \n", apple1.unit_price);
}

仿佛没有什么问题,但实际上是没法编译通过的,错误出在下面这条语句

apple1 = {10, 2.1};

C语言是不允许我们将全局变量在函数外部进行单独的赋值操作的,我们能做的只是声明,定义。如果非要进行赋值操作,那也只能在定义的时候进行初始化操作,如下所示:

typedef struct{
	unsigned int count;
	float unit_price;
} apple_struct;

apple_struct apple1 = {10, 2.1};

int main(void)
{
		printf("apple count is : %d \n", apple1.count);
		printf("apple unit_price is : %f \n", apple1.unit_price);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值