int vs *char

friends i compiled this code and i dont understand how the output is 42.the condition in if should be an int but we put a string and still we got 42.i expected an error like "cannot convert *char to int"

1
2
3
4
5
6
7
8
9
10
11
12
13
#‎include‬<stdio.h>
 int main()
 {
 int x;
 x=42;
 if("yes")
 {
 int x;
 x++;
 }
 printf("%d\n",x);
 return 0;
 }
Last edited on
You're declaring a new variable x in the if block. That x variable is not the same as the one declared outside the if block. I just added a couple of extra output statements to the code to show the difference.

the value of x in the if statement is 1
the value of x outside the if statement is 42
yea that i figured out already.my problem isnt with scope related concept.what i'm asking is why *char when converted to int don show any error.
closed account (2AoiNwbp)
Why do you think that char* must be converted to int in that expression? "yes" is just a single expression different than 0, which makes it true.
Thus, if you put a print statement inside the if block, you'll see the real value of the inner x (which is never initialized, so it could have any value)
Last edited on
closed account (2AoiNwbp)
After doing so, you can test
 
if(!"yes")

and see, it turns to false.

regards,
Alejandro
Ok Thanks for the answer.
Topic archived. No new replies allowed.