开发者

This program sounds the bell!

I'm a brand-new student in programming arena so I can't grasp this program written in my book that I have been following for a few days. The program is like this:

#include "stdio.h"

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

What does this program mean? Does this program mean that we cou开发者_C百科ld hear a ringing bell? I can't hear any ringing bell sound!!!


ASCII character 7 is the BELL character, and it's represented in C as \a. Some terminals will produce a beep when this character is output on the terminal; nowadays, many don't. (I'm looking at you, Ubuntu.)


Back in the dark ages when ASCII was codified out of the ashes of BAUDOT, a terminal was a large chunk of iron that hammered ink onto paper, often included a paper tape punch and reader, and interpreted keystrokes to generate an asynchronous serial signal at a few hundred baud with spinning wheels and relays.

In case an operator fell asleep to the soothing noises of it hammering out text, it had an actual bell it could ring. The character coded 007 in octal, 0x07 in hex, or as \a in a C character or string constant rang the bell when received.

As terminals became smaller and implemented with few or no moving parts, the physical bell was replaced by a beeper.

Exactly what your terminal emulator (aka a Console Window in Windows, xterm or something similar in Unix) does when it is asked to display that control character is not well standardized today. It ought to make a noise or flash the window, but your mileage will vary.


Have a look at this wikipedia entry: bell character:

In the C Programming Language (created in 1972), the bell character can be placed in a string or character constant with \a ('a' stands for "alert" or "audible" and was chosen because \b was already used for backspace).


\a does in fact trigger the system chime. It's the escape sequence for the ASCII BEL character.


You'll hear a beep from your PC's internal speaker (not the external speakers or headphones you may have attached).


Strings can contain characters which are handled different from all the other characters. The most often explicit used one is '\n'. The '\n' character does not print a character in the console, instead it tells the console to start a new line. Such special characters are called non printable since they have no own visible representation in c and have to use escape sequences instead.

In the escape sequence "\a" the backslash before the a tells the compiler that the a is an identifier for a special character and will store its char-value instead of the char-value of 'a'.

The '\a' escape sequence is the audible bell character, giving this character to a console via print() should cause a beep sound. Some consoles wont beep.

Here are some special characters, the link is from a c++ reference but most should be valid for c.


\a is the C representation of the ASCII audible alert ("bell") control character. On an old-school serial terminal, outputting that character produced a "beep" sound. Your terminal emulator may or may not implement this feature.


Apart from all the answers you've got, take into account that your program won't probably compile. Here is the fixed version:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("\a");
    return EXIT_SUCCESS;
}

The most important change is that system headers must be surronded with < and >, instead of quotes. Also, it is better to know that the main() function always returns an int (to the operating system), and that this int is coded in two constants, EXIT_SUCCESS, and EXIT_FAILURE, in the header stdlib.h


Try something simpler:

printf("hello\tworld");
printf("hello\nworld");

and see what happens.

Your example with the BELL char, as others have pointed out, probably won't work on today's toasters^H^H^H^H^H^H^H^H computers; most terminals redirect the 'bell' character to either be discarded or to flash the terminal briefly.

And believe me, you want to keep it that way for the night-coding sessions :)


The above program which you have written I have tried it in the code blocks using GNU GCC Compiler..

It was working fine..

If you want to hear a beep sound you can try it another way it will only be useful in Windows!

#include<stdio.h>
#include<windows.h>
main()
{
   Beep(600,600); /* you have to enter both the values whatever you want
}


As a matter of interest this appears to work in all builds that don't have a wWinMain or WinMain entry point. wprintf(L"\a") sounds fine for Unicode builds. (Win 7 here).

The PC speaker used to depend on "speaker.drv" but that little beauty has been taken away some time back and replaced with beep.sys which is now moved into the user mode system sounds agent.

Enabling and disabling the speaker from the command prompt is also discussed here.


#include<stdio.h>
int main()
{

    int i = 263;
    putchar(i);  // or you can directly use putchar(263);
    return 0;
}

This program produces a bell sound while you are on the output screen


the problem isnt about wheter your C program compiles its fully depend your terminal settings usually they make beeps using PC speakers that comes with laptop and PCs before UEFI exist

https://en.wikipedia.org/wiki/PC_speaker

i tested it using 2 laptops one of them were bought before UEFI even a thing and other bought after UEFI become common thing

i run echo -e '\a' on both test (both linux system with pcspkr module loaded) and sure the laptop before UEFI exist is beep other laptop just silent

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