当我忘记用分号结束变量初始化而放入逗号时,我的代码中出现了错误。但是,令我惊讶的是它从未返回错误,代码正常工作。
因此我想知道这是如何工作的?我通过编写下面的代码简化了我的代码;
uint32_t randomfunction_wret()
{
printf("(%d:%s) - \n", __LINE__, __FILE__);
return 6;
}
uint32_t randomfunction()
{
printf("(%d:%s) - \n", __LINE__, __FILE__);
}
int main()
{
uint32_t val32 = 3, randomfunction_wret(), valx = 6, randomfunction();
printf("(%d:%s) - %u %u\n", __LINE__, __FILE__, val32, valx);
return 0;
}
执行时返回;
(43:test.c) - 3 6
当我在初始化中分离了函数时,我感到非常震惊。然而,功能甚至没有被调用。
==============更新
如果代码如下,从我看到的,现在调用每个函数;
int main()
{
uint32_t val32;
val32 = 3, randomfunction_wret(), randomfunction();
printf("(%d:%s) - %u \n", __LINE__, __FILE__, val32);
return 0;
}
输出将是
(23:test.c) -
(29:test.c) -
(38:test.c) - 3
这条线
uint32_t val32 = 3, randomfunction_wret(), valx = 6, randomfunction();
相当于;
uint32_t val32 = 3; // Defines and initializes the variable.
uint32_t randomfunction_wret(); // Re-declares the function. Nothing else is done.
uint32_t valx = 6; // Defines and initializes the variable.
uint32_t randomfunction(); // Re-declares the function. Nothing else is done.
函数中使用的变量已正确定义和初始化。因此,该功能没有任何问题。
顺便说一句,randomfunction()
的实现没有return
声明。使用它将导致未定义的行为。
val32 = 3, randomfunction_wret(), randomfunction();
相当于:
(val32 = 3), randomfunction_wret(), randomfunction();
将评估逗号分隔表达式的所有子表达式。因此,调用函数randomfunction_wret
和randomfunction
并丢弃它们的返回值。