How to print "\a" in c++ using codelite?

480 views Asked by At

I was trying to print \a but it shows nothing so I searched for this and found out it should have made a sound but it didn't either.

I am using code lite on Windows 8.

  1. How to print \a?
  2. Where is that sound?

This is my code:

#include <iostream>
using namespace std;
int main()
{
    cout <<"\a";
    return 0;
}
1

There are 1 answers

0
Mohit Jain On BEST ANSWER

How to produce bell sound

If you want to print '\a' character to produce bell sound, you are already doing it the right way.

If the system beep is disabled (depending on your OS/target system, steps to enable beep may be different), you won't hear any sound. For windows you can follow this tutorial to enable the beep sound. One of the method tutorial suggests is following (For windows 7, should be more or less similar in windows 8 also)

Device Manager > View menu > show hidden devices > Non-Plug and Play Drivers > Beep > double click or right click and properties > Driver tab > Startup > Type > Change from disabled to some other mode (system for example)

Also it is a legacy method to produce system beep, modern systems may not make a noise, and may instead make a visual indication (such as flashing the screen, or do nothing at all.) (From Wikipedia Bell character)


How to print \a then

If you want to literally print \a use following

cout <<"\\a";

\ is a delimiter character and \a stands for ascii alarm or bell character.

Using escape sequence \\ will print the first \ character followed by the next a.

"\a" is const char 2 with values \a and '\0'

"\\a" in const char 3 with values \, 'a' and '\0'

Further readings:
1. String literal in C++
2. Escape sequences in C++