2

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 
  • 4
    It's simply Undefined Behavior not to return from a function that is declared to return. Undefined Behavior means literally anything can happen, and that includes appearing to work. – alter_igel Mar 18 '19 at 18:11
  • Possible duplicate of [How can I declare and define multiple variables in one line using C++?](https://stackoverflow.com/questions/6838408/how-can-i-declare-and-define-multiple-variables-in-one-line-using-c) – Reticulated Spline Mar 18 '19 at 18:12
  • @alterigel while that is true, this is not the immediate cause of OP's confusion. – SergeyA Mar 18 '19 at 18:16
  • @alterigel It's only undefined behavior if the return value of that function is used in an expression. If not, there's no UB. – dbush Mar 18 '19 at 18:19

1 Answers1

9

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.


Update, in response to the edited post.

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    Small caveat - `randomfunction` exposes undefined behavior, probably worth to add to the answer. – SergeyA Mar 18 '19 at 18:17
  • Added an extra part to question if you would know that. Thank you very much! –  Mar 18 '19 at 18:33