user2931858
user2931858

Reputation: 47

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?

Upvotes: 1

Views: 817

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

In your code, Val_MAX being a #defined 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;
}

Upvotes: 2

Amol Saindane
Amol Saindane

Reputation: 1598

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

Upvotes: 0

Related Questions