33
\$\begingroup\$

Output the device's screen resolution in the specific format of [width]x[height](without the brackets). For example, an output could be 1440x900.

Here's an online tester that you can use to check your own screen resolution.

\$\endgroup\$
  • 17
    \$\begingroup\$ The specific output format is no fun, but it's probably too late to change now \$\endgroup\$ – Luis Mendo May 2 '17 at 13:29
  • 3
    \$\begingroup\$ What should the behaviour be if multiple displays are connected? \$\endgroup\$ – Jonathan Allan May 2 '17 at 13:40
  • 4
    \$\begingroup\$ I suppose we're not allowed to first change your resolution and then tell you those values, right? \$\endgroup\$ – Engineer Toast May 2 '17 at 15:12
  • 3
    \$\begingroup\$ APL\360 (can only be run on IBM/360 typewriter environment), 5 bytes: '0x0' \$\endgroup\$ – Adám May 2 '17 at 16:14
  • 4
    \$\begingroup\$ I like that this one disqualifies most golfing languages and encourages people to explore the limits of practical ones. \$\endgroup\$ – robbie May 4 '17 at 14:31

46 Answers 46

0
\$\begingroup\$

VBA (32-Bit), 110 Bytes

Locally declared windows function and anonymous VBE immediate window function that takes no input and outputs to the VBE Immediate window.

Note: The below may be made into a 64-bit compatible function by the addition of PtrSafe following Declare

Declare Function GetSystemMetrics Lib"user32.dll"(ByVal i&)As Long
?GetSystemMetrics(0)&"x"&GetSystemMetrics(1)
\$\endgroup\$
1
\$\begingroup\$

Racket, 73 bytes

