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
add comment

45 Answers

up vote 9 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
    
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
    
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
    
My bad for not elaborating what I mean under "language" –  vsz Nov 30 '12 at 11:40
8  
@vsz dc is – of course – turing complete, as any better calculator such as ghc should be. –  FUZxxl Dec 3 '12 at 21:54
add comment

Bash, 3

yes

Doesn't work on CPUs with passive heat sink. If you don't like the output to stdout it causes as a "side-effect", use

Bash, 13

yes>/dev/null

If you have too many cores you might need a multithreaded version, e.g.

Bash, 39

for((x=0;x<4;++x))do yes>/dev/null&done

Of course, none of these programs terminate, but you didn't specify the sound needed to have a finite duration...

share|improve this answer
18  
«Doesn't work on CPUs with passive heat sink.» made my day... –  FUZxxl Dec 3 '12 at 22:25
    
This is great, my favourite solution so far. –  J.Ashworth Dec 3 '12 at 23:53
    
Why does this work? –  Joe May 6 '13 at 6:45
    
add comment

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 / earses everything on your hard drive)

share|improve this answer
2  
I think that one should come with a disclaimer -- "Don't try this at home" :D –  daniero Nov 24 '12 at 20:28
6  
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
30  
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
add comment

sh (5)

eject

(works only if you have CD/DVD or similar drive..)

share|improve this answer
add comment

brainfuck: 8

+++++++.

Prints the bell character.

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

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
add comment

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
5  
+1 for not just relying on a terminal to translate BEL characters to beeps. –  Ilmari Karonen Nov 18 '12 at 12:28
add comment

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
11  
Oooh... I've got ssh access to an osx box... –  boothby May 4 '13 at 17:43
add comment

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
1  
Ouch, Golfscript strikes again! –  vsz Nov 17 '12 at 18:27
add comment

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
add comment

Mathematica 6

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

Beep[]
share|improve this answer
add comment

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
add comment

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
add comment

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
add comment

R 9

The bell character

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

Ruby, 7

$><<?\a

Tested on Windows with Ruby 1.9.3.

share|improve this answer
add comment

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
11  
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
9  
Okay, Java sucks for golf except when the challenge is to throw errors. ;) –  boothby May 6 '13 at 1:47
add comment

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
add comment

brainfuck, 5 bytes

+[.+]

prints all chars including bell

share|improve this answer
add comment

><> 3

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

7o;
share|improve this answer
add comment

DOS, 5

Indirect, requires user cooperation.

pause
share|improve this answer
    
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
2  
Run the program. Cooperate. Listen. –  boothby May 4 '13 at 17:36
1  
@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
2  
I do not understand programmers on puzzle sites who cannot think outside of the box. –  boothby May 5 '13 at 18:20
3  
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
show 5 more comments

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
show 1 more comment

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
    
I get bash: audio: Permission denied even running under sudo. :( –  Riking May 5 '13 at 4:46
    
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
    
I think you mean dsp, not audio. See below for a similar example I wrote using /dev/random –  Lee Jan 2 at 1:42
    
or cat /dev/sda | aplay –  TheDoctor Mar 4 at 0:23
add comment

sh 6

echo .

(where the . is in fact a BELL character)

The HEX representation of the above code is:

65 63 68 6f 20 07
share|improve this answer
add comment

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
add comment

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
add comment

PowerShell, 24

Since nobody has posted this version yet:

[console]::beep(200,500)
share|improve this answer
    
Surely you could at least use smaller numbers –  Dennis Jaheruddin Jan 13 at 17:06
add comment

Ruby 8

 puts"\a"

Tested on Windows with Ruby 1.9.3.

share|improve this answer
add comment

Autoit 6

Beep()

Although declaring a function would allow for less characters, not counting the declaration:

z()
Func z()
Beep()
EndFunc
share|improve this answer
2  
That way that you made the program longer? It didn't make it shorter. –  boothby May 4 '13 at 17:45
    
Writing own functions count toward the the length. otherwise everything would be a how can I call the method with less chars and some languages allow that with 1 char. –  Johannes Kuhn May 10 '13 at 0:15
add comment

ATARI BASIC (4 chars)

?"E"

(? is short for "print". E is in fact the "escape" symbol; see http://raster.infos.cz/atari/chars/atascii.htm, item 1B. Printing this symbol would make a characteristic noise.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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