The if
block introduces inner scope and declaration of res1
in the inner scope will hide the res1
declared and defined in the outer scope. Within the if
block wherever you use res1
, it will be the res1
declared in inner scope and not the one in the outer scope.
Also, in this statement of if
block,
int res1=(-1)*res1;
the uninitialised res1
is used in the expression used to initialise it. In the context of your program, this is undefined behaviour (curious to know why!, check this and this).
To fix the problem, either follow the suggestion given in the comment i.e. don't redeclare the res1
in if
block or you can also use standard library function abs()
to get the absolute value of res1
:
if ((res1 < 0) && (abs(res1) >= res2)) {
printf("%d\n", abs(res1)); //assuming you want to print absolute value of res1
}
Note that, to use standard library function abs()
, stdlib.h
header file need to include in the program.