473,320 Members | 1,958 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,320 software developers and data experts.

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

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;
}

Jul 17 '06 #1
7 2133
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

Py***********@gmail.com wrote:
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;
}
Jul 17 '06 #2
Py***********@gmail.com wrote:
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%20directive
>
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;
}
Jul 17 '06 #3

Py***********@gmail.com wrote:
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.

Jul 17 '06 #4
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.

Jul 17 '06 #5
Py***********@gmail.com wrote in news:1153124056.951388.87120
@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.
Jul 17 '06 #6
Vladimir Oka wrote:
Py***********@gmail.com wrote:
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.

Jul 17 '06 #7
"Vladimir Oka" <no****@btopenworld.comwrites:
Py***********@gmail.com wrote:
>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.

--
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.
Jul 17 '06 #8

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

Similar topics

2
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
5
by: Koen | last post by:
Hi all, I am experimenting with DAO. I wrote this sub to update one specific field of one specific row in one specific table. Nothing wrong with the SQL statement, but when I execute this I get...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
6
by: Daniel Rudy | last post by:
What is wrong with this program? When I try to compile it, I get the following error. Compiler is gcc on FreeBSD. strata:/home/dcrudy/c 1055 $$$ ->cc -g -oe6-3 e6-3.c e6-3.c: In function...
8
by: Brian Basquille | last post by:
Hello all, Bit of a change of pace now. As opposed to the typical questions regarding my Air Hockey game, am also working on a Photo Album which uses an Access Database to store information...
22
by: noridotjabi | last post by:
Okay. I'm quite embarased to be asking this but I cannot seem to get files to work. I don't know what the problem is. Is there something wrong with this: (file related) #include <stdio.h>...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
5
by: Ryan | last post by:
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder Creek","STATE":"CA","DIST":5745.4} I get an "invalid label" error... I'm kinda new to this. Thanks!
3
by: =?Utf-8?B?U2N5dGhlbg==?= | last post by:
Hi all, I have some code similar to the following: C# MyClass mVer = ne w MyClass(); …in a member function mVar.SendEvent( new Event() );
5
by: ukfusion | last post by:
I have an sql query which has several fields and no matter how hard i try i cant get the less than statement to work....i originally tried between but that wouldnt work either. $yourpricesearch is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.