How do I make my program beep.

Haseen I. Alam halam2 at umn-d-ub.D.UMN.EDU
Sat Jun 9 02:15:11 AEST 1990


In article <43605 at ism780c.isc.com> marv at ism780.UUCP (Marvin Rubenstein) writes:
>In article <3168 at goanna.cs.rmit.oz.au> ok at goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>>(2) #define mybeep putchar('\7')
>>    has a fairly serious defect:  output to stdout is often buffered or
>>    line-buffered, and '\7' is not a complete record.  You need
>>	#define mybeep (putchar('\7'), fflush(stdout))
>>
>
>putchar('\7') has an even more serious defect.  The reason for beeping is
>to notify the user of the program of something while the program is
>*running*. There is no guarantee that sending a '\7' to stdout will cause an
>beep.  Stdout may well be a diskfile.  The output must be directed to a
>device that will make a noise that the user can hear.  I know of no portable
>way to do this.
>
>    Marv Rubinstein

  How about using fprintf to print to stderr?  A non-ANSI example follows.
  Even if you redirect the output to a file it will still beep.  But probably
  will not if you send it to /dev/null.  I know a lot of people like to
  have it as a macro, but I have adopted the following, mainly because I can
  specify how many beeps I want in a call, and 3 in a row almost makes a 
  melody!!!!

  Haseen.

  BTW: a BEEP in the right time will save a lot of cries.


/*  Here comes the three beepers...
    BEEP-BEEP-BEEP-I-RAY!!!!!!!!			 	*/

#include <stdio.h>
/* #define BELL '\a'		/* use it if you got it 	*/
#define BELL '\007'		/* or live with what you have 	*/

void beep (times)
    int times ;
{
    int i, j ;
    for (i=0; i < times; i++)
    {
	fprintf (stderr, "%c", BELL) ;
	for (j=0; j < 100000; j++) ;		/* just a delay */
    }
    fflush (stderr) ;
}

main ()
{
    beep (3) ;
}
-- 
 .--------------------------------------------------------------------.
 |  Haseen Ibne Alam                       Tel: (218)-728-2139        |
 |  email : halam1 at ub.d.umn.edu      or    halam at cola.d.umn.edu       |
 `--------------------------------------------------------------------'



More information about the Comp.lang.c mailing list