- What is/was the certain use of that \a escape sequence?
According to the same section of the standard you were reading,
Alphabetic escape sequences representing nongraphic characters in the execution character set are intended to produce actions on display devices as follows
The particular action that a \a sequence is intended to produce is what you quoted. This is in fact used rarely, if ever, in modern code, and typical C implementations just map that sequence to a single ASCII character that has the same significance, relying on the terminal driver to handle the actual alert. You're more likely to see the effect if you accidentally output a binary file to a terminal than just about any other way.
- Is it a remain from the earlier days of C, that is obsolete now?
I am not aware of it being used for the purpose described in any code I have ever seen. In that sense, yes, it is obsolete, but from the perspective of the standard, that provision is not obsolete.
- And in which situation it would be or were (if it is obsolete today) appreciated to use that? Can you provide an example?
You would include that sequence in a string that is printed to stdout. When the string is printed, you (maybe) get a bell sound and / or the screen flashes and / or something similar.
printf("Ring the bell...\a\n");
Answer from John Bollinger on Stack Overflow
- What is/was the certain use of that \a escape sequence?
According to the same section of the standard you were reading,
Alphabetic escape sequences representing nongraphic characters in the execution character set are intended to produce actions on display devices as follows
The particular action that a \a sequence is intended to produce is what you quoted. This is in fact used rarely, if ever, in modern code, and typical C implementations just map that sequence to a single ASCII character that has the same significance, relying on the terminal driver to handle the actual alert. You're more likely to see the effect if you accidentally output a binary file to a terminal than just about any other way.
- Is it a remain from the earlier days of C, that is obsolete now?
I am not aware of it being used for the purpose described in any code I have ever seen. In that sense, yes, it is obsolete, but from the perspective of the standard, that provision is not obsolete.
- And in which situation it would be or were (if it is obsolete today) appreciated to use that? Can you provide an example?
You would include that sequence in a string that is printed to stdout. When the string is printed, you (maybe) get a bell sound and / or the screen flashes and / or something similar.
printf("Ring the bell...\a\n");
\aIsn't always used for an error- It is an ascii character and can be used anywhere.
\aIs used to display a sound. You can try by typingprintf '\a'in bash.
Videos
The '\a' in an escaped representation of the BEL charcater which has ascii code 7.
The \ is used to "escape" a character that otherwise has no representation and cannot be written in a string by other means. Another examples are the newline '\n' and carriage return '\r'.
Characters starting with a backslash \ are called escape sequences, these are special and aren't printed out. In this case, \a is the sequence for a bell ring.
It's just one note. Without any other pitches for context, it could just as easily be A in D, E, F, G, A or B♭ major, or their relative minor scales.
Keyboard images taken from piano-keyboard-guide.comI'll admit there's plenty of .hack media that I haven't seen/played/read, but I've only ever heard it as a single note. Is there something that explains how they decided it's in C major?
EDIT: Answer seemingly found. I looked further into the original Japanese after u/rose-ramos suggested it could be a translation issue. It looks like an alternate translation would be "La in C major." That makes a lot more sense.
For those not in the know, there are multiple ways to refer to notes. Using C major as an example, you've got the letters, C D E F G A B, and you've got solfège, Do Re Mi Fa Sol La Ti. You might be familiar with solfège if you've heard that song from The Sound of Music ("Do, a deer, a female deer. Re, a drop of golden sun." Etc.)
The interesting thing about solfège is that it can change. You've got fixed Do solfège, where Do is C, Re is D, etc., and you've got movable Do, where Do refers to the root note of the scale. In C major, that means Do Re Mi is C D E, but in D major, that means Do Re Mi is D E F#.
So there it is. With the movable Do system, La in C major is indeed A, but just calling it A gets rid of the context that would make it necessary to specify that it's in C major.
What you are comparing are the two memory addresses for the different strings, which are stored in different locations. Doing so essentially looks like this:
if(0x00403064 == 0x002D316A) // Two memory locations
{
printf("Yes, equal");
}
Use the following code to compare two string values:
#include <string.h>
...
if(strcmp("a", "a") == 0)
{
// Equal
}
Additionally, "a" == "a" may indeed return true, depending on your compiler, which may combine equal strings at compile time into one to save space.
When you're comparing two character values (which are not pointers), it is a numeric comparison. For example:
'a' == 'a' // always true
I'm a bit late to the party, but I'm going to answer anyway; technically the same bits, but from a bit different perspective (C parlance below):
In C, the expression "a" denotes a string literal, which is a static unnamed array of const char, with a length of two - the array consists of characters 'a' and '\0' - the terminating null character signals the end of the string.
However, in C, the same way you cannot pass arrays to functions by value - or assign values to them (after initialization) - there is no overloaded operator == for arrays, so it's not possible to compare them directly. Consider
int a1[] = {1, 2, 3};
int a2[] = {3, 4, 5};
a1 == a2 // is this meaningful? Yes and no; it *does* compare the arrays for
// "identity", but not for their values. In this case the result
// is always false, because the arrays (a1 and a2) are distinct objects
If the == is not comparing arrays, what does it actually do, then? In C, in almost all contexts - including this one - arrays decay into pointers (that point to the first element of the array) - and comparing pointers for equality does what you'd expect. So effectively, when doing this
"a" == "a"
you are actually comparing the addresses of first characters in two unnamed arrays. According to the C standard, the comparison may yield either true or false (i.e. 1 or 0) - "a"s may actually denote the same array or two completely unrelated arrays. In technical terms, the resulting value is unspecified, meaning that the comparison is allowed (i.e. it's not undefined behavior or a syntax error), but either value is valid and the implementation (your compiler) is not required to document what will actually happen.
As others have pointed out, to compare "c strings" (i.e. strings terminated with a null character) you use the convenience function strcmp found in standard header file string.h. The function has a return value of 0 for equal strings; it's considered good practice to explicitly compare the return value to 0 instead of using the operator `!´, i.e.
strcmp(str1, str2) == 0 // instead of !strcmp(str1, str2)
It counts the number of occurrences of each letter.
'A' is just a number. On an ASCII-based machine, 'A' is just another way of writing 65.
In ASCII, the 26 latin uppercase letters are found consecutively, so,
'A'-'A'=0'B'-'A'=1'C'-'A'=2- ...
'Z'-'A'=25
(Note that this code doesn't work on EBCDIC machines because the letters aren't contiguous in that encoding.)
So letter[c - 'A'] produces a unique element of letter for each letter.
Finally, ++x increments the value of x, so ++letter[c - 'A'] increments the element of letter that corresponds to the letter in c.
For example, if we read in ABRACADABRA, we'll end up with
int letter[26] = {
/* A B C D E F G H I J K L M */
5, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* N O P Q R S T U V W X Y Z */
0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0
};
regarding:
if ('A' <= c && c <= 'Z')
++letter[c - 'A'];
would be much better written as:
if( isupper( c ) )
{
letter[ c-'A' ]++;
}
where isupper() is exposed via the header file ctype.h
regarding:
for (i = 0; i < 26; ++i)
letter[i] = 0;
can be eliminated IF this:
int c, i, letter[26];
had been written as:
int c, i, letter[26] = {0};
Now for your question:
letter[] is an array for all the capital (ASCII) alphabet where letter[0] is a counter for the letter A.... letter[25] is a counter for the letter Z.
This calculation:
[ c-'A' ]
is taking the ordinal value in the variable c and subtracting the ordinal value of the capital letter A. I.E. if c contains A then the result is 0 (which matches the index for the letter A in the array letter[]
The ++ says to increment the value in the array letter[]
The overall result is the entry in the array letter[] for the current letter in c will be incremented, thereby keeping a count of the number of occurrences of each capital letter encountered in the input