About Conditional Inclusion

H

Huangsyx

#include <stdio.h>

int main(void)
{
int a = 1;

#if 1 == a
puts("Success");
#else
puts("Failure");
#endif

return 0;
}

I've Compile this code in Visual C++ 6.0 and MingW.
It' Ok, but puts"Failure".
Why?
Alsoly, I've Compier this code in Turbo C 2.0. The TC(Turbo C)2.0 report an Error."Constant expression required in function main".

What the definination of "constant of expression"?

I am a beginner.:)
 
B

Ben Pfaff

Huangsyx said:
int a = 1;

#if 1 == a
puts("Success");
#else
puts("Failure");
#endif

This does not do what you think it does. Variables are not
visible in preprocessor expressions. In preprocessor
expressions, undefined names are silently replaced by 0. So "#if
1 == a" is equivalent to "#if 1 == 0".
I've Compile this code in Visual C++ 6.0 and MingW.
It' Ok, but puts"Failure".
Why?

This should be clear.
Alsoly, I've Compier this code in Turbo C 2.0. The TC(Turbo
C)2.0 report an Error."Constant expression required in function
main".

Turbo C 2.0 was released late in 1988. I doubt that it is fully
ANSI C compliant. Use some other compiler.
 

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,952
Messages
2,570,111
Members
46,695
Latest member
Juliane58C

Latest Threads

Top