#!racket/gui
(let-values([(x y)(get-display-size #t)])(printf"~ax~a"x y))

Just discovered the (discouraged) shorthand for #lang. Saves a few bytes! Documentation for get-display-size.

\$\endgroup\$
6
\$\begingroup\$

Bash + xrandr, 44 characters

read -aa<<<`xrandr`
echo ${a[7]}x${a[9]::-1}

xrandr belongs to the X server, on Ubuntu is provided by x11-xserver-utils package.

Sample run:

bash-4.3$ read -aa<<<`xrandr`;echo ${a[7]}x${a[9]::-1}
1920x1080

xrandr + grep + util-linux, 30 characters

xrandr|grep -oP '\d+x\d+'|line

Thanks to:

Sample run:

bash-4.3$ xrandr|grep -oP '\d+x\d+'|line
1920x1080
\$\endgroup\$
  • \$\begingroup\$ I have no bash with a display, would xrandr|grep * work? \$\endgroup\$ – Jonathan Allan May 2 '17 at 13:13
  • \$\begingroup\$ Sure. But for now the my grep and sed attempts to parse xrandr's output (pastebin.com/uTVcjWCq) were longer. \$\endgroup\$ – manatwork May 2 '17 at 13:15
  • \$\begingroup\$ Maybe xrandr|grep *|cut -d' ' -f1? (using the matching line from your paste @TIO) \$\endgroup\$ – Jonathan Allan May 2 '17 at 13:29
  • \$\begingroup\$ Ah, you mean to pick the resolution from the list by the “*” mark? Thought to that possibility, but I am not sure whether would work with multiple displays connected. As I remember, that would list each connected display's current resolution. \$\endgroup\$ – manatwork May 2 '17 at 13:36
  • \$\begingroup\$ Ah yes it would, not sure what the OP wants in such a scenario though! \$\endgroup\$ – Jonathan Allan May 2 '17 at 13:37
0
\$\begingroup\$

C#, 90 Bytes

()=>{var b=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return b.Width+"x"+b.Height;};

This actually is quite a similiar answer to that one, which already exists, however, I'd like to add a few things. This solution acutally needs a reference to System.Windows.Forms and one to System.Drawing. Those references are not normally added for a console application, so I'm not sure wether it's valid. Therefore I wrote a solution without references, however this has 580 287 Bytes:

using System;using System.Runtime.InteropServices;class P{static void Main()=>Console.Write(G().Item1+"x"+G().Item2);static(int,int)G()=>(GetSystemMetrics(0),GetSystemMetrics(1));[DllImport("User32.dll",ExactSpelling=true,CharSet=CharSet.Auto)]static extern int GetSystemMetrics(int n);}

Here's the solution with line breaks:

using System;
using System.Runtime.InteropServices;

class P
{
    static void Main() => Console.Write(G().Item1 + "x" + G().Item2);
    static (int, int) G() => (GetSystemMetrics(0), GetSystemMetrics(1));

    [DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    static extern int GetSystemMetrics(int n);
}

This solution was created with the huge help of AdamSchiavone on Stackoverflow.

I also wrote a C# Interactive programm with 123 Bytes, which automatically adds the references, so it can be executed on every machine without problems and without having the reference problems:

#r "System.Windows.Forms"
#r "System.Drawing"
var b=System.Windows.Forms.Screen.PrimaryScreen.Bounds;b.Width+"x"+b.Height

This basically is the same as the first one, but it adds the reference automatically and as it is executed in the interactive, you don't need the return statement, you can simply leave away the semicolon.

\$\endgroup\$
  • \$\begingroup\$ Your missing the ; on the end of your func. \$\endgroup\$ – TheLethalCoder May 4 '17 at 8:23
  • \$\begingroup\$ Side note: Those references aren't added normally for a Console application, they are for a Windows Forms one. Either way they don't need adding into the byte count. \$\endgroup\$ – TheLethalCoder May 4 '17 at 8:24
  • \$\begingroup\$ @TheLethalCoder You're right, edited my answer. \$\endgroup\$ – MetaColon May 4 '17 at 12:42
  • 1
    \$\begingroup\$ In your large solution you can change internal static class Program to just class P, you could probably remove static from the methods and use new P(), you can change IntPtr to int I believe, Move the i() out of the struct probably, change the struct to a class and probably a couple of other things. \$\endgroup\$ – TheLethalCoder May 4 '17 at 12:52
  • \$\begingroup\$ @TheLethalCoder I'll update my answer. Unfortunately, I won't have time to do this till tomorrow. \$\endgroup\$ – MetaColon May 4 '17 at 13:01
1
\$\begingroup\$

Batch, 218 208 206 bytes

I can't golf....

@for /f %%# in ('"@wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"')do @set %%#>nul
@echo %CurrentHorizontalResolution%x%CurrentVerticalResolution%
\$\endgroup\$
3
\$\begingroup\$

ZX Spectrum Basic, 10 bytes

just for completeness:

PRINT "256x192"

outputs 256x192. The Spectrum has a fixed hardwired screen resolution.

\$\endgroup\$
  • \$\begingroup\$ ...and uses a single byte for keywords like PRINT. \$\endgroup\$ – Jens Oct 18 at 8:11
0
\$\begingroup\$

8th, 32 bytes

0 hw:displaysize? swap . "x" . .

Returns the device's screen resolution of the first display (0) in pixels in the required format.

Output:

1536x864
\$\endgroup\$
1
\$\begingroup\$

Batch, 128 bytes

@for /f "tokens=1,2delims=x " %%A in ('"wmic path Win32_VideoController get VideoModeDescription|findstr "l""') do @echo %%Ax%%B

Basically the same thing as SteveFest's answer, except I grab a slightly shorter chunk of Wim32_VideoController and then I use something vaguely resembling regex to get the one line that contains the data that I want.

I have no idea why the wmic string needs to be inside of double quotes to work, and I can't believe that I can't shorten the string at all.

How It Works

Ordinarily, wmic path Win32_VideoController get VideoModeDescription will display something like this:

VideoModeDescription
3440 x 1440 x 4294967296 colors
 

I can use findstr's super rudimentary attempt at regex to find the letter "l," which only matches the line with the word "colors." From there, that line is delimited on spaces and the letter "x." %%A contains the first token and %%B contains the second token. After that, I just display the values.

\$\endgroup\$
  • \$\begingroup\$ Aw.. I've been looking for ways to shorten my code... Nice answer anyways \$\endgroup\$ – stevefestl May 6 '17 at 4:12
1
\$\begingroup\$

Python 3, 54 bytes

Python 2, 52 51 bytes

from PIL.ImageGrab import*;print'%dx%d'%grab().size

Saved 2 bytes by executing in Python 2

Saved 1 byte by changing import

\$\endgroup\$
  • \$\begingroup\$ Welcome to the site! I don't have PIL installed, so I can't test, but could you just do i.grab().size instead of the string formatting? I don't think it has to be exactly in the WxH format \$\endgroup\$ – DJMcMayhem May 3 '17 at 21:23
  • \$\begingroup\$ @DJMcMayhem Hello! I thought the same thing, but OP says specific format of [width]x[height]. i.grab().size prints out a tuple as (1920, 1080). Granite that would save me 8 bytes, I'd rather go for accuracy to OPs requirements \$\endgroup\$ – Wondercricket May 3 '17 at 21:26
1
\$\begingroup\$

Javascript, 42 bytes

console.log((k=screen).width+'x'+k.height)
\$\endgroup\$
  • 1
    \$\begingroup\$ Hello and welcome to our site! Make sure to include a byte count in your title for code-golf questions. \$\endgroup\$ – Wheat Wizard May 4 '17 at 5:25
  • \$\begingroup\$ Okay, thank you! Sorry, I didn't know it was a requirement. \$\endgroup\$ – Will Brickner May 5 '17 at 7:55
1
\$\begingroup\$

Bash + xdotool + tr, 35 bytes

-1 thanks to @manatwork

xdotool getdisplaygeometry|tr \  x

There are two spaces after the \

Gets geometry, then uses tr to replace spaces with x

\$\endgroup\$
  • \$\begingroup\$ Is enough to just escape the space, is shorter than quoting it: tr␠\␠␠x. (Used ␠ to mark spaces here just because HTML rendering collapses consecutive whitespaces.) \$\endgroup\$ – manatwork May 4 '17 at 8:31
33
\$\begingroup\$

TI-BASIC, 30 32 29 bytes (non-competing?)

*sigh* TI-BASIC takes an extra byte for every lowercase letter.

+2 thanks to @Timtech

-3 thanks to @Timtech

:If ΔX>.1
:Then
:Disp "96x64
:Else
:Disp "320x240

This only works because TI-BASIC can only be run on calculators with two different screen resolutions: 96 by 64 and 320 by 240. I just test to see which screen I have by setting the Zoom to something that is different depending on screen resolution then outputting the correct resolution.

I'm marking this as non-competing for now, since it is hard coded.

\$\endgroup\$
  • 6
    \$\begingroup\$ Thats a clever abuse ;) \$\endgroup\$ – Matthew Roh May 2 '17 at 15:38
  • 1
    \$\begingroup\$ You can save by not using ZDecimal and then using a different Xmax comparison, at least one byte. Also, I think you need to use lowercase x which is two bytes (x2) instead of the one-byte uppercase equivalent. \$\endgroup\$ – Timtech May 2 '17 at 18:22
  • \$\begingroup\$ @Timtech I have to use a two byte Zoom (like ZDecimal) because the default zoom (ZStandard) is the same on both calculators. I'll fix the capitalization, though. \$\endgroup\$ – Scott Milner May 2 '17 at 22:47
  • 1
    \$\begingroup\$ Oh, I see what you mean. If you use ZStandard though, would ΔX be different then between the calculators? Also, ZDecimal is only one byte, so this is 31 bytes. \$\endgroup\$ – Timtech May 3 '17 at 10:18
  • 2
    \$\begingroup\$ For some reason, my instant reaction is "that's valid, but wouldn't be valid if there were only one possible screen resolution", but that point of view seems internally inconsistent. So I'm really unsure as to whether this is cheating or not. \$\endgroup\$ – user62131 May 4 '17 at 1:08
0
\$\begingroup\$

Clojure, 86 bytes

Shameless port of this Java answer

(#(str(.width %1)"x"(.height %1))(.getScreenSize(java.awt.Toolkit/getDefaultToolkit)))
\$\endgroup\$
1
\$\begingroup\$

Red, 26 Bytes

system/view/screens/1/size

Outputs for example:

1920x1080

The code is pretty self explanatory. The 1 refers to the first screen

\$\endgroup\$
0
\$\begingroup\$

MATLAB, 42 byte

a=get(0,'ScreenS');fprintf('%dx%d',a(3:4))

Uses property shortening. Other than that, this is a nice showcase of the inflexibility of MATLAB indexing, requiring a temporary variable a. fprintf prints to stdout by default.

\$\endgroup\$
0
\$\begingroup\$

Javascript, 46 bytes

var s=screen;console.log(s.width+'x'+s.height)

I wanted to write it in the comment but I don't have reputation yet. Can someone explain to me why other solutions don't count bytes for console.log()? If they were counted then this solution would be the shortest.

\$\endgroup\$
7
\$\begingroup\$

PowerShell, 67 60 55 Bytes

-7 thanks to Martin Ender

-5 (actually 12!) from Leaky Nun , Regex wizardry is beyond me.

This is long but not longer than the horrendous System.Windows.Forms.SystemInformation.PrimaryMonitorSize solution

(gwmi win32_videocontroller|% v*n)-replace" |x \d+\D+$"

first we Get-WmiObject(gwmi) to retrieve the Win32_VideoController object, which contains a member named VideoModeDescription, which is a string in the format of 1920 x 1080 x 4294967296 colors, then I run a regex replace to get correct format.

PS H:\> (gwmi win32_videocontroller|% v*n)-replace" |x \d+\D+$"
1920x1080
\$\endgroup\$
  • \$\begingroup\$ I think (gwmi win32_videocontroller|% v*n)-replace" |x[^x]+$" shaves a couple of bytes by tweaking the regex. \$\endgroup\$ – TessellatingHeckler May 11 '17 at 16:49
6
\$\begingroup\$

C#, 101 95 89 bytes

_=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;};

-6 bytes thanks to @TheLethalCoder by reminding me OP didn't mention about printing, so returning a string is also fine. And an additional -6 bytes by changing it to a lambda.

\$\endgroup\$
  • \$\begingroup\$ You can save 11 bytes by compiling to a Func<string>: ()=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;};. However, you have a return of void but you are returning a string so you need to add 2 bytes for that. \$\endgroup\$ – TheLethalCoder May 3 '17 at 9:48
  • 1
    \$\begingroup\$ The challenge also doesn't state that you can't take input so you could add an unused input to save another byte i.e. _=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;}; \$\endgroup\$ – TheLethalCoder May 3 '17 at 9:49
  • 1
    \$\begingroup\$ Oh ignore the return comment you're writing the result out, you can save 6 bytes by returning it. \$\endgroup\$ – TheLethalCoder May 3 '17 at 9:49
  • \$\begingroup\$ And unless you can think of a way to get it shorter var s=System.Windows.Forms.Screen.AllScreens[0].Bounds; would also be the same count but you could golf it with that idea in mind. \$\endgroup\$ – TheLethalCoder May 3 '17 at 9:52
20
\$\begingroup\$

JavaScript (ES6), 32 bytes

_=>(s=screen).width+'x'+s.height

console.log((_=>(s=screen).width+'x'+s.height)())

\$\endgroup\$
  • 2
    \$\begingroup\$ the lambda version is acceptable \$\endgroup\$ – Felipe Nardi Batista May 2 '17 at 14:35
  • 6
    \$\begingroup\$ _=>(s=screen).width+'x'+s.height saves a byte \$\endgroup\$ – Felipe Nardi Batista May 2 '17 at 14:38
  • \$\begingroup\$ @FelipeNardiBatista Thanks, the thought just occurred to me as well :) \$\endgroup\$ – SethWhite May 2 '17 at 14:40
  • 1
    \$\begingroup\$ Good job! +1 :) \$\endgroup\$ – Arjun May 2 '17 at 14:42
  • 5
    \$\begingroup\$ I love how all the JS entries have been consistently shorter than a large number of the other answers. Almost never happens. \$\endgroup\$ – Draco18s May 2 '17 at 17:29
