the whys of the comma operator

M

Michael Scarlett

I was just curious as to why i could not write a function like the
following:
Code:
int bad_limits(int begin, end, low, high){
    int not_good = 0; 
    if (begin > end){
        printf("%d isn't smaller than %d\n", begin,end);
        not_good = 1;
    }

when decalaring, one can make multiple declarations:

Code:
int begin, end, low, high

however after compiling the preceeding function, devc++ complained and
stop comilation.

Any ideas as to why?
 
P

pete

Michael said:
I was just curious as to why i could not write a function like the
following:
Code:
int bad_limits(int begin, end, low, high){
int not_good = 0;
if (begin > end){
printf("%d isn't smaller than %d\n", begin,end);
not_good = 1;
}

when decalaring, one can make multiple declarations:

Code:
int begin, end, low, high

however after compiling the preceeding function, devc++ complained and
stop comilation.

Any ideas as to why?

That's just the rules. The comma, as used to separate
parameters in a function defintion,
is a punctuator and not an operator,
despite what the subject line of this thread says.
 
P

Peter Ammon

Michael said:
I was just curious as to why i could not write a function like the
following:
Code:
int bad_limits(int begin, end, low, high){
int not_good = 0; 
if (begin > end){
printf("%d isn't smaller than %d\n", begin,end);
not_good = 1;
}

when decalaring, one can make multiple declarations:

Code:
int begin, end, low, high

however after compiling the preceeding function, devc++ complained and
stop comilation.

Any ideas as to why?

I've always thought that was a silly and confusing limitation as well.
 
S

Stephen Sprunk

Because the standard says you can't?
I've always thought that was a silly and confusing limitation as well.

(A) types are mandatory but argument names are optional in a function
declaration.

(B) differing types can appear in the same argument list; the comma becomes
ambiguous if it always separates variables but not always separates types.

Neither of these problems occur in variable declarations, so it makes sense
they have slightly different syntax.

S
 
D

Dan Pop

In said:
Michael said:
I was just curious as to why i could not write a function like the
following:
Code:
int bad_limits(int begin, end, low, high){
int not_good = 0; 
if (begin > end){
printf("%d isn't smaller than %d\n", begin,end);
not_good = 1;
}

when decalaring, one can make multiple declarations:

Code:
int begin, end, low, high

however after compiling the preceeding function, devc++ complained and
stop comilation.

Any ideas as to why?

I've always thought that was a silly and confusing limitation as well.

That's because you didn't engage your brain. Function definitions must
be consistent with function prototypes, which may use a list of types,
only:

int foo(int, int, int);

and you really don't want to use parameter names in a prototype
declaration, that is not also a definition. So, it is perfectly natural
and consistent for the function definition to follow the same pattern.

Dan
 
E

Emmanuel Delahaye

In said:
int begin, end, low, high

however after compiling the preceeding function, devc++ complained and
stop compilation.

Any ideas as to why?

Yes. A semi-colon is missing.

int begin, end, low, high;
 
M

Michael Scarlett

Emmanuel Delahaye said:
Yes. A semi-colon is missing.

int begin, end, low, high;

--------------


Like someone else said earlier, the variable names don't need to be
declared in the prototype they are just dummy names anyway so:

int myfunction(int,char,int);


is fine, as long as when you call the function they are given the
correct arguement types.

incedently to in regards to the intial question: the obivious answer
is cause ANSI C doesn't allow it. However ANSI does allow you to use
the pre-ANSI version of the declaration as follows:
[fragment]

int ThisFunction(arg1,arg2,arg3)
int arg1,arg2,arg3;

[/fragment]

Though I suppose this will be phased out in time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top