This can be achieved easily with binary AND (&) operation as shown the function below.

typedef unsigned long IPv4Addr;

/* Function returns 1 if the IP falls in the subnet. Otherwise returns 0. */
int IsIpInTheSubnet(IPv4Addr ip, IPv4Addr subnet, IPv4Addr mask)
{
   if((ip & mask) == (subnet & mask)) return 1;
   return 0;
}

Example:

IP Address Subnet Mask Return Value
192.168.1.10 192.168.1.0 255.255.255.0 1
192.168.2.10 192.168.1.0 255.255.255.0 0

The above function can easily be converted in to a macro to make it simple and more efficient.

This is what preciselyTCP/IP networking stack does for finding a route in the system routing table before sending a packet out.