```
#define ABS(x) ((x)<0?-(x):(x))
int comparefn( const void* a, const void* b)
{
int int_a = * ( (int*) a );
int int_b = * ( (int*) b );
if ( int_a == int_b ) return 0;
else if ( int_a < int_b ) return -1;
else return 1;
}
int threeSumClosest(int* nums, int numsSize, int target) {
// sort the array
qsort(nums, numsSize, sizeof(int), comparefn);
int i, j, k, diff, min_diff = INT_MAX;
for(i = 0 ; i < numsSize ; i++)
{
j = i + 1;
k = numsSize - 1;
while(j < k)
{
diff = nums[i] + nums[j] + nums[k] - target;
//temp = ABS(temp);
if(diff == 0)
{
return target;
}
else
{
if(ABS(diff) < ABS(min_diff))
{
min_diff = diff;
}
if(diff < 0)
{// increase value
j++;
}
else//>0
{
k--;
}
}
}
}
return target + min_diff;
}
```