what's wrong with my code? about the #if statement.

P

Python.LeoJay

dear all,

i compiled the following code in VC8.0 and the output was "a < 2", but
in fact, the a is 3.
why the compiler thought the expression a<2 is true?

thanks.

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
const int a = 3;

#if (a < 2)
cout << "a < 2" << endl;
#endif

return 0;
}
 
F

Flo

You should write

if (a < 2) {
cout << "a < 2" << endl;
}

#if is a precompiler directive, not a statement. The difference is that
with the precompiler directive (#if) the test is made at compile time.
Either code is generated or not at all. Thus the argument must be a
preprocessor constant - to be honest I can't exactly describe what that
is. In the other case, the c++ if statement, the test is made at
runtime.

Flo
 
A

ankisharma

dear all,

i compiled the following code in VC8.0 and the output was "a < 2", but
in fact, the a is 3.
why the compiler thought the expression a<2 is true?
These directives are used in preprocessing so at this stage compiler is
not aware of the value of a. If you define "a" as macro then your
program will not print the "a<2" statement. In your case a is replaced
by 0 so u r getting the "a<2" output. if you replace the statement
(a<2) with (a<-1) then you will not see the "a<2" output.
Plz look at the following link for more detailed explanation
http://www-ccs.ucsd.edu/c/preproc.html#if directive
thanks.

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
const int a = 3;

#if (a < 2)
cout << "a < 2" << endl;
#endif

return 0;
}
 
V

Vladimir Oka

dear all,

i compiled the following code in VC8.0 and the output was "a < 2", but
in fact, the a is 3.
why the compiler thought the expression a<2 is true?

thanks.

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
const int a = 3;

#if (a < 2)
cout << "a < 2" << endl;
#endif

return 0;
}

First thing that's wrong with this is that it's C++, not C. You should
really have asked this in comp.lang.c++.

Second, are you sure this actually compiled, in whichever language?

At the time `#if` is parsed (by the pre-processor), `a` should not be
known to it.

Either you're not showing the actual code, or your (C) compiler is
broken. If you want C++ view on this, comp.lang.c++ is just down the
hall.
 
P

Python.LeoJay

i got it ,

thanks for all of your help.

p.s.
what i want to do is, of course, not just print a string on screen,
that is just a demonstration.
in reality, i want to write some log if the constant switch meet the
requirement, or the
log object will not be created at all.
 
H

HenryHu

(e-mail address removed) wrote in @s13g2000cwa.googlegroups.com:
dear all,

i compiled the following code in VC8.0 and the output was "a < 2", but
in fact, the a is 3.
why the compiler thought the expression a<2 is true?

thanks.

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
const int a = 3;

#if (a < 2)
cout << "a < 2" << endl;
#endif

return 0;
}

The "const variable" isn't compiling-term variable. So you cannot use it
in the process of precompile.
 
A

ankisharma

Vladimir said:
dear all,

i compiled the following code in VC8.0 and the output was "a < 2", but
in fact, the a is 3.
why the compiler thought the expression a<2 is true?

thanks.

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
const int a = 3;

#if (a < 2)
cout << "a < 2" << endl;
#endif

return 0;
}

First thing that's wrong with this is that it's C++, not C. You should
really have asked this in comp.lang.c++.
You are somewhat right but question asked by leo is very well
applicable to 'c' as well.
Second, are you sure this actually compiled, in whichever language?

At the time `#if` is parsed (by the pre-processor), `a` should not be
known to it.

Either you're not showing the actual code, or your (C) compiler is
broken. If you want C++ view on this, comp.lang.c++ is just down the
hall.
I am able to compile the code with gcc(replaced cout with printf) 3.2.3

In the condition check if preprocessor founds something which is not
macro then it replaces it with 0.
 
K

Keith Thompson

Vladimir Oka said:
i compiled the following code in VC8.0 and the output was "a < 2", but
in fact, the a is 3.
why the compiler thought the expression a<2 is true?

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
const int a = 3;

#if (a < 2)
cout << "a < 2" << endl;
#endif

return 0;
}

First thing that's wrong with this is that it's C++, not C. You should
really have asked this in comp.lang.c++.

Second, are you sure this actually compiled, in whichever language?

At the time `#if` is parsed (by the pre-processor), `a` should not be
known to it.

In a #if condition, unrecognized identifers are replaced by 0. See
C99 6.10.1.
 

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
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top