Hi again!And happy new year to all.Just got to attained an interview and was asked about some c questions.Would like to know what does these lines means?Can anyone tell me?

int main()
{
int k=printf("Hello");
return 0;
exit(10);
}

I got answeres like

You're outputting "Hello" on the screen but since the function main has to return an integer (you said that when you named the function "int main()") you return 0 (meaning that the function has succeed)

But if the function doesn't work, the exit function will end it up and return a code error 10

But at interview I was asked questions like, what is the relation between int k and return 0?why it is returning 0?where it is passing it?who is checking if the function has succeded or not?and so on.Please guide me.As i cant find the explaination over internet.

But if the function doesn't work, the exit function will end it up and return a code error 10

That's completely wrong. The call to exit() will never occur because there's an unconditional return immediately prior.

what is the relation between int k and return 0?

There's no relation.

why it is returning 0?

Because the convention of C is to return a status code from main. If the status code is 0, that means successful termination. Anything else is unsuccessful.

where it is passing it?

The C runtime environment. Some people will say the operating system, but that's not strictly correct.

who is checking if the function has succeded or not?

Potentially nobody, but it stands to reason that the user or application who initially ran your program should have access to the final status code, right?

Thanks for your reply DJSAN10 and Narue.So I can say that main is a function which will output hello and will return 0 when successful.Is it that exit(10); has no role in the code?And is the function actually printing anything?Because it has just stored string in int k.I am bit confused about it.

Is it that exit(10); has no role in the code?

when you use return it tells a function to return execution of the program to the calling function and its value, all code that comes after that will be ignored

And is the function actually printing anything

you can try it yourself to see

Is it that exit(10); has no role in the code?

That's correct. However, there's a use case where the opposite situation would remove a warning. Some compilers will warn about the following with something akin to "no return from a function with non-void return type":

static int error_code;

...

int main(void)
{
    ...

    exit(error_code);
}

In such a case a common solution is adding a redundant return that will never be executed due to the call to exit():

static int error_code;

...

int main(void)
{
    ...

    exit(error_code);
    return 0;
}

And is the function actually printing anything?

The side effect of calling printf() is that it prints something. :D

This question has already been answered. Start a new discussion instead.