[comp.lang.c] How do I make my program beep.

halam2@umn-d-ub.D.UMN.EDU (Haseen Alam) (05/26/90)

 
 Hi,

 The K&R says that '\a' is an alert (audible bell), but how do I use it to
 make my program beep?  All my attemps were vain!!

 Please reply via email if possible.  Thanks in advance.

 Haseen.
 5/26/90

 email:	halam1@ub.d.umn.edu
	halam@umnd-cpe-cola.d.umn.edu

henry@utzoo.uucp (Henry Spencer) (05/27/90)

In article <3472@umn-d-ub.D.UMN.EDU> halam2@umn-d-ub.D.UMN.EDU (Haseen Alam) writes:
> The K&R says that '\a' is an alert (audible bell), but how do I use it to
> make my program beep?  All my attemps were vain!!

On what machine?  You should ask this on a machine-specific newsgroup, like
say comp.sys.ibm.pc.programmer.  How you get a beep is *highly* hardware-
dependent.  My Sun-3/180 can't beep at all (although most of the terminals
attached to it can).
-- 
Life is too short to spend    |     Henry Spencer at U of Toronto Zoology
debugging Intel parts. -Van J.| uunet!attcan!utzoo!henry henry@zoo.toronto.edu

e89hse@rigel.efd.lth.se (05/28/90)

In article <1990May27.000808.13551@utzoo.uucp>, henry@utzoo.uucp (Henry Spencer) writes:
>In article <3472@umn-d-ub.D.UMN.EDU> halam2@umn-d-ub.D.UMN.EDU (Haseen Alam) writes:
>> The K&R says that '\a' is an alert (audible bell), but how do I use it to
>> make my program beep?  All my attemps were vain!!
>
>On what machine?  You should ask this on a machine-specific newsgroup, like
>say comp.sys.ibm.pc.programmer.  How you get a beep is *highly* hardware-
>dependent.  My Sun-3/180 can't beep at all (although most of the terminals
>attached to it can).
>-- 
 Yyes is is hardwared dependent, but on most machines ^G (ascii 7) will do it.

 Henrik Sandell

michelbi@oregon.uoregon.edu (05/29/90)

In article <1990May27.000808.13551@utzoo.uucp>, henry@utzoo.uucp (Henry Spencer) writes:
> In article <3472@umn-d-ub.D.UMN.EDU> halam2@umn-d-ub.D.UMN.EDU (Haseen Alam) writes:
>> The K&R says that '\a' is an alert (audible bell), but how do I use it to
>> make my program beep?  All my attemps were vain!!

Your best bet is to       printf("\a");  or   printf("\c", 7);  I think.

I hope this helps

Michel Biedermann	michelbi@oregon.uoregon.edu
U. of Oregon

pfalstad@phoenix.Princeton.EDU (Paul John Falstad) (05/29/90)

In article <19620.26610767@oregon.uoregon.edu> michelbi@oregon.uoregon.edu writes:
>Your best bet is to       printf("\a");  or   printf("\c", 7);  I think.

Don't you mean %c instead of \c?  Actually, \a or \x7 work better.

-- 
Paul Falstad  PLINK:Hypnos GEnie:P.FALSTAD net:pfalstad@phoenix.princeton.edu
Disclaimer: My opinions, which belong to me and which I own, are mine.
-Anne Elk (not AN elk!)  The sun never set on the British empire because
the British empire was in the East and the sun sets in the West.

harrison@necssd.NEC.COM (Mark Harrison) (06/01/90)

In article <1990May27.000808.13551@utzoo.uucp>,
henry@utzoo.uucp (Henry Spencer) writes:
> In article <3472@umn-d-ub.D.UMN.EDU>
  halam2@umn-d-ub.D.UMN.EDU (Haseen Alam) writes:

> > The K&R says that '\a' is an alert (audible bell), but how do I use it to
> > make my program beep?  All my attemps were vain!!

> On what machine?  You should ask this on a machine-specific newsgroup, like
> say comp.sys.ibm.pc.programmer.  How you get a beep is *highly* hardware-
> dependent.  My Sun-3/180 can't beep at all (although most of the terminals
> attached to it can).

I think Haseen was asking why the program

	main() { printf("hello world\a\n"); }

prints
	"hello worlda"

