Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Your program has to make the computer produce a sound, any sound.

Shortest code wins, not sooner than 10 days after the first valid answer. If there is a tie, the one submitted sooner, wins.

  • The program should run on a reasonable, not too uncommon personal computer.
  • Opening a pop-up, message box, etc. (for example, on a web page) does not count, as they might or might not produce a sound, depending on a lot of settings.
  • Just entering a wrong command or invalid character on a console and receiving a warning beep does not count, just as the compiler/interpreter/OS beeping on an error or crash does not count either. Your code must be a valid program.
share|improve this question
1  
^G aka system bell or OS beep may be suppressed on many systems too (e.g. switched to be a visible "bell") and should be handled like sounds of popus and so on too: Not a valid solution. –  yeti Jun 17 at 21:43
    
Every program that prints "\a" to a terminal fails if xterm uses a visible bell. –  kernigh Jun 18 at 23:42
4  
rm / will cause a lot of screaming. –  cjfaure Jun 22 at 17:23

68 Answers 68

up vote 19 down vote accepted

dc, 2 chars

this one prints the bell character, too:

7P

Run with $ dc <<< 7P or save 7P to file and run $ dc file.

Also:

Befunge, 2 chars

Similar but infinitly looping and beeping:

7,
share|improve this answer
1  
Could you please elaborate what language that is? I never heard about it, and its name is too short (and a very common abbreviation) to be efficiently searched for. –  vsz Nov 24 '12 at 17:35
3  
From the man-page: dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. -- it's included in most linux/unix distros as far as I know. –  daniero Nov 24 '12 at 20:12
    
A few more examples if you're interested: 1, 2 and 3. Seen a few others use it here as well. –  daniero Nov 24 '12 at 20:20
9  
@vsz dc is – of course – turing complete, as any better calculator such as ghc should be. –  FUZxxl Dec 3 '12 at 21:54
1  
All sounds can be disabled. Just turn the volume down. –  daviewales Jun 18 at 8:53

bash (13)

sudo rm -rf /

The faster the hard drive the better the sound. Don't work with SSDs.
(Don't try this at home, sudo rm -rf / erases everything on your hard drive)

share|improve this answer
6  
I think that one should come with a disclaimer -- "Don't try this at home" :D –  daniero Nov 24 '12 at 20:28
13  
Yeah, that's the sound when your computer *r*eads your *m*ail *r*eally *f*ast (rm -rf). –  FUZxxl Dec 3 '12 at 22:27
56  
Wait, you got it wrong. The task is to make the computer produce a sound, not the user... though I have to admit that scream was quite impressive... –  leftaroundabout Dec 3 '12 at 22:51
2  
It doesn't do anything without --no-preserve-root, except maybe on ancient systems. –  nyuszika7h Jun 9 at 14:58
1  
@nyuszika7h ancient as in before 2005. –  Jwosty Jun 16 at 22:21

*sh (5)

eject

(does not work if you have no CD/DVD or similar drive..)

share|improve this answer
    
I like this one. But, on my Mac (Mac OS X), the command is not found : sh: eject: command not found –  Nicolas Barbulesco Jun 7 at 20:27
2  
Nicolas: right, on Mac, the eject "command" seems to be much shorter: (: –  mykhal Jun 10 at 7:59
    
@ mykhal — If you go this way, I have a solution that works on all modern Macs : 🔊 –  Nicolas Barbulesco Jun 11 at 21:44
1  
This fails with OpenBSD. usage: eject [-t] device The device argument is not optional! The shortest that works here is eject cd0 for 9 characters. Also, if the drive has no tray (as with a slot-loading drive), then eject cd0 makes no noise unless a disc is in the drive. –  kernigh Jun 18 at 23:32
1  
kernigh: you're right. on the other hand, it might succeed on many systems without a sound card or speaker, where most of the other solutions would fail –  mykhal Jun 19 at 14:36

Befunge, 0

If I read the spec correctly, this is an endless loop.
Endless loop = you'll hear your CPU cooler spin up.

share|improve this answer

brainfuck: 8

+++++++.

Prints the bell character.

share|improve this answer
12  
+1 BF Solution both understandable AND shorter than C. –  OldCurmudgeon Nov 18 '12 at 0:27
12  
A brainfuck is easier to mentally digest than C... we should learn from this incident. –  Camilo Martin Dec 18 '12 at 14:10

sh 7

w|aplay

Plays a short noise.

Also 7:

aplay *

Assembly

Another non-bell solution. Resulting binary is just 15 bytes.

mov al, 182
out 43h, al
out 42h, al
mov al, 16
out 42h, al
mov al, 3
out 61h, al
ret

Assemble with nasm sound.asm -o sound.com. Can be tried with dosbox sound.com.

share|improve this answer
7  
+1 for not just relying on a terminal to translate BEL characters to beeps. –  Ilmari Karonen Nov 18 '12 at 12:28

bash (Mac OS X) 5

say a

although stylistically I prefer the somewhat longer:

say 'Hello, Code Golf !'
share|improve this answer
    
If your Mac is set to English, you could also do say 'hahahahahahahahahahahahahahahahahahahahahahahahahaj'. –  tomsmeding Dec 29 '12 at 14:23
16  
Oooh... I've got ssh access to an osx box... –  boothby May 4 '13 at 17:43
1  
This command works in AppleScript. I did not know it works in the Terminal too. Funny with ssh ! –  Nicolas Barbulesco Jun 7 at 20:30
1  
Corollary on Linux: espeak... –  daviewales Jun 18 at 8:57

Haha, good ol' QBASIC code.

BEEP
share|improve this answer
    
Ah, the fun that could be had with the PLAY command! For reminiscence, see e.g. this video. –  Jussi M Mar 12 at 19:19
    
I'm 15, xD. We learned QBASIC in school. –  Soham Chowdhury Mar 14 at 8:29

Golfscript 3

'.'

(where the . is in fact a BELL character)

The HEX representation of the above code is:

27 07 27
share|improve this answer
2  
Ouch, Golfscript strikes again! –  vsz Nov 17 '12 at 18:27

Python 3.3.3, one character

<bell character>

The error message will contain a bell character, causing the sound.

Python 3.3.3, 10 characters

If errors are not allowed, this solution won't output the error message, only the sound.

print('<bell character>')
share|improve this answer
    
Why the downvote, this code actually produces a sound. –  Koops Jan 1 at 18:14
    
1 char O_o nice +1 –  Timtech Jan 2 at 1:38

Java - 222

Enough of this bell character or predefined beep function stuff, this makes a real sound :)

import javax.sound.sampled.*;class S{static{try{SourceDataLine
l=AudioSystem.getSourceDataLine(new
AudioFormat(4000,8,1,0<1,0>1));l.open();l.start();for(byte
i=9;i!=0;i+=9)l.write(new byte[]{i},0,1);}catch(Exception e){}}}
share|improve this answer
15  
Java truly sucks for golf –  boothby May 4 '13 at 17:44
    
@boothby not always, check this out –  aditsu May 6 '13 at 1:07
17  
Okay, Java sucks for golf except when the challenge is to throw errors. ;) –  boothby May 6 '13 at 1:47
    
