There are a number of macros that should be defined for your needs:
__GNUC__ // major
__GNUC_MINOR__ // minor
__GNUC_PATCHLEVEL__ // patch
The version format is major.minor.patch, e.g. 4.0.2
The documentation for these can be found here.