instead of
	"hello world" and then beeping.

The '\a' alarm sequence is a new feature.  Old fashioned compilers (read:
most Unix compilers) don't support this, but on ascii machines, you can
encode an octal seven (the "bel" character) to do the same thing:  Of
course your terminal/computer has to have the beeping hardware to make
the noise.

	main() { printf("hello world\7\n"); }
-- 
Mark Harrison             harrison@necssd.NEC.COM
(214)518-5050             {necntc, cs.utexas.edu}!necssd!harrison
standard disclaimers apply...

halam2@umn-d-ub.D.UMN.EDU (Haseen Alam) (06/06/90)

>I think Haseen was asking why the program
>
>       main() { printf("hello world\a\n"); }
>
>prints
>       "hello worlda"
>
>instead of
>       "hello world" and then beeping.
>
 [stuff deleted ...]
>
>       main() { printf("hello world\7\n"); }


 Thank you Mark, you just hit the bulls eye.  Maybe it was a trivial question
 for most C programmers.  But apparantly there are a few of us who are still
 novice, and the net seems a relatively efficient way to get answers to most
 questions.  Specially if the books and the local guru's are of no help.
 
 One other quick question.  Will '\7' still work on an ANSI compiler?
 
 On behalf of me and others that I've helped with this same question, thanks
 again to all the guys who replied.  Bye.                                   
                                                                            
 Haseen. 
 .--------------------------------------------------------------------.
 |  Haseen Ibne Alam                       Tel: (218)-728-2139        |
 |  email : halam1@ub.d.umn.edu      or    halam@cola.d.umn.edu       |
 `--------------------------------------------------------------------'
-- 
 .--------------------------------------------------------------------.
 |  Haseen Ibne Alam                       Tel: (218)-728-2139        |
 |  email : halam1@ub.d.umn.edu      or    halam@cola.d.umn.edu       |
 `--------------------------------------------------------------------'

dankg@tornado.Berkeley.EDU (Dan KoGai) (06/06/90)

In article <3519@umn-d-ub.D.UMN.EDU> halam2@ub.d.umn.edu (Haseen Alam) writes:
>
>>       main() { printf("hello world\7\n"); }
>
> Thank you Mark, you just hit the bulls eye.  Maybe it was a trivial question
> for most C programmers.  But apparantly there are a few of us who are still
> novice, and the net seems a relatively efficient way to get answers to most
> questions.  Specially if the books and the local guru's are of no help.

	It is not a very good idea to make your machine beep via printf().
ASCII 007 is indeed beep but it's just a remainder of old terminal days--this
doesn't work Macintosh and other sophisticated machine--X window has a
capability of suppressing beeps and substitute it to screen flash.  You'd
better write a function|macro to do the task.  It also helps porting over the
system.  Many machines have distinct beep() functions and some can even use
various beeps, such as Macintosh (Mine is Bill and Ted shouting "Exellent!").
	So your function should be:

/* #ifdef UNIX */
#define RETURN '\n' /* for portability. \r for Macintosh, et al */
#define mybeep beep() putchar(\07)	/* Also for portability */
/* endif */

main(){
	....
	printf("hello, world%c", RETURN);
	mybeep();
} 
> One other quick question.  Will '\7' still work on an ANSI compiler?

	Yes it does.  But usually we use octal or hexadecimal for covenience
but any digit followed after '\' is treated as corresponding character.
(ASCII, EBCDIC, whatever).  It can be decimal, octal, or hexadecimal

----------------
____  __  __    + Dan The "Exellent!" Man
    ||__||__|   + E-mail:	dankg@ocf.berkeley.edu
____| ______ 	+ Voice:	+1 415-549-6111
|     |__|__|	+ USnail:	1730 Laloma Berkeley, CA 94709 U.S.A
|___  |__|__|	+	
    |____|____	+ "What's the biggest U.S. export to Japan?" 	
  \_|    |      + "Bullshit.  It makes the best fertilizer for their rice"

karl@haddock.ima.isc.com (Karl Heuer) (06/07/90)

In article <1990Jun6.124609.7316@agate.berkeley.edu> dankg@tornado.Berkeley.EDU (Dan KoGai) writes:
>but any digit followed after '\' is treated as corresponding character.
>It can be decimal, octal, or hexadecimal