0
\$\begingroup\$

PHP + GTK 2.0, 69 bytes

This whole answer was written based on http://php-gtk.eu/en/code-hints/grabbing-a-screenshot-with-gdk

<?list($W,$H)=Gdk::get_default_root_window()->get_size();echo$W,x,$H;

Basically, fetches the "root" window (entire screen) and gets it's size.


Another way (based on http://www.kksou.com/php-gtk2/sample-codes/get-the-size-of-display-screen.php):

<?$w=(new GtkWindow())->get_screen();echo$w->get_width(),x,$w->get_height();

Gets the screen where the window was created, displaying it's dimentions.

\$\endgroup\$
1
\$\begingroup\$

Ruby + xrandr, 37 bytes

puts `xrandr`.split[7..9].join[0..-2]

Alternate solution (52 bytes):

puts `xrandr`.match(/t (\d+) (x) (\d+),/)[1..3].join
\$\endgroup\$
7
\$\begingroup\$

C (Windows), 79 78 77 bytes

Thanks to @Johan du Toit for saving a byte!

#import<windows.h>
#define G GetSystemMetrics
f(){printf("%dx%d",G(0),G(1));}
\$\endgroup\$
  • 2
    \$\begingroup\$ I was still messing around with 'GetDeviceCaps' until I saw your answer :-) You can still save 1 byte by using the following: #define G GetSystemMetrics f(){printf("%dx%d",G(0),G(1));} \$\endgroup\$ – Johan du Toit May 3 '17 at 18:45
