terça-feira, junho 23, 2009

Do you really know C

I don't like to write about C because Wanderley, a friend of mine, is much more able to talk about it, but I think I must report what happened to me these days.

After some time developing with a programming language we get so used to it that we start thinking we are the master on it.

But usually the opposite happens: we forget some basic and subtle aspects of the language.

A couple of days ago I wrote the C command bellow:
printf("x position = %d (again??)", 7);
and to my surprise it was printed
x position = 7 (again ]
Why didn't the application printed
x position = 7 (again ??)
I thought "Visual Studio (2005) is a crap!".

Some taboo words latter and I realized what the problem was.

I remembered C has a feature named trigraph. In other words, C replaces all special sequences of three characters by their single-character equivalent.

The following are the nine trigraph sequences that C recognizes.
Trigraph    Equivalent
??=         #
??/         \
??'         ^
??(         [
??)         ]
??!         |
??<         {
??>         }
??-         ~
Why do C has trigraphs? To let us for writing source code when the keyboard being used does not support any of these nine characters.

I compiled the program using gcc and it gives me the following warning
warning: trigraph ??) ignored, use -trigraphs to enable
This means that on gcc we must explicitly request to it to enable trigraphs. The output presented after running the program was
x position = 7 (again ??)
Since 1994 the C standard (C99), supplies digraphs as more readable alternatives to six of the trigraphs. They are:
Digraph     Equivalent
<:      [
:>      ]
<%      {
%>      }
%:      #
%:%:    ##
I must not be so proud of myself because eventually something (like this) remembers me that I am already a newbie and a wanna-be.

Oh, and at this time Visual Studio was unfairly blamed. Sorry ;)

Nenhum comentário: