is it legal??? is it in the standard???Code:/*nothing here*/getsum(i) int i; /*what's this???*/ { /*... ...*/ }
is it legal??? is it in the standard???Code:/*nothing here*/getsum(i) int i; /*what's this???*/ { /*... ...*/ }
Last edited by Antigloss; 08-31-2005 at 01:00 AM.
The following code is strange for me. I don't specify a return value to function getsum, but it turns out that sum is actually returned to function main. why?
Code:#include <stdio.h> getsum(i) int i; { int sum = i; return sum; } int main( void ) { printf( "%d", getsum(10) ); return 0; }
That style is equivalent to:
It was used by K&R originally, before ANSI C.Code:getsum(int i) { int sum = i; return sum; }
But how can it return a value since we don't specify one?Originally Posted by cwr
And what are the benifits for using
Code:getsum(i) int i { int sum = i; return sum; }
> But how can it return a value since we don't specify one?
If anything is missing, K&R 'C' assumed int
So you have in effect 'int getsum( int i );
> And what are the benifits for using
None at all - ANSI-C prototypes make for a much better life.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Originally Posted by Antigloss
Wow it compiled That kind of style is confusing i wont use that.
Yes, sorry, I missed the implicit int return confusion. I still used to older style code omitting the int that my brain immediately glossed past it as quite normal.
And, indeed, it's a much better idea to always have an explicit return type.