tags:

views:

416

answers:

9

Hello there,

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

+4  A: 

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

s_b
Either speaker didn't sounds beep in my windows xp
Sharifhs
+3  A: 

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

BoltClock
+8  A: 

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

ChristopheD
Either speaker didn't beep in my case
Sharifhs
+14  A: 

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

Thomas
`...nowadays, many don't.` Thank goodness!
Bobby
It would be either the **character is displayed** or **a bell is sounded**, but not both. For me, I just hear the bell, no character displayed.
Lazer
In my case neither character is displayed nor beep is heared...but it was compliled correctly
Sharifhs
With "displayed" I meant "sent to the terminal"; it wouldn't necessarily appear on the screen.
Thomas
+1  A: 

\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.

Jim Lewis
+5  A: 

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.

RBerteig
+2  A: 

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 :)

lorenzog
Thanks for suggestion
Sharifhs
A: 

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

Baltasarq
Thank you, i tried as you explained, but it didn't work yet!
Sharifhs
Take into account the other answers. The BEL character may or may not ring the bell in your system. Change "printf("\a");" with " printf("Hello, world");"If the message "Hello, world" appears on the screen, then your terminal does not support the BEL character.
Baltasarq
I've tested the program as in the cource code above, and it works on Windows 7, using mingw' gcc.
Baltasarq
A: 

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.

josefx