A cast can only convert a value; it cannot affect an object (the memory of a variable). And values do not have qualifiers such as volatile
.
When an object’s value is used in an expression, qualifiers are removed, per C 2018 6.3.2.1 2:
Except when it is the operand of the sizeof
operator, the unary &
operator, the ++
operator, the --
operator, or the left operand of the .
operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue); this is called lvalue conversion. If the lvalue has qualified type, the value has the unqualified version of the type of the lvalue;…
So, in (int)a
, the a
is converted from a volatile int
lvalue to an int
value before the cast occurs. The cast has no effect; it converts an int
to an int
. Also note that the value is cast—your title asks about “casting a volatile variable,” but it is impossible to cast a variable, because it is automatically converted to its value before the cast.
So the cast has no effect on a
. The object a
remains volatile. Its value is passed to my_function_1
as a value without the volatile
qualifier.