4
\$\begingroup\$

Python 2, 61 49 bytes

Thanks @Jonathan-allan, @felipe-nardi-batista

from Tkinter import*
print'%sx%s'%Tk().maxsize()

For single display setups, this matches the output from the site. This gives entire resolution for multiple displays.

\$\endgroup\$
  • \$\begingroup\$ print'x'.... saves a byte \$\endgroup\$ – Felipe Nardi Batista May 3 '17 at 10:52
  • \$\begingroup\$ v=Tk().maxsize(), print'%sx%s'%v saves 9 bytes. \$\endgroup\$ – Jonathan Allan May 3 '17 at 13:04
  • \$\begingroup\$ oops, and then print'%sx%s'%Tk().maxsize() saves another 4 >_< \$\endgroup\$ – Jonathan Allan May 3 '17 at 13:14
0
\$\begingroup\$

C#, 89 bytes

_=>{var s=System.Windows.Forms.Screen.PrimaryScreen.Bounds;return s.Width+"x"+s.Height;};
\$\endgroup\$
-1
\$\begingroup\$

Bash, 21 bytes

tr , x</*/*/*/fb0/v*

If the device has frame buffer you can query /sys/class/graphics/fb0/virtual_size to get resolution. Width and Height are delimited by , so tr translates to x. Path to the file is shortened by using * to the point there are no ambiguities.

