The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => The Orphanage => Topic started by: Robert Collins on March 23, 2005, 12:40:57 AM

Title: C parameter
Post by: Robert Collins on March 23, 2005, 12:40:57 AM
Can someone explain the below code snippit? It's no big deal I'm just curious.


int some_function(ecode)
int ecode;                 <-------------------- what does this mean here instead of it being in the function argument list?
{
     '
     '
     '
     '
  return 0;
}


My curisoty is the int ecode; before the curly bracket. Usually I see it in the argument list some_function(int ecode) so I was just wondering what the difference is between the two. That's all.
Title: Re: C parameter
Post by: rea on March 23, 2005, 12:51:19 AM
Only the sintaxis??? :D

Is the same, they are defining the type of the variable... I supose is a standar, for example you will find that notation in the gcc source code... then should be a standar (perhaps old???).
Title: Re: C parameter
Post by: tenkey on March 23, 2005, 05:16:37 AM
It's the old Unix (pre-C++) syntax for declaring the types of parameters. It's often called K&R syntax for Kernighan and Ritchie, who wrote the first C book that appeared in book stores. Unless you're using old compilers, you don't want to use it.
Title: Re: C parameter
Post by: Robert Collins on March 23, 2005, 05:27:05 AM
Quote from: tenkey on March 23, 2005, 05:16:37 AM
It's the old Unix (pre-C++) syntax for declaring the types of parameters. It's often called K&R syntax for Kernighan and Ritchie, who wrote the first C book that appeared in book stores. Unless you're using old compilers, you don't want to use it.

Well appearantly it makes no difference to VC++ 6.0. I downloaded some C code (I think it's a mumbo-jumbo array of all kinds of coding from various authors all put together in a single zip file) and there is a mixture of the various styles but VC++ 6.0 seems to deal with it quite OK. Not being a really good 'C' programmer I just decided to leave well enough alone and not try to change any of it as long as I don't get any compile errors it's OK with me.
Title: Re: C parameter
Post by: tenkey on March 26, 2005, 02:54:40 AM
It's part of the C standards to accept the old syntax. Not sure about C++.

For compatibility, you don't get any of the new type checking features with it.
Title: Re: C parameter
Post by: Robert Collins on March 26, 2005, 03:27:33 AM
So, then, is it correct to conclude that....

int some_function(a, b, c, d, e)
int a;
int b;
char* c;
long d;
long e;
{
     '
     '
     '
     '
  return 0;
}

....is the same as.....

int some_function(int a, int b, char* c, long d, long e)
{
     '
     '
     '
     '
  return 0;
}

If so, it sure seems to me that the 'old' way is kind of redundant...much simpler to use new style.
Title: Re: C parameter
Post by: James Ladd on March 26, 2005, 05:24:45 AM
Robert,
You are using K&R style declarations. K&R = Brian W. Kernighan and Dennis M. Ritchie. http://cm.bell-labs.com/cm/cs/cbook/
In modern C compilers the type declarations are inline with the argument.
You wont have trouble with using the inline version with modern compilers like GCC and Visual Studio.
You would have had trouble in the past.

Title: Re: C parameter
Post by: rea on March 26, 2005, 05:17:30 PM
By the way, in the prototypes or declaration of a C function you can only define the types of arguments like:


xStruct * nameOfFunction(int, double, char);

Is sometimes called the signature of the function (some one correct me if Im wrong ;) ).



But sure in the implementation of the function you should declare the names... because if you dont declare names, how you will refer to those values inside the function...???

xStruct * nameOfFunction(int v1, double v2, char v3);{
// implementation of the function
}