Tuesday, March 2, 2010

A closer look at #define

Today as I was starting on assignment#2, I've found out that in a #define statement one can just call it's index like in an array.
e.g. #define whatever "abcde"
one can just code: whatever[1] and if called correctly it should output the letter b.

Besides what I've just mentioned, however, we also needed to use this character: \ or the backslash in the define statement.
And what I've found out is that anytime one see the first \ it probably acts like Pearl's escapes, escaping the next character, but I think it's using printf's forward declaration or some sort to see what's the next character and if it is: either an n, t, b, etc. the backslash will escape those key letters representing newline, tab, beep respectively including itself all together; else it will probably just escape itself.  In this case the missing \ in index [0, 2, 4, 7, 9, 11, 13, 18] shows the escaping itself effect and the \n escaping both the backslash and the n, all together, because of the key-letter n.   Hoping I've concluded this correctly...
Try this code:
#define WHATEVER "abcde"
#define INDEX_CHARS "\\\!\/-\\\\\\\\|/-\\|"
#include <stdio.h>
void main(void){
    printf("%c\n", WHATEVER[1]);
    printf("\\\!\/-\\\\\\\\|/-\\| \n");
    printf("%s\n", INDEX_CHARS);
}
And the outcome is: