I had made an error in my code at one point when I forgot to end a variable initialization with a semicolon and instead had put a comma. However, to my surprise it never returned an error and the code worked like normal.
Therefore I was wondering how this works ? I simplified my code by writing the below code;
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;
}
When executed is returns;
(43:test.c) - 3 6
I am very shocked that there is no error when I have functions separated in my initialization. However the functions were not even being called.
============== UPDATED
How about if the code was as follows, from what I see, now each function is called;
int main()
{
uint32_t val32;
val32 = 3, randomfunction_wret(), randomfunction();
printf("(%d:%s) - %u \n", __LINE__, __FILE__, val32);
return 0;
}
Output would be
(23:test.c) -
(29:test.c) -
(38:test.c) - 3
The line
uint32_t val32 = 3, randomfunction_wret(), valx = 6, randomfunction();
is equivalent to;
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.
The variables used in the function are properly defined and initialized. Hence, the function works without any problem.
Just as an aside, the implementation of randomfunction()
does not have a return
statement. Using it will cause undefined behavior.
Due to operator precedence, the line
val32 = 3, randomfunction_wret(), randomfunction();
is equivalent to:
(val32 = 3), randomfunction_wret(), randomfunction();
All the sub-expressions of the comma separated expression are evaluated. Hence, the functions randomfunction_wret
and randomfunction
are called and their return values are discarded.