\$\endgroup\$
  • \$\begingroup\$ On my machine that glob also matches 4 files + 1 symlink in /usr/share/games/fortunes/vi, so I have to specify …/*0/… instead of …/f*/… (not sure whether you can count on that “0”, I'm not framebuffer fan). \$\endgroup\$ – manatwork May 3 '17 at 13:22
  • \$\begingroup\$ That depends upon directories. \$\endgroup\$ – Pandya May 3 '17 at 14:04
  • \$\begingroup\$ several upvoted answers do it as well, edited for a less likely to hapen option \$\endgroup\$ – marcosm May 3 '17 at 14:10
  • 1
    \$\begingroup\$ Note that this gives the size of the console frame buffer, which for X11 is often quite different from the size of the screen you are sitting in front of when logged in over the network. \$\endgroup\$ – Jens May 3 '17 at 14:15
  • \$\begingroup\$ console framebuffer is fbcon, not fb0. This outputs resolution of screen connected as framebuffer device fb0. \$\endgroup\$ – marcosm May 3 '17 at 18:45
1
\$\begingroup\$

xrandr and sh, 23 bytes

$ set `xrandr`;echo $6x$8
3360x1050

Tested on a CentOS 5 box with display redirected to a Cygwin machine with two monitors. Here the full xrandr output is

$ xrandr
 SZ:    Pixels          Physical       Refresh
*0   3360 x 1050   ( 889mm x 278mm )  *0
Current rotation - normal
Current reflection - none
Rotations possible - normal
Reflections possible - none
\$\endgroup\$
0
\$\begingroup\$

Python 2, 60 bytes

from win32api import*
u=GetSystemMetrics
print u(0),'x',u(1)
\$\endgroup\$
3
\$\begingroup\$

xrandr + awk, 25 bytes

xrandr|awk /\*/{print\$1}

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ This doesn't work. grep * expands the asterisk to all files in the directory. \$\endgroup\$ – Jens May 3 '17 at 8:07
  • \$\begingroup\$ @Jens Corrected. Thanks for pointing out \$\endgroup\$ – Pandya May 3 '17 at 8:56
  • \$\begingroup\$ Thanks; another hint: the proper spelling for grep|cut is awk. \$\endgroup\$ – Jens May 3 '17 at 9:11
  • \$\begingroup\$ It still doesn't work. It outputs *0. My xrandr output is *0 3360 x 1050 ( 889mm x 278mm ) *0. \$\endgroup\$ – Jens May 3 '17 at 9:15
  • \$\begingroup\$ @Jens then you need -f2 Btw, Can you check xrandr|awk '/\*/{print $2}'? \$\endgroup\$ – Pandya May 3 '17 at 9:29
6
\$\begingroup\$

Java 7, 123 114 bytes

String f(){java.awt.Dimension s=java.awt.Toolkit.getDefaultToolkit().getScreenSize();return s.width+"x"+s.height;}

This method will not work in a headless installation of Java (like on TIO) because it uses the awt libraries. Under the hood, calling getScreenSize uses the Java Native Interface to call out (typically into a C library) for the screen width and screen height.

-9 bytes thanks to Olivier Grégoire for reminding me that I can return the string instead of printing it.

\$\endgroup\$
  • 2
    \$\begingroup\$ I was just about to post... \$\endgroup\$ – Leaky Nun May 2 '17 at 13:24
  • \$\begingroup\$ @LeakyNun You and me both. +1 Poke. \$\endgroup\$ – Kevin Cruijssen May 2 '17 at 13:46
  • \$\begingroup\$ Too bad the output is restricted to ...x..., because void f(){System.out.print((java.awt.Toolkit.getDefaultToolkit().getScreenSize()+"").replaceAll("[^\\d,]",""));} which outputs 1920,1200 is shorter.. \$\endgroup\$ – Kevin Cruijssen May 2 '17 at 14:21
  • 1
    \$\begingroup\$ @KevinCruijssen yeah I did try playing with that as well. The real "too bad" is that using regex in java is so heavy in terms of byte count. \$\endgroup\$ – Poke May 2 '17 at 14:56
  • 1
    \$\begingroup\$ @Poke You're indeed right. I have been able to use that what I show above with an x instead of , by using some regex replacement, but it's five bytes more than your current answer: void f(){System.out.print((java.awt.Toolkit.getDefaultToolkit().getScreenSize()+"").replaceAll("[^\\d,]","").replace(",","x"));} or void f(){System.out.print((java.awt.Toolkit.getDefaultToolkit().getScreenSize()+"").replaceAll(".*?(\\d+).*?(\\d+).*","$1x$2"));} Ah well, what isn't heavy in Java.. ;p \$\endgroup\$ – Kevin Cruijssen May 2 '17 at 15:02
1
\$\begingroup\$

Lua (löve framework),116 bytes

f,g=love.window.setFullscreen,love.graphics function love.draw()f(1)w,h=g.getDimensions()f(0>1)g.print(w.."x"..h)end

The programm changes first to fullscreen then it gets the width and height and prints it then :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.