Assuming you meant int r; and not int d; then yes that is valid. However the scanf line serves little purpose as the value of r is overwritten by the getchar line.
Also note that it is very easy in C/C++ to write code that is completely legal while at the same time being logically undesirable.
Comment