Suppose we want to return a long integer by a function. How to do it? Is it valid?
long int function()
{
long int b;
b=1000000000;
return b;
}
Suppose we want to return a long integer by a function. How to do it? Is it valid?
long int function()
{
long int b;
b=1000000000;
return b;
}
Yes, it is valid.
As long as you're retuning a proper long int
value (which we can see, you're doing#) and catching that into another long int
(you need to take care), it should be fine.
#) As per the C11
standard, LONG_MAX
is +2147483647
and 1000000000
is less than that. Chapter §5.2.4.2.1, for reference.
The C standard stipulates a minimum size of 32 bits for a long int
, stored in such a way as to guarantee that values in the range [−2147483647, +2147483647] can be represented. (Note that a 2s compliment representation gives you −2147483648). So the assignment of 1,000,000,000 to a long int
is always defined.
Returning a value copy of a long int
is well-defined too.
Note that if you hadn't initialised b
(i.e. if you had omitted the statement b=1000000000;
), then the behaviour of your program would have been undefined.
A function can return pretty much any type:
6.9.1
The return type of a function shall be void or a complete object type other than array type.
And that's it.