C语言 如何使用“\a”转义字符产生提示音?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3845590/
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
How to produce beep sound using "\a" escape character?
提问by Parikshita
If I write the following program, then there is no beep sound on running the code.
如果我编写以下程序,则运行代码时没有哔声。
#include <stdio.h>
int main()
{
printf("\a");
return 0;
}
Can you tell me how to use \afor producing beep sound using C program ?
你能告诉我如何\a使用 C 程序产生哔声吗?
回答by pmg
The only thing wrong (half wrong) with your program is mainsignature.
您的程序唯一错误(一半错误)是main签名。
To be 100% portable it should be int main(void)or int main(int argc, char **argv)or equivalent: int main()is not equivalent.
要 100% 便携,它应该是int main(void)或int main(int argc, char **argv)或等效的:int main()不等效。
And I'd print a '\n'too, or flush the output buffer rather than relying on the runtime flushing all buffers for me automatically, but your program should sound the bell as it is. If it doesn't the problem is elsewhere, not with C.
我也会打印一个'\n',或者刷新输出缓冲区,而不是依赖运行时自动为我刷新所有缓冲区,但是您的程序应该按原样响起。如果不是,问题出在其他地方,而不是C.
#include <stdio.h>
int main(void)
{
printf("\a\n");
return 0;
}
回答by Edward Leno
I agree with @Steve Jessop. People will go to great lengths to keep their computers quiet.
我同意@Steve Jessop。人们会不遗余力地保持他们的计算机安静。
In Windows: As an alternative to "\a", you could use WinAPI's Beep command. If you are using Windows 7, this may not work as expected.
在 Windows 中:作为“\a”的替代,您可以使用 WinAPI 的 Beep 命令。如果您使用的是 Windows 7,这可能无法按预期工作。
回答by user2654137
If you are using Windows, the Windows-versions making beep very different ways. Some of them enable that and working fine, but some windows versions don't. In some windows, it works just that case, if you have an internal motherboard speaker. But some other windows, it works fine without internal motherboard speaker, directly from sound-card (that would be nice!).
如果您使用的是 Windows,则 Windows 版本发出蜂鸣声的方式非常不同。其中一些启用并运行良好,但有些 Windows 版本没有。在某些窗口中,如果您有一个内置的主板扬声器,它就是这种情况。但是其他一些窗口,它在没有内置主板扬声器的情况下工作正常,直接来自声卡(那会很好!)。
If you are lucky, and using the appropriate windows-version, beep/Beep/printf("\a") will work (on internal speaker (if you have), or best case via soundcard). But if you are using an other windows version, it will not work. If in your computer it's okay, your friend's / family member's pc will silent, and he/she will think that you wrote a bad program :-D but not.
如果你很幸运,并且使用适当的 windows 版本,beep/Beep/printf("\a") 将工作(在内部扬声器上(如果你有),或者最好的情况是通过声卡)。但是如果您使用的是其他 Windows 版本,它将无法正常工作。如果你的电脑没问题,你朋友/家人的电脑会静音,他/她会认为你写了一个坏程序:-D 但不是。
My advice, that you should use a library for audio. It's simple, cross-platform, and it will be working always all times all computers etc. For example, Allegro_v4, Allegro_v5, SDL (Simple DirectMedia Layer), or something like that. These librarys works fine with OpenGL / DirectX, and with these librarys, you can load images, play videos, and things like that. Native OpenGL / GLUT / DirectX can't do things like that.
我的建议是,您应该使用音频库。它很简单,跨平台,并且在所有计算机等上始终可以正常工作。例如,Allegro_v4、Allegro_v5、SDL(简单直接媒体层)或类似的东西。这些库与 OpenGL / DirectX 配合良好,使用这些库,您可以加载图像、播放视频等。原生 OpenGL / GLUT / DirectX 不能做这样的事情。
回答by user332422
i usually use another way to get the beep sound it works 100% on windows 7.
我通常使用另一种方式来获得它在 Windows 7 上 100% 工作的蜂鸣声。
int x=7; //7 is beep sound other numbers may show emoji and characters
printf("%c",x);
回答by qht
Please list your operating system, how did you run your code, and if your computer has a beeper.
请列出您的操作系统,您是如何运行代码的,以及您的计算机是否有蜂鸣器。
If you use Windows, maybe this Q&A How to make a Beep sound in C on Windows?would help you.
如果你使用 Windows,也许这个 Q&A如何在 Windows 上用 C 发出哔哔声?会帮助你。
If you use a GUI desktop Linux distro, terminal emulator like gnome-terminal or xfce4-terminal have a preference option bell to check. Then, make sure your speaker works.
如果您使用 GUI 桌面 Linux 发行版,像 gnome-terminal 或 xfce4-terminal 这样的终端模拟器有一个首选项铃要检查。然后,确保您的扬声器正常工作。
The code below works well for me:
下面的代码对我来说效果很好:
/* name: bell.c
* run: gcc bell.c && ./a.out
*/
#include <stdio.h>
int main(void) {
printf("\a");
return 0;
}
By the way, your code is not the problem.
顺便说一句,您的代码不是问题。
回答by Lilanka Kushan Udawatta
#include<stdio.h>
#include<windows.h>
int main()
{
Beep(1000, 1000); /* you can use any number starting from 250, but make sure you use it both times
return 0;
}

