The main issue here is understanding the priorities yasnippet uses to
determine what snippet it should expand.
If I understand the documentation correctly, this is controlled by the
yas-key-syntaxes variable which by default is set to: ("w" "w_" "w_." "w_.()" yas-try-key-from-whitespace). This might look rather cryptic if you're not
familiar with Emacs syntax
classes, but it should be rather clear from the following description:
Yasnippet wants to skip an unknown number of characters backwards to find
keywords to expand, so it tests each of the elements of yas-key-syntaxes in
turn, using each combination of characters to determine what characters to skip:
w Skip any piece of text that matches words.
_ Skip any piece of text that matches symbols.
. Skip any piece of text that matches punctuation.
In this case, you can fix your #if snippet by changing the yas-key-syntax
priorities to try "w." before "w" or "w_" since # belong to the
punctuation syntax class. I.e.,:
(setq yas-key-syntaxes '("w." "w" "w_" "w_." "w_.()" yas-try-key-from-whitespace))
Personally, I would bind the snippet to a different keyword, such as pif (pp for pre-processor) instead, but feel free to try it out.
It might also be useful to check out M-x describe-syntax, to figure out what
characters belong to which syntax class in a particular major mode.