The following snippet can be used to center a given GLFWwindow on any given GLFWmonitor. If there's no need for multi-monitor support, then simply use glfwGetPrimaryMonitor() to get the primary monitor. Thus centering the window by calling centerWindow(window, glfwGetPrimaryMonitor()).

Complete example can be found in the GLCollection repository on GitHub.

void centerWindow(GLFWwindow *window, GLFWmonitor *monitor)
{
    if (!monitor)
        return;

    const GLFWvidmode *mode = glfwGetVideoMode(monitor);
    if (!mode)
        return;

    int monitorX, monitorY;
    glfwGetMonitorPos(monitor, &monitorX, &monitorY);

    int windowWidth, windowHeight;
    glfwGetWindowSize(window, &windowWidth, &windowHeight);

    glfwSetWindowPos(
        window,
        monitorX + (mode->width - windowWidth) / 2,
        monitorY + (mode->height - windowHeight) / 2);
}

Best Monitor

The "best monitor" is a bit abstract, so let's consider a computer which has two monitors. The "best monitor" could then be defined, as the monitor which the window is mostly on, i.e., if 2/5 of the window is on monitor A and 3/5 of the window is on monitor B, then the "best monitor" would be monitor B.

Any given window can now be centered, on the nearest monitor by doing centerWindow(window, getBestMonitor(window)).

#define MAX(a, b) (((a) < (b)) ? (b) : (a))
#define MIN(a, b) (((b) < (a)) ? (b) : (a))

GLFWmonitor* getBestMonitor(GLFWwindow *window)
{
    int monitorCount;
    GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);

    if (!monitors)
        return NULL;

    int windowX, windowY, windowWidth, windowHeight;
    glfwGetWindowSize(window, &windowWidth, &windowHeight);
    glfwGetWindowPos(window, &windowX, &windowY);

    GLFWmonitor *bestMonitor = NULL;
    int bestArea = 0;

    for (int i = 0; i < monitorCount; ++i)
    {
        GLFWmonitor *monitor = monitors[i];

        int monitorX, monitorY;
        glfwGetMonitorPos(monitor, &monitorX, &monitorY);

        const GLFWvidmode *mode = glfwGetVideoMode(monitor);
        if (!mode)
            continue;

        int areaMinX = MAX(windowX, monitorX);
        int areaMinY = MAX(windowY, monitorY);

        int areaMaxX = MIN(windowX + windowWidth, monitorX + mode->width);
        int areaMaxY = MIN(windowY + windowHeight, monitorY + mode->height);

        int area = (areaMaxX - areaMinX) * (areaMaxY - areaMinY);

        if (area > bestArea)
        {
            bestArea = area;
            bestMonitor = monitor;
        }
    }

    return bestMonitor;
}