There is no decimal escape.  Just octal (up to three digits) and hex
(unlimited length).

Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (06/07/90)

In article <1990Jun6.124609.7316@agate.berkeley.edu>, dankg@tornado.Berkeley.EDU (Dan KoGai) writes:
> /* #ifdef UNIX */
> #define RETURN '\n' /* for portability. \r for Macintosh, et al */
> #define mybeep beep() putchar(\07)	/* Also for portability */
> /* endif */

(1) Isn't \n supposed to be mapped to whatever the local end of line
    convention is?  Is it legal for a Mac C compiler to map \n to
    anything other than CR, or to something which will have the same
    effect when output?

(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))

(3) This is better as a function than a macro anyway.
-- 
"A 7th class of programs, correct in every way, is believed to exist by a
few computer scientists.  However, no example could be found to include here."

news@ism780c.isc.com (News system) (06/08/90)

In article <3168@goanna.cs.rmit.oz.au> ok@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

halam2@umn-d-ub.D.UMN.EDU (Haseen I. Alam) (06/08/90)

In article <43605@ism780c.isc.com> marv@ism780.UUCP (Marvin Rubenstein) writes:
>In article <3168@goanna.cs.rmit.oz.au> ok@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@ub.d.umn.edu      or    halam@cola.d.umn.edu       |
 `--------------------------------------------------------------------'

dankg@tornado.Berkeley.EDU (Dan KoGai) (06/09/90)

In article <43605@ism780c.isc.com> marv@ism780.UUCP (Marvin Rubenstein) 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.

	You are absolutely right:  Even using fputc(stderr, '\7'), some shells
can still put stderr to files without prob.  fputc(/dev/tty, '\7') ? maybe.
	Still it's not very good idea to beeb via ascii beep:  Such machines
as Macintosh have very extensive sound package and maybe other program is using
sound port alread (Mac has 4 channels though).  Suppose you are plaing Loud
music with CD-ROM driver and you want to beep.  What you you do?  Find unused
soundport and beep on the background which might be ignored due to other sound?
or you halt other programs, stop other sound and beep?  There are many
possibilities.
	IMHO, warning by ascii beep is obsolete technique, with machines with
far more complex and capability of handling sound.

----------------
____  __  __    + Dan The "beep("Don't Beep, man!")" Man
    ||__||__|   + E-mail:	dankg@ocf.berkeley.edu
____| ______ 	+ Voice:	+1 415-549-6111
|     |__|__|	+ USnail:	1730 Laloma Berkeley, CA 94709 U.S.A
|___  |__|__|	+	
    |____|____	+ "What's the biggest U.S. export to Japan?" 	
  \_|    |      + "Bullshit.  It makes the best fertilizer for their rice"

simon.ewins@f664.n250.z1.fidonet.org (simon ewins) (06/10/90)

> In article <3168@goanna.cs.rmit.oz.au> ok@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.
 
 
How about...
 cprintf("\a");
or
 fprintf(stderr,"\a");
 
. 

--- D'Bridge 1.30/002506
 * Origin: A_X_A_X_A  [ FactBase/qDos <> 416-483-2821 ] (1:250/664)

exspes@gdr.bath.ac.uk (P E Smee) (06/11/90)

In article <43605@ism780c.isc.com> marv@ism780.UUCP (Marvin Rubenstein) writes:
>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.

Only things certain in life are death and taxes.  However, I'd say you
could maximize your changes by writing the '\7' to stderr rather than
stdout; or, even better, to /dev/tty.  With flushes as required.

-- 
Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK
 P.Smee@bristol.ac.uk - ..!uunet!ukc!bsmail!p.smee - Tel +44 272 303132

meissner@osf.org (Michael Meissner) (06/11/90)

In article <90061017513753@masnet.uucp>
simon.ewins@f664.n250.z1.fidonet.org (simon ewins) writes:

| > In article <3168@goanna.cs.rmit.oz.au> ok@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.
|  
|  
| How about...
|  cprintf("\a");
| or
|  fprintf(stderr,"\a");

Just as stdout may be a diskfile, so can stderr (ie, with 2>file in
the Bourne Shell, or >&file in the C-shell).  There may be no terminal
attached to the process, or the process may be in the backround, in
which case it may get a stop signal upon attempt to write to the
terminal.

If you really want to write a '\7' to the terminal, open /dev/tty.  Be
prepared for some user somewhere to complain because s/he was running
a modem program in the foreground, and the program was connected to
GNU emacs, and the '\007' caused it to abort the current
operation.....

--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA

Catproof is an oxymoron, Childproof is nearly so

karl@haddock.ima.isc.com (Karl Heuer) (06/11/90)

In article <90061017513753@masnet.uucp> simon.ewins@f664.n250.z1.fidonet.org (simon ewins) writes:
>> In article <3168@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>>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.

Of course not; there's no guarantee that there's *any* beepable device
accessible to the current program.

>How about...  cprintf("\a");

Not portable.  This is some vendor-specific hack.
(Also the "\a" isn't portable to pre-ANSI, but this can be worked around.)

>or  fprintf(stderr,"\a");

Using stderr is probably best.  The user could have redirected the error
output away from the terminal, but if so, it's very possible that he doesn't
want the beep on the terminal either.

Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint

rob@baloo.eng.ohio-state.edu (Rob Carriere) (06/12/90)

In article <16844@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer)
writes: 
[beeping on the terminal]
>Using stderr is probably best.  The user could have redirected the error
>output away from the terminal, but if so, it's very possible that he doesn't
>want the beep on the terminal either.

Thank you.  I have been irritated by more than one program that tried to
decide for me what I wanted.

If your user has directed your output to files, it means she doesn't want to
hear from you.

SR
"Just run perfmon on a SUN under X11 and tell me how you like _that_"
(be prepared to reboot!)
---

jak@sactoh0.UUCP (Jay A. Konigsberg) (06/12/90)

The following was extracted from the current release of Gnu Awk 2.11.1.
I have only had a short amount of time to examin it, but it looks like
it is worthwhile to post.

X/* Figure out what '\a' really is. */
X#ifdef __STDC__
X#define BELL	'\a'		/* sure makes life easy, don't it? */
X#else
X#	if 'z' - 'a' == 25	/* ascii */
X#		if 'a' != 97	/* machine is dumb enough to use mark parity */
X#			define BELL	'\207'
X#		else
X#			define BELL	'\07'
X#		endif
X#	else
X#		define BELL	'\057'
X#	endif
X#endif

-------------------------------------------------------------
Jay @ SAC-UNIX, Sacramento, Ca.   UUCP=...pacbell!sactoh0!jak
If something is worth doing, its worth doing correctly.

arromdee@crabcake.cs.jhu.edu (Kenneth Arromdee) (06/12/90)

In article <3265@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes:
>The following was extracted from the current release of Gnu Awk 2.11.1.

Wouldn't it be a bad idea to use that code in many cases, since the Gnu
license would require you have to give out the source code to your own
program then?
--
"And they shall be cast out where there is no outlet for their evil doings..."
	-- the Book of Ubizmo, on sinful uses of electricity

Kenneth Arromdee (UUCP: ....!jhunix!arromdee; BITNET: arromdee@jhuvm;
     INTERNET: arromdee@crabcake.cs.jhu.edu)

peter@ficc.ferranti.com (Peter da Silva) (06/13/90)

In article <3265@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes:
> The following was extracted from the current release of Gnu Awk 2.11.1.

[ code to determine bell character deleted ]

Um... why would "awk" need to know what the BEL character is?

[ Followups *out* of comp.lang.c ]
-- 
`-_-' Peter da Silva. +1 713 274 5180.  <peter@ficc.ferranti.com>
 'U`  Have you hugged your wolf today?  <peter@sugar.hackercorp.com>
@FIN  Dirty words: Zhghnyyl erphefvir vayvar shapgvbaf.

karl@haddock.ima.isc.com (Karl Heuer) (06/13/90)

In article <6439@crabcake> arromdee@crabcake.cs.jhu.edu (Kenneth Arromdee) writes:
>In article <3265@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes:
>>The following was extracted from the current release of Gnu Awk 2.11.1.
>
>[Isn't that copylefted code, then?]

The following code fragment is in the public domain.

#if '\a' != 'a'
#define BEL '\a'
#else
#define BEL '\7'
#endif
#define ringbell() putc(BEL, stderr)