Getting the Windows screen resolution in C

How you go about getting the screen resolution is dependent on the operating system and/or hardware you’re using. There is no concept of screen resolution in the C standard library.

  • If you’re running an operating system, you could use the API function(s) provided by the operating system to obtain the screen resolution.
  • If you’re not running any operating system, you would have to get this information from device registers, which are either memory-mapped or attached to I/O ports.
  • If you’re using third-party library to abstract the operating system or the hardware, you would use whatever mechanism the library provides to obtain this information.

If you’re running a modern version of Windows (Windows 2000, Windows XP, or later), and you don’t want to use a third-party library, you can use the Windows API functions GetDesktopWindow and GetWindowRect to get the current screen resolution:

#include "windows.h"
#include <stdio.h>
#include <stdbool.h>

// This function will return the horizontal
// and vertical screen resolution in pixels. 
// The function returns true on success, false otherwise.

bool GetDesktopResolution(int *pHorizontal, int *pVertical)
	bool status = false;

	// We'll need a RECT (rectangle) structure to receive the info.
	RECT desktopRectangle;

	// We'll also need an HWND (handle) to the Windows desktop. 
	const HWND desktopHandle = GetDesktopWindow();

	// If we got a handle, and we can get the rectangle
	// structure filled in, then report the resolution values 
	// to the caller through the parameters.
	if (desktopHandle && GetWindowRect(desktopHandle, &desktopRectangle))
		// The left and top values of the rectangle are 0 and 0.
		// We're interested in the right and bottom values.
		*pHorizontal = desktopRectangle.right;
		*pVertical = desktopRectangle.bottom;

		// Ensure we report success to the caller.
		status = true;

	return status;

// The following demonstrates how to use the
// GetDesktopResolution function.

int main(void)
	int horizontalResolution;
	int verticalResolution;

	if (GetDesktopResolution(&horizontalResolution, &verticalResolution))
		printf("Screen resolution is %d x %d.\n", 
			   horizontalResolution, verticalResolution);

	return 0;

On a Windows system with a current display resolution setting of 1920 x 1080, running the above program would display:

Screen resolution is 1920 x 1080.

Keep in mind that the user and other APIs can change the screen resolution, and that this code will report whatever the current screen resolution is. This might not be the maximum supported resolution or the native screen resolution.

Leave a Reply