Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Re: [C and C++]...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 12 Topic 4095 of 4306
Post > Topic >>

Re: [C and C++] && Operator precedence

by Jack Klein <jackklein@[EMAIL PROTECTED] > Apr 7, 2008 at 09:48 PM

On Tue, 08 Apr 2008 00:20:31 GMT, Hal Vaughan <hal@[EMAIL PROTECTED]
> wrote
in alt.comp.lang.learn.c-c++:

> Ulrich Eckhardt wrote:
> 
> > Aggro wrote:
> >> I would like to know the answer to this for both C and C++ as I use
both
> >> languages (usually in differen projects).
> >> 
> >> Assume the code
> >> if( A && B )
> >> 
> >> Normally A is executed before B
> > 
> > No. If A yields false, B is not executed at all. This is called
> > short-circuit evaluation. Note that in C++ you are actually able to
> > override operator && and then it's a normal function call which means
that
> > both arguments are evaluated, though their order is unspecified.
> > 
> > 
> >> But recently I got second hand information that the execution order
is
> >> not quaranteed. E.g. B could be executed before A by some compilers.
> > 
> > Not by conforming ones, due to the short-circuit rule.
> > 
> >> Different execution order could cause e.g. application to crash if we
> >> first test is the pointer valid and then call a function for that
> >> pointer: if( p && p->func() )
> > 
> > This code is fine.
> 
> I've seen something like this used in Perl where you can test if an
object
> or variable is defined by something like:
> 
> if ($p) ...

That is because Perl is a language that is interpreted at run time,
and objects are created by name at run time as part of the
interpretation process.

> Does it work that way in C and C++?  I didn't expect it to, but that
code
> example implies that you can do:
> 
> if (p)...
> 
> and if p is defined it evaluates as true and if p isn't defined, it
> evaluates as false.

This does not test if an object named 'p' has been defined.  If
nothing with the name 'p' is in scope, you will get an error message
from a C or C++ compiler.

C and C++ define logical expressions in a specific way.  Anything that
evaluates to 0 is considered false.  Anything that evaluates to a
non-zero value is true, any non-zero value at all.

So for any scalar type object 'obj', the expression:

   if (obj)

....is exactly equivalent to:

   if (obj != 0)

Note that I said scalar type, at least in C.  Scalar types include all
the arithmetic types such as char, int, long, float, double, and
pointer types.  These are also commonly called "built-in" in C++.

Another feature specific to the C language is that a null pointer to
any type will compare equal to a plain integer constant of 0.

So in both C and C++, you can test the results of standard library
functions like this:

   FILE *f = fopen("some_file_name", "r");
   void *v = malloc(SOME_NUMBER_OF_BYTES);

Each of these functions returns a valid pointer on success, or NULL on
failure.  So normally you test them:

   if (f != NULL) // file opened OK
   if (v == NULL) // memory allocated OK

....and take appropriate action if you could not open the file or
allocate memory.

But in both C and C++, a null pointer compares equal to a constant
expression with a value of 0, so you could just as easily write:

   if (f == 0) // file opened OK
   if (v == 0) // memory allocated OK

....and finally, evaluating a built-in type all by itself is equivalent
to comparing it to 0, so:

   if (f) // f != 0, file opened OK
   if (v) // v != 0, memory allocated OK

> Is that correct and is that acceptable usage in C and C++?

Note that the original posters example was testing a pointer for NULL
before using:

  if (p && p->func())

If p is a null pointer, the first expression of the && evaluates to 0
so the attempt to call a member function using a null pointer is not
executed.

In C++, there is the additional factor that some standard library
cl***** and types have overloaded comparison functions that return
true if the object is in a good state and can be used, and false if
the object is in some error state and not ready to use.  Consider this
example:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   for ( ; ; )
   {
      string fname;
	  cout << "enter file name: " << flush;
      getline(cin, fname);
      ifstream myFile(fname.c_str(), ios_base::in);

      if (myFile)
	  {
		  cout << "Opened " << fname << " OK" << endl;
		  myFile.close();
	  }
	  else
	  {
		  cout << "Can't open " << fname << endl;
		  myFile.clear();
	  }
   }
   return 0;
}

And here is some output I get from running it:

enter file name: c:\autoexec.bat
Opened c:\autoexec.bat OK
enter file name: c:\no_file_by_this_name
Can't open c:\no_file_by_this_name
enter file name: c:\boot.ini
Opened c:\boot.ini OK
enter file name:

You can't do this in C, with a structure or array type, only
arithmetic types and pointers.

> Thanks!
> 
> Hal

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.para****ft.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 




 12 Posts in Topic:
[C and C++] && Operator precedence
Aggro <spammerdream@[E  2008-04-07 20:24:45 
Re: [C and C++] && Operator precedence
Ulrich Eckhardt <dooms  2008-04-07 22:34:34 
Re: [C and C++] && Operator precedence
Hal Vaughan <hal@[EMAI  2008-04-08 00:20:31 
Re: [C and C++] && Operator precedence
Jack Klein <jackklein@  2008-04-07 21:48:22 
Re: [C and C++] && Operator precedence
Philip Potter <pgp@[EM  2008-04-08 08:55:35 
Re: && Operator precedence
=?ISO-8859-1?Q?Andr=E9_Ca  2008-04-08 04:06:10 
Re: && Operator precedence
Philip Potter <pgp@[EM  2008-04-08 13:21:46 
Re: [C and C++] && Operator precedence
Philip Potter <pgp@[EM  2008-04-07 23:12:16 
Re: [C and C++] && Operator precedence
Francis Glassborow <fr  2008-04-07 23:42:49 
Re: [C and C++] && Operator precedence
Anand Hariharan <znvyg  2008-04-08 04:48:45 
Re: [C and C++] && Operator precedence
Francis Glassborow <fr  2008-04-08 09:15:35 
Re: [C and C++] && Operator precedence
Anand Hariharan <znvyg  2008-04-08 16:00:12 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 12:22:15 CDT 2008.