-1

I am learning C using Visual Studio 2015. If I create a brand new project and execute nothing other than the following code:

#include <stdio.h>

int main()
{
#if abc == xyz
    printf("Expression is true.");
#else
    printf("Expression is false.");
#endif
}

the application prints out the string Expression is true which is something I was not expecting. The reason I was not expecting this to work like this is because I was expecting a compile error given that the abc and xyz tokens are not defined or declared anywhere in code. So the question is why is this working?

Finally, if I declare and define abc and xyz as follows:

int abc = 123;
int xyz = 456;

the application prints out the string Expression is true? This looks clearly wrong doesn't it? Why is it that if I declare and define the variables as integers with different values I trigger the #if and not the #else?

Thank you.

T555
  • 217
  • 2
  • 9
  • 2
    `#define` doesn't use `=` for its syntax. It's `#define IDENTIFIER string`, then the C pre-processor will replace `IDENTIFIER` in the code with `string` before the C compiler even gets to it. Arguments to `IDENTIFIER` can also be provided. Just google search "C #define syntax" and find, for example, [this information](https://msdn.microsoft.com/en-us/library/teas0593.aspx?f=255&MSPPError=-2147217396). – lurker Feb 14 '16 at 23:12
  • That's not how the `#define` directive is used, if you define it like that then `abc` will be replaced by `= 'a';` literally. Also, if `abc` and `xyz` are not defined I would expect `#if abc == xyz` to be true. – Iharob Al Asimi Feb 14 '16 at 23:12
  • 2
    The C Preprocessor does not interact with normal C code, at all. They have completely different syntaxes, and you cannot use C variables in the preprocessor like you are trying to. – Colonel Thirty Two Feb 14 '16 at 23:13
  • @lurker: I edited the post (fixed the #define = 'a' ..... ) to reflect what I really meant to ask. I was rushing while posting this and I totally screwed it up. I will checkout your link. Thanks. – T555 Feb 14 '16 at 23:30
  • @iharob: That was a mistake. See my response to lurker. Thanks. – T555 Feb 14 '16 at 23:34
  • 1
    Semicolons are not part of the syntax either, unless you want the semicolons to be part of your substituted string. You should read the documentation. I think that will clear up some of your issues. – lurker Feb 14 '16 at 23:35
  • @lurker: True, man, I really suck at this. I am trying to learn C in a day and its not working out very well. I removed the whole paragraph because it was wrong. Thanks. – T555 Feb 14 '16 at 23:37
  • 2
    You've got 3 different questions here. It's best to try and keep things to 1 question per post, especially since you keep getting the syntax wrong and changing the question. Your first question is a duplicate of [this question](http://stackoverflow.com/q/5085392/1287251) (which, despite being about C++, was answered with a C answer (and later edited to include the C++ answer, which is the same)). The answer to your second question is the same as the first (the preprocessor does not care about your declarations that exist outside of the preprocessor). – Cornstalks Feb 14 '16 at 23:38
  • 2
    If you can't learn C in a day, that doesn't mean you suck at it. A single day is a very aggressive goal. You need time to study and try things out. Study has to be part of it, or your just guessing. – lurker Feb 14 '16 at 23:40
  • @Cornstalks: Lesson learned. – T555 Feb 14 '16 at 23:41

1 Answers1

2

My suggestion would be, at least in the first few days (weeks/months/years?) of using C, don't use the preprocessor # commands, other than,

  1. to #include library headers, and
  2. to #define flags if required by those headers.

Trying to do anything else with the preprocessor is not worth the effort and confusion.

In this case, use a regular if statement:

if ( abc == xyz ) {
    printf("Expression is true.");
} 
else {
    printf("Expression is false.");
}
Chad
  • 693
  • 5
  • 17