@boothby What about this? codegolf.stackexchange.com/a/28818/24965 –  Jop Vernooij Jun 11 at 14:25
1  
@JopVernooij That's not golf, that's an underhanded popularity contest. –  Jwosty Jun 16 at 22:26

Pascal: 30 characters

uses Crt;begin
Sound(999)
end.

This takes advantage of the lack of any rule regarding the sound's duration. So just turns on the speaker on 999 Hz and lets it so. (At least until another program calls Nosound or the computer is turned off.)

share|improve this answer
    
And the sound keeps screaming even after the program has quit ? I like this one ! :-) –  Nicolas Barbulesco Jun 22 at 16:35
    
@NicolasBarbulesco, exactly. That was the normal behaviour of all programs on DOS, including those written in C or assembly. –  manatwork Jun 23 at 9:46

DOS Prompt / DOS Script

a:

Requires 3.5" floppy drive :)

share|improve this answer
    
What magic is this? –  Sieg Jun 8 at 21:44
1  
It causes DOS to try to read from the floppy, which does cause a sound. It is, however, wrong that this needs a 3.5" floppy drive; a 5.25" floppy drive works even better. –  celtschk Jun 15 at 14:02
    
Aye. Wouldn't the 5.25" normally be b:? –  svidgen Jun 15 at 15:26
1  
Try a CD drive with a defective disk inside, works great too :) –  Alejandro Jun 15 at 17:53

Mathematica 6

This will evoke the system beep. On my computer, it is currently a chirp.

Beep[]
share|improve this answer

J (2)

a.

This prints all the characters from 0 to 255, that includes the bell.

If I have to do it without printing anything else, it's 4 characters:

