News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

C parameter

Started by Robert Collins, March 23, 2005, 12:40:57 AM

Previous topic - Next topic

Robert Collins

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.

rea

#1
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???).

tenkey

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.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

Robert Collins

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.

tenkey

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.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

Robert Collins

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.

James Ladd

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.


rea

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
}