how is this possible?? … I am expecting an error in storing of address in integer variable.
int y=&a;
violates the constraints for simple assignment in C 2018 6.5.16.1 1, which list several combinations of types allowed for the right operands. The only combination in which the left operand has arithmetic type other than _Bool
, as int
does, says the right operand must also have arithmetic type, which &a
does not:
… the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type…
C 2018 5.1.13 1 says what a conforming C implementation must do for this:
A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint,…
A warning is a diagnostic message. Therefore it satisfies the requirements of the C standard. This answers your question “how is this possible?”
Enable warnings in your compiler and elevate warnings to errors. With Clang, start with -Wmost -Werror
. With GCC, start with -Wall -Werror
. With MSVC, start with /W3 /WX
.