7{a.
share|improve this answer

cat/type/PHP 1

This is actually polyglot. It runs in cat (or Microsoft Cat called type) and PHP.

$ xxd file
0000000: 07                                       .

Execution:

$ xxd -r > file
0000000: 07                                       .
^D
$ cat file # cat could be replaced with type (on Windows) or with php
share|improve this answer
    
What is the program ? How to run it ? This is not clear. –  Nicolas Barbulesco Jun 7 at 20:36
    
@NicolasBarbulesco: The program is one byte (\x07), however because it's binary, I cannot paste it here. You can run it in PHP CLI, with php. –  xfix Jun 8 at 7:01
    
It is not clear. But it works ! With cat. The trick is the ASCII char 7 “bell”. –  Nicolas Barbulesco Jun 8 at 15:48

Bash, 22

Back in the 90's, my brother taught me this one -- to be used in a computer lab where one has remote access and knows a person to be alone in said lab. Kids these days won't know what a computer lab is... but oh well.

cd /dev;cat sda1>audio
share|improve this answer
1  
I get bash: audio: Permission denied even running under sudo. :( –  Riking May 5 '13 at 4:46
1  
This worked on Debian as of 2006. I can't get it to work on my Ubuntu box :*( –  boothby May 5 '13 at 15:41
1  
I think you mean dsp, not audio. See below for a similar example I wrote using /dev/random –  user12447 Jan 2 at 1:42
3  
or cat /dev/sda | aplay –  TheDoctor Mar 4 at 0:23
1  
@TheDoctor or aplay</dev/sda (UUOC ;-)) –  DigitalTrauma Jun 12 at 17:06

brainfuck, 5 bytes

+[.+]

prints all chars including bell

share|improve this answer

DOS, 5

Indirect, requires user cooperation.

pause
share|improve this answer
1  
Are you sure? I can not remember any beeping version of DOS pause. (At least not MSDOS and FreeDOS.) –  manatwork May 4 '13 at 9:42
5  
Run the program. Cooperate. Listen. –  boothby May 4 '13 at 17:36
2  
@Riking, how that conforms with the “Your program has to make the computer produce a sound” part of the requirement? –  manatwork May 5 '13 at 9:52
5  
I do not understand programmers on puzzle sites who cannot think outside of the box. –  boothby May 5 '13 at 18:20
4  
I wasn't criticizing your answer. I was criticizing the question. A pointless question will obviously get pointless answers. Your answer then make as much sense as any other answer to that stupid question. So, i'm giving you +1. –  user8059 May 6 '13 at 2:00

R 9

The bell character

cat("\a")
share|improve this answer

Ruby, 7

$><<?\a

Tested on Windows with Ruby 1.9.3.

share|improve this answer

Brainfuck (4)

+[.+]

Prints every character from \x01 to \xFF, including the bell character.

share|improve this answer
    
Upvote! Way more elegant than the other Brainfuck solution! –  flawr Jun 18 at 6:09
1  
Why "(4)"? Am I unable to count? –  yeti Jun 21 at 16:19

PowerShell, 24 17

Since nobody has posted this version yet:

[console]::beep()

UPDATE: Removed frequencies so it uses the default frequency and duration (MSDN says the default is 800 hertz and 200 milliseconds)

share|improve this answer
    
Surely you could at least use smaller numbers –  Dennis Jaheruddin Jan 13 at 17:06

><> 3

As in all answers, prints the bell (0x07) to stdout.

7o;
share|improve this answer

Python 9 chars


produces beep sound when executed.

print'\a'
share|improve this answer
1  
You can shorten this to 8 characters if you replace \a with the actual bell character (ASCII code 7). –  flornquake May 3 '13 at 16:37
    
But how to print with the help of the value 7 ? –  Anbarasan May 6 '13 at 3:55
1  
You can type the bell character into your editor with Alt+07. Alternatively, you can run this snippet to create a python file with the 8-character solution: open("play_a_sound.py", 'w').write("print'"+chr(7)+"'") –  flornquake May 6 '13 at 11:33
    
Got it now. Thanks ! –  Anbarasan May 7 '13 at 3:55

Tcl, 1 char

-

replace - with the bell char.
It will print the bell char as part of the error message. Twice.

And if you think this is not a valid program, add this before the bell char:

proc \007 {} {};

It is a valid program, I just did not define the command, because I like the error message.

share|improve this answer
    
Is that based on some Tcl 8.6 feature? In Tcl 8.5 I got different result: pastebin.com/bh73UF6F Or are you on Windows? –  manatwork May 3 '13 at 15:19
    
No. The 2nd part should show that this is a valid Tcl file. And it should beep 2x (the error message contains the command to be executed twice). Calling a command that does not exist is not an error for Tcl, it is only one if it can not find it. (e.g. parray, which is loaded on the first use) –  Johannes Kuhn May 3 '13 at 16:28
    
Oops. I misunderstood you. I thought you mean, it should beep with the proc declaration too. (Regarding the beep count, probably my terminal is doing something as puts [string repeat ".\a" 10] displays 10 dots but beeps only once.) –  manatwork May 3 '13 at 16:37

C: 19

main(){puts("\a");}

edit: header file is not needed for "puts"

share|improve this answer
2  
You can remove the space between #include and < :) –  tomsmeding Nov 18 '12 at 8:13
    
puts(char*);main(){puts("\a");} is smaller IMHO. –  FUZxxl Dec 3 '12 at 22:26
    
You can just remove the #include. –  ugoren Dec 4 '12 at 8:10
1  
@FUZxxl How can you have an opinion about code size? –  Doorknob Dec 22 '13 at 0:24
    
@DoorknobofSnow Because of the first amendment? –  FUZxxl Dec 22 '13 at 0:35

Applescript (4 Characters)

beep

This will play the default beep sound of OS X.

share|improve this answer

top & aplay - 9

top has rhythm!

top|aplay

The periodic display sometimes does not feed the sound sink fast enough but aplay continues after grouching a bit... :-)

...this will run on many Linuxes...

Bonus:

While running this, type "s 1 ENTER" to get more beats per second. :-)

Party on!!! \o/

share|improve this answer

Ruby 8

 puts"\a"

Tested on Windows with Ruby 1.9.3.

share|improve this answer

protected by Community Jun 17 at 22:19

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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