Embedded Systems October 2000 Vol13_11

Issue link:

Contents of this Issue


Page 76 of 181

Dan Saks Character and String Literals ( and C++ provide several different kinds of literals: integer literals such as 10 and Ox1C, floating litel-als such a 1.0 and 6.022e+23, character li terals uch as I a I and I \x10 I , oved oaded functions and o perators can have the expecL ed be havior, as explained by the following example. The Standard C++ library provides and string liL- erals such as "ouch!" and "\n". Last mo nLh , I examined the integer and floating li terals. l This month , I'll look at the character and SU-ing liLerals. C and C++ diffel- with respect to numel-ic (integer and fl oating) literals in thaL C offers a couple of features nOL available in C++. In particular: • Integer liLerals in C can have Long Long integer types. C++ does not have Long Long integer types • C permiLs fl oati ng literals in hexa- decimal form. C++ does not Aside from th ese added capabilities of , numeric li terals are the same in C++ as they are in C. OL surprisingly, cha racte r and string liLerals a re also very simil ar in C and C++. e ither lang uage offers form unavaila bl e in th e o th e r la n- guage. However, C++ changed th e be havio r of character and string li t- e rals in some subtl e but inte r sting ways. Character literals In C, cha racte r literals such as I and I \x10 I ostream &operator« (ostream &, char); ostream &operator« (ostream &, int); ostream &operator« (ostream &, Long int); The Standard C++ library defines ostream a the type of an output sU-eam. Each fu nction has a first para- me te r and re turn value of type os t rea m passed by refe rence. An expression such as: a I have type i nt, not char. At the risk of in troducing a minor incom- patibili ty with C, C++ changed the Lype of character literals to char so th at cout « X; translates into the call : operator«(cout, x); If x is an i nt, then from among the a seL of functions named operator« as output operators, declared more or less as: functions declared just above, the best match for this call is: ostream &operator« (ostream &, int); which writes a character seque nce rep- In C++, character and string literals function just as C programmers would expect them to. Even more so. resenting the numeric value of x to output stream couto On the other hand, if x is a char, the n the best match for the call is: ostream & operator«(ostream &,char); which writes a single character repre- senting the value of x to couto If character li terals in C++ had type int (as they do in C) , then an expres- sion such as: cout « I a would call: ostream &operator« (ostream &, int); which displays its right-hand operand as a number, not as a character. On a Embedded Systems Programming OCTOBER 2000 75 I ;

Articles in this issue

Archives of this issue

view archives of EETimes - Embedded Systems October 2000 Vol13_11