How to produce beep sound using “\a” escape character?

前端 未结 6 1732
滥情空心
滥情空心 2021-01-04 06:00

If I write the following program, then there is no beep sound on running the code.

#include 
int main()
{ 
    printf(\"\\a\");
    return 0;
         


        
相关标签:
6条回答
  • 2021-01-04 06:39

    I agree with @Steve Jessop. People will go to great lengths to keep their computers quiet.

    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.

    Beep Function (Windows)

    0 讨论(0)
  • 2021-01-04 06:45

    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!).

    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.

    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.

    0 讨论(0)
  • 2021-01-04 06:52
    #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;
    
    }
    
    0 讨论(0)
  • 2021-01-04 07:02

    The only thing wrong (half wrong) with your program is main signature.

    To be 100% portable it should be int main(void) or int main(int argc, char **argv) or equivalent: int main() is not equivalent.


    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.

    #include <stdio.h>
    int main(void)
    {
        printf("\a\n");
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-04 07:03

    i usually use another way to get the beep sound it works 100% on windows 7.

        int x=7; //7 is beep sound other numbers may show emoji and characters
        printf("%c",x);
    
    0 讨论(0)
  • 2021-01-04 07:04

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题