Did you know, that in the C programming language, and close brother C++, you can concatenate two strings without even using strcat or the += operator?
At school, they never told me that you could just write two strings contiguously, and that they would be concatenated automatically. But they are! And mind you, it doesn't work in Java!
C++:string s = "C++" "string";
What would be the result?
Free-form language
Strictly speaking, nobody's telling the compiler to actually concatenate these strings. It might look similar to semicolon spamming:
C++:int i = 13 + 14;;;;;
The compiler sees these as five statements. The fact that four are empty, is utterly irrelevant to the compiler. After all, C (and C++) is a free-form programming language that allows you to put arbitrary tabs and spaces wherever you desire. But you can't do that with variables. Right?
In mathematics, (and especially in complex algebra) to improve readability in formulas, multiplications like c = a x b can be simplified to c = a b. Could I do this with C++?
C++:int a = 12;
int b = 7;
int c = a b; // Error: missing ';' before identifier 'b'
Space operator
Ofcourse it doesn't work, but why not? Because the binary = operator takes only one argument. And fortunately, there is no such thing as the space operator in C++. That would, in theory, make it possible to get a valid result out of line 3, but would render our favorite programming language awfully malformed. Every space character would have to be substituted because it would otherwise be mistaken for the space operator. What should we substitute it with? In the following example, MOO is used:
C++:intMOOaMOO=MOO12;
intMOObMOO=MOO7;
intMOOcMOO=MOOa b;Caution: Hypothetical code. Do not use in mission-critical business systems or intelligent refridgerators!
Illiterate strings
Then how about pasting two literal strings behind each other? You are probably thinking, isn't strcat or the += operator supposed to do that? Yes, but they operate on variable, non-literal (not, illiterate) strings. Literal strings can easily be concatenated in the following manner:
C++:string s = "C++" "string";
const char* s2 =
"C++ strings "
"as well as"
"regular C strings";
This allows you to not cross the horizontal border and still make use of indentation.
2 comments:
Hello Daniel,
You will be fascinated by the whitespace programming language, discussed here:
http://compsoc.dur.ac.uk/whitespace/
Gert
Post a Comment