C语言 这个程序敲响了警钟!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3456138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
This program sounds the bell!
提问by Sharifhs
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 could hear a ringing bell? I can't hear any ringing bell sound!!!
这个程序是什么意思?这个程序是否意味着我们可以听到铃声?我听不到任何铃声!!!
回答by Thomas
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.)
ASCII 字符 7 是 BELL 字符,它在 C 中表示为\a. 有的终端在终端输出该字符时会发出哔哔声;如今,许多人没有。(我在看着你,Ubuntu。)
回答by RBerteig
Back in the dark ages when ASCIIwas codified out of the ashes of BAUDOT, a terminalwas 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.
在BAUDOT的灰烬中编码ASCII的黑暗时代,终端是一大块铁,可以将墨水锤到纸上,通常包括纸带打孔器和阅读器,并解释击键以生成异步串行信号带有旋转轮和继电器的几百波特。
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 \ain a C character or string constant rang the bell when received.
如果操作员在敲打文本的舒缓噪音中睡着了,它就会有一个真正的铃铛可以响起。八进制编码 007、十六进制编码 0x07 或\aC 字符或字符串常量中的字符在收到时响起。
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.
当要求显示控制字符时,您的终端仿真器(在 Windows 中也称为控制台窗口、xterm 或在 Unix 中类似的东西)究竟做了什么,今天还没有很好地标准化。它应该发出声音或使窗户闪光,但您的里程会有所不同。
回答by ChristopheD
Have a look at this wikipedia entry: bell character:
看看这个维基百科条目: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).
在 C 编程语言(创建于 1972 年)中,铃声字符可以用 \a 放置在字符串或字符常量中('a' 代表“警报”或“可听”,之所以选择它是因为 \b 已经用于退格)。
回答by s_b
You'll hear a beep from your PC's internal speaker (not the external speakers or headphones you may have attached).
您会听到 PC 的内部扬声器(不是您可能连接的外部扬声器或耳机)发出的哔哔声。
回答by BoltClock
\adoes in fact trigger the system chime. It's the escape sequence for the ASCII BELcharacter.
\a实际上确实会触发系统提示音。它是 ASCIIBEL字符的转义序列。
回答by Baltasarq
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
最重要的变化是系统标题必须用 < 和 > 包围,而不是用引号括起来。此外,最好知道 main() 函数总是返回一个 int(到操作系统),并且这个 int 被编码为两个常量,EXIT_SUCCESS 和 EXIT_FAILURE,在头文件 stdlib.h 中
回答by josefx
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.
字符串可以包含与所有其他字符不同处理的字符。最常显式使用的是'\n'. 该'\n'字符不会在控制台中打印字符,而是告诉控制台开始一个新行。这种特殊字符被称为不可打印,因为它们在 c 中没有自己的可见表示,并且必须使用转义序列。
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'.
在转义序列"\a"中,a 之前的反斜杠告诉编译器 a 是特殊字符的标识符,并将存储其 char-value 而不是 的 char-value '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.
该'\a'转义序列是听得见的响铃字符,给通过打印这个人物控制台()应当引起蜂鸣声。有些控制台不会发出哔哔声。
Here are somespecial characters, the link is from a c++ reference but most should be valid for c.
这里有一些特殊字符,链接来自 c++ 参考,但大多数应该对 c 有效。
回答by Jim Lewis
\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.
\a 是 ASCII 声音警报(“bell”)控制字符的 C 表示。在老式串行终端上,输出该字符会产生“哔”声。您的终端模拟器可能会也可能不会实现此功能。
回答by lorenzog
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.
正如其他人指出的那样,您使用 BELL char 的示例可能不适用于今天的烤面包机^H^H^H^H^H^H^H^H;大多数终端重定向'bell'字符要么被丢弃,要么短暂地闪烁终端。
And believe me, you want to keep it that way for the night-coding sessions :)
相信我,你想在夜间编码会议上保持这种状态:)
回答by Harsha Vardhan
The above program which you have written I have tried it in the code blocks using GNU GCC Compiler..
您编写的上述程序我已经在使用 GNU GCC 编译器的代码块中尝试过。
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!
如果您想听到哔声,您可以尝试另一种方式,它只会在 Windows 中有用!
#include<stdio.h>
#include<windows.h>
main()
{
Beep(600,600); /* you have to enter both the values whatever you want
}

