473,388 Members | 1,277 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Does this program make any sense?

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.

Feb 21 '06 #1
23 2167

"centurian" wrote:
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
Feb 21 '06 #2
centurian wrote:
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.
Feb 21 '06 #3
In article <11**********************@f14g2000cwb.googlegroups .com>,
"centurian" <ra***********@gmail.com> wrote:
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.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 21 '06 #4
centurian wrote:
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.
--
==============
Not a pedant
==============
Feb 21 '06 #5
Daniel T. wrote:
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?

--
Chris "was stirred, now shaken" Dollin
RIP Andreas "G'Kar" Katsulas, May 1946 - February 2006
Feb 21 '06 #6

Chris Dollin wrote:
Daniel T. wrote:
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?


Readability, perhaps?

--
Chris "was stirred, now shaken" Dollin
RIP Andreas "G'Kar" Katsulas, May 1946 - February 2006


/Peter

Feb 21 '06 #7

Chris Dollin wrote:
Daniel T. wrote:
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?


Readability, perhaps?

--
Chris "was stirred, now shaken" Dollin
RIP Andreas "G'Kar" Katsulas, May 1946 - February 2006


/Peter

Feb 21 '06 #8

"centurian" <ra***********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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
Feb 21 '06 #9
Chris Dollin wrote:

Daniel T. wrote:
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?


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)))

--
pete
Feb 21 '06 #10
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.

--
pete
Feb 21 '06 #11
In article <dt**********@malatesta.hpl.hp.com>,
Chris Dollin <ke**@hpl.hp.com> wrote:
Daniel T. wrote:
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?


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.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 21 '06 #12
Chris Dollin <ke**@hpl.hp.com> writes:
Daniel T. wrote:
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?


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #13
Mike wrote:
This statement causes evaluation of two uninitialized
objects. Thus the behavior is undefined.


Can you point me to the point in the standard?
Jens

Feb 21 '06 #14
pemo wrote:
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

Feb 21 '06 #15
jt***@arcor.de (Jens Theisen) writes:
This statement causes evaluation of two uninitialized
objects. Thus the behavior is undefined.


Can you point me to the point in the standard?


Which standard? This discussion is cross-posted to comp.lang.c and
comp.lang.c++.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #16
jt***@arcor.de (Jens Theisen) writes:
pemo wrote:
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 , .


C doesn't have operator overloading (look at the Newsgroup: header line).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #17
Keith Thompson wrote:
Chris Dollin <ke**@hpl.hp.com> writes:
Daniel T. wrote:
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?


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.

--
Chris "was stirred, now shaken" Dollin
RIP Andreas "G'Kar" Katsulas, May 1946 - February 2006
Feb 22 '06 #18
On 2006-02-22, Chris Dollin <ke**@hpl.hp.com> wrote:
Keith Thompson wrote:
Chris Dollin <ke**@hpl.hp.com> writes:
Daniel T. wrote:
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?


It's ugly.


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.
Feb 22 '06 #19
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.

Feb 22 '06 #20
In article <dt**********@malatesta.hpl.hp.com>,
Chris Dollin <ke**@hpl.hp.com> wrote:
Keith Thompson wrote:
Chris Dollin <ke**@hpl.hp.com> writes:
Daniel T. wrote:
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?


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.


*Just* a discussion about style? "It compiles" is not the sole
justification for using a particular construct. If you disagree, try
maintaining some winners of the obfuscated code contest.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 22 '06 #21
jt***@arcor.de (Jens Theisen) wrote in news:9o*********@jens-theisen.de:
pemo wrote:
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 , .


But you shouldn't do that. Overloading operator, is a dangerous idea (as
Scott Meyers would tell you) because it doesn't behave as the comma op for
built-in types: the built-in guarantees that the left operand is evaluated
before the right operand, whereas the overloaded operator, is a function,
and the order of eveluating arguments for a function is implementation-
defined (or worse).

--
Life is complex, with real and imaginary parts.
Feb 23 '06 #22
Csaba wrote:
jt***@arcor.de (Jens Theisen) wrote in news:9o*********@jens-theisen.de:
pemo wrote:
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 , .


But you shouldn't do that. Overloading operator, is a dangerous idea (as
Scott Meyers would tell you) because it doesn't behave as the comma op for
built-in types: the built-in guarantees that the left operand is evaluated
before the right operand, whereas the overloaded operator, is a function,
and the order of eveluating arguments for a function is implementation-
defined (or worse).


I beg to differ. One good thing about operator overloading is that you can
write code that closely resembles what you would read in text books on the
particular problem domain that you are dealing with. A prominent example of
this is boost::spirit. I overloads the comma-operator so that code can use
the EBNF to denote syntax rules. I cannot see anything wrong with that.
Best

Kai-Uwe Bux
Feb 23 '06 #23
Kai-Uwe Bux <jk********@gmx.net> wrote in
news:dt**********@murdoch.acc.Virginia.EDU:
Csaba wrote:
[snip] Overloading operator, is a dangerous idea
.... and as such should only be attempted by a trained professional :-)
(as Scott Meyers
would tell you) because it doesn't behave as the comma op for
built-in types [snip]

I beg to differ. One good thing about operator overloading is that you
can write code that closely resembles what you would read in text
books on the particular problem domain that you are dealing with. A
prominent example of this is boost::spirit. It overloads the
comma-operator so that code can use the EBNF to denote syntax rules. I
cannot see anything wrong with that.


boost::spirit is strange enough so the user would not expect the comma
operator to behave as the builtin. For innocuous-looking classes it is
probably best avoided.

--
Life is complex, with real and imaginary parts.
Feb 24 '06 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Edwards | last post by:
Hello, I have sort of a newbie question. I'm trying to optimize a loop by breaking it into two passes. Example: for(i = 0; i < max; i++) {
0
by: mike | last post by:
regards: Does the following programming architecture make sense? http://www.wretch.cc/album/show.php?i=otp&b=1&f=1111993473&p=2 ...
22
by: Ryan M | last post by:
I've been programming for a while, but most of my experience is on unix. How do C compilers work on operating systems that weren't written in C? And that have no libc? Compiling C on unix seems...
2
by: mikelinyoho | last post by:
Does the following program architecture make sense? #include <stdio.h> #include "test1.c" #include "sample.cpp" int main(void){ output();
19
by: centurian | last post by:
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...
3
by: electrician | last post by:
Yes, no GOTO. This is a major blunder on part of the creators of these tools. GOTO gives the programmer the absolute control over the program. Yes, no matter what, a GOTO sends the program to...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
4
by: Jason Teagle | last post by:
I'm not sure which is the correct group to post this to, if either, so apologies for the crosspost and if it's OT. I have a Visual Studio.NET 2002-compiled solution that originated at work. At...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.