1
#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?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100

2 Answers2

2

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;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
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

Amol Saindane
  • 1,568
  • 10
  • 19