How many parameters can "return statement" take in C programming?

2298 views c
7

code snippet code following:

#include <stdio.h>
#include <stdbool.h>

int test()
{
    return (printf("it is test\n"), false);
}

int main()
{ 
    if (false == test())
        printf("return result is false\n");
}

The return statement works but I don't know why it can work as i didn't encounter the statement before. Can any body help to explain the usage of return statement in this snippet code?

answered question

This is a C question and therefore there is no operator overloading.

@Blaze: That's half an answer. You first need to establish it's parsed as an operator.

there is something called the comma operator: stackoverflow.com/questions/1737634/c-comma-operator

1 Answer

1

To answer the actual main question, exactly one, If you wish to return more you need to pass in a pointer, or return one to a struct, ie:

#include <stdio.h>
#include <stdbool.h>

bool test(int * data)
{
    *data = printf("it is test\n");
    return false;
}

int main()
{
    int data;
    if (false == test(&data))
        printf("return result is false, data = %d\n", data);
}

As for why it works, please see: Comma-Separated return arguments in C function

posted this

Have an answer?

JD

Please login first before posting an answer.