简体   繁体   中英

warning C4127: conditional expression is constant in cl command

#define Val_MAX 0
int main() {
   if(Val_MAX)
      printf("The value is %d",VALUE_MAX);
   return 0;
}

When I try to compile the above program if(VALUE_MAX) is showing a warning

conditional expression is constant.

How to solve the above warning?

In your code, Val_MAX being a #define d value to 0

if(Val_MAX)

is actually (you can check after preprocessing with gcc -E )

if(0)

which is not of any worth. The Following printf() will never execute.

FWIW, a selection statement like if needs an expression, for which the value evaluation will be expectedly be done at runtime. For a fixed value, a selection statement makes no sense. It's most likely going to end up being an "Always TRUE" or "Always FALSE" case.

One Possible solution: [With some actual usage of selection statement]

Make the Val_MAX a variable, ask for user input for the value, then use it. A pseudo-code will look like

#include <stdio.h>

int main(void) 
{
   int Val_MAX = 0;

   printf("Enter the value of Val_MAX\n");
   scanf("%d", &Val_MAX);

   if(Val_MAX)
      printf("The value is %d",VALUE_MAX);

   return 0;
}

Your preprocessor directive will replace VAL_MAX with 0 it becomes

if(0)

So anyways it will be false always and your printf won't execute and so if condition is of no use

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM