Does this program make any sense?

C

centurian

I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++
6" and it ran without any errors. Does this thing make any sense? Is it
of some use or some problem with grammar/CFG of c/c++?

#include <stdio.h>
int func()
{
int x;
return x,x;
}

int main()
{
int a,b;
a,b,a;
printf("%d",func);
return (a,b);
}


Regards,
Rabeeh.
 
J

John F

centurian said:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++
6" and it ran without any errors. Does this thing make any sense? Is it
of some use or some problem with grammar/CFG of c/c++?

#include <stdio.h>
int func()
{
int x;
return x,x;
}

int main()
{
int a,b;
a,b,a;
printf("%d",func);
return (a,b);
}

Are you sure:

printf("%d",func);

shouldn't be

printf("%d",func());

??

If so, it is legal, but it does not serve any useful purpose...

John
 
J

Jacek Dziedzic

centurian said:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++
6" and it ran without any errors. Does this thing make any sense? Is it
of some use or some problem with grammar/CFG of c/c++?

#include <stdio.h>
int func()
{
int x;
return x,x;
}

int main()
{
int a,b;
a,b,a;
printf("%d",func);
return (a,b);
}

Read up on "comma operator".

- J.
 
D

Daniel T.

"centurian said:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++
6" and it ran without any errors. Does this thing make any sense? Is it
of some use or some problem with grammar/CFG of c/c++?

#include <stdio.h>
int func()
{
int x;
return x,x;
}

int main()
{
int a,b;
a,b,a;
printf("%d",func);
return (a,b);
}

I recently berated someone for using the comma operator to avoid braces
in an 'if' statement. He did:

if ( condition ) expression1, expression2;

I was not impressed.
 
P

pemo

centurian said:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual
C++ 6" and it ran without any errors. Does this thing make any sense?
Is it of some use or some problem with grammar/CFG of c/c++?

#include <stdio.h>
int func()
{
int x;
return x,x;
}

int main()
{
int a,b;
a,b,a;
printf("%d",func);
return (a,b);
}


The comma operator is a 'sequence point', everything to the left of the
comma is evaluated before anything to the right.

So, return x,x; means evaluate x [and then throw away the result], now
evaluate x again, and return that value to the caller.

In main(), a,b,a means evaluate a [throw it away], do the same with b, then
do the same again with a.

printf("%d", func) - is either a typo - you meant func(), or else you're
trying to output the address of func() - in which case you should use %p and
cast func() to a void *, e.g. printf("%p", (void *)func);

Lastly, the return (a,b); As you're using a parens [which are not needed],
firstly a is evaluated and thrown away, then b is evaluated - it then
'feels' to me like b is evaluated again possibly - because of the parens.
Finally, the value of b is returned to the caller.
 
C

Chris Dollin

Daniel said:
I recently berated someone for using the comma operator to avoid braces
in an 'if' statement. He did:

if ( condition ) expression1, expression2;

I was not impressed.

Looks fine to me. What's your problem with it?
 
M

Mike Wahler

centurian said:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++
6" and it ran without any errors.

That was by chance. The program has undefined behavior.
Does this thing make any sense?

Not to me.
Is it
of some use

Not that I can see.
or some problem with grammar/CFG of c/c++?

The syntax is valid.
#include <stdio.h>
int func()
{
int x;
return x,x;

This statement causes evaluation of an
uninitialized object. Thus, if this
function is called (it's not called
in this program), the behavior is undefined.
}

int main()
{
int a,b;
a,b,a;

This statement causes evaluation of two uninitialized
objects. Thus the behavior is undefined.
printf("%d",func);

"%d" specifier is for type 'int' arguments.
You passed a function's address. Thus
the behavior is undefined.
return (a,b);

More undefined behavior.

-Mike
 
P

pete

Chris said:
Looks fine to me. What's your problem with it?

I'm one of those
who always uses a compound statement with an "if"
instead of a simple statement.

if (condition) {
expression1;
expression2;
}

About the only use I have for the comma operator
is in one of my swapping macros.

#define SWAP(A, B, T) \
((void)(*(T) = *(A), *(A) = *(B), *(B) = *(T)))
 
P

pete

pemo wrote:
printf("%d", func) - is either a typo - you meant func(),
or else you're
trying to output the address of func()
- in which case you should use %p and
cast func() to a void *, e.g. printf("%p", (void *)func);

The conversion of a function pointer to an object pointer,
is undefined.
 
D

Daniel T.

Chris Dollin said:
Looks fine to me. What's your problem with it?

Well, the actual line was:

if(*n % *i == 0) *i=(sig ? *n & (*i+1) : (*n+1) & *i)*2, n++;
else n++;

Apart from the duplicated code, it isn't idiomatic. Even most experts
would take pause, especially considering the code that surrounded it.
 
K

Keith Thompson

Chris Dollin said:
Looks fine to me. What's your problem with it?

It's ugly. It would be better written as

if (condition) {
expression1;
expression2;
}

The comma operator can be useful because it can be used in a larger
expression expression and because it yields the value of its right
operand. Here it's not being used in a larger expression, and the
result is being thrown away.
 
J

Jens Theisen

pemo said:
The comma operator is a 'sequence point', everything to the left of the
comma is evaluated before anything to the right.

Except for overloaded operator , .

Jens
 
J

Jens Theisen

Mike said:
This statement causes evaluation of two uninitialized
objects. Thus the behavior is undefined.

Can you point me to the point in the standard?


Jens
 
C

Chris Dollin

Keith said:
It's ugly.

Eye of the beholder, and all that - it looks fine to me ...
It would be better written as

if (condition) {
expression1;
expression2;
}

.... whereas /that/ looks ugly to me [always assuming, of course, that
the expressions will fit comfortably as written].
The comma operator can be useful because it can be used in a larger
expression expression

`E1, E2` is larger than either of `E1` and `E2`, but I don't see why
that matters; the comma operator is useful because it can /combine/
expressions into bigger expressions ...
and because it yields the value of its right operand.

.... and has a particularly simple evaluation rule.
Here it's not being used in a larger expression, and the
result is being thrown away.

The results of many expressions in C are thrown away, but that
doesn't make it wrong to use them as the then-part of an if-statement.

I was hoping for something more concrete than "it's ugly". Otherwise
it's just a discussion about style, and we can all pick our own.
 
R

Richard G. Riley

Eye of the beholder, and all that - it looks fine to me ...

if expr includes funtion calls it is always better to split the lines
so that you can use a debugger easily. Maintainability and all
that. This is not just a style thing, it is a practical requirement
for real systems.
 
C

centurian

I didn't know about comma operator before writing this program.

func (without () ) was just a typo.

I think comma operator makes sense. It adds writability to the program
but reduces readability.

Thanks.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top