Home arrow Practices arrow Page 4 - Basic Ideas

Names Using Extended Character Sets - Practices

This article will give you a good understanding of the basic concepts and practices of the C++ language, so that you will have the foundation to eventually learn these ideas in detail as you continue working with the language. It is excerpted from Ivor Horton's Beginning ANSI C++ The Complete Language (Apress, 2004; ISBN 1590592271).

  1. Basic Ideas
  2. Interpreted vs. Compiled Program Execution
  3. A Simple C Program
  4. Names Using Extended Character Sets
  5. C Statements and Statement Blocks
  6. Creating an Executable from Your Source Files
  7. C Source Characters
  8. Whitespace in Statements
  9. Procedural and Object-Oriented Programming
By: Apress Publishing
Rating: starstarstarstarstar / 18
March 23, 2005

print this article



As mentioned in the previous section, the C++ standard permits UCS characters to be included in a name. You can write them in the form \Udddddddd or the form \udddd, where d is a hexadecimal digit in the UCS code for the character.

No one really expects anyone to include characters in names like this, though. Embedding \U followed by a bunch of hexadecimal digits in a name would hardly improve the readability of the code. The purpose of allowing UCS characters in a name is to allow compiler writers to accommodate names written in characters for national languages other than English, such as Greek, or Korean, or Russian, for instance.

The C++ standard allows a compiler to be implemented so that any characters can be used to specify a name. Any compiler that takes advantage of this must then translate the characters that arenít in the basic set to the standard representation for UCS characters noted previously, before the compilation of the code begins. In the source code someone may write the name Khhra, which will be meaningful to a Russian programmer. Internally the compiler will convert this name to one of the two standardized representations of UCS characters before it compiles the code, perhaps as /u041A/u043D/u0438/u0433/u0430. Indeed, regardless of the character set used to write names in the source, it will always end up as characters from the basic set that you saw initially, plus possibly UCS characters as \Udddddddd or as \udddd.

You must always use the basic set of characters a to z, A to Z, 0 to 9, and the underscore in a name as explicit characters. Using the UCS codes for these characters in a name is illegal. The reason for this is that the standard doesnít specify the encoding to be used for the basic set of characters, so this is left to the compiler. Consequently, if you were to specify any of the basic characters by its UCS code, itís possible it will be different from the encoding used by the compiler when the character is specified explicitly with the obvious chaotic result.

Note that it isnít a requirement that any given compiler must support the use of explicit national language characters in specifying names. If it does, they must be mapped into the UCS form before processing. A compiler that conforms to the standard must in any event support names in the basic character set plus the use of UCS characters in the somewhat unfriendly forms noted at the beginning of this section.


Iím sure that you noticed there was a line in the simple C++ program that I didnít explain in the preceding discussion. To understand it, you need to know what name-spaces are, and for those to make any sense, I had to first to tell you about names. As a reminder, the line in question was

using namespace std;

Within the rules for identifiers that I discussed in the previous section, you can choose any names that you like for things in your programs. This obviously means that you might choose a name for something thatís already used for some other purpose within the standard library. Equally, if two or more programmers are working concurrently on parts of a larger project, there is potential for name collisions. Clearly, using the same name for two or more different things is bound to cause confusion, and namespaces are there to alleviate this problem.

A namespace name is a bit like a family name or a surname. Each individual within a family has his or her own name, and within most families each family member has a unique name. In the Smith family, for instance, there may be Jack, Jill, Jean, and Jonah, and among family members theyíll refer to each other using these names. However, members of other families may have the same names as members of the Smith family. Within the Jones family, for instance, there might be John, Jean, Jeremiah, and Jonah. When Jeremiah Jones refers to Jean, itís clear that he means Jean Jones. If he wants to refer to Jean in the Smith family, heíll use the fully qualified name: Jean Smith. If youíre not a member of either family, you can only be sure that people know whom youíre talking about if you use the full names of individuals, such as Jack Smith or Jonah Jones.

This is pretty much how namespaces workóa namespace name is analogous to a surname. Inside a namespace, you can use the individual names of things within the namespace. From outside the namespace, you can only refer to something within the namespace by a combination of the name of the particular entity and the namespace name. The purpose of a namespace is to provide a mechanism that minimizes the possibility of accidentally duplicating names in various parts of a large program and thereby creating confusion. In general, there may be several different namespaces within a program.

The entities in the C++ standard library are all defined within a namespace called std, so the names of all the entities within the standard libraries are qualified with std. The full name of cout, therefore, is actually std::cout. Those two colons together have a very fancy title: the scope resolution operator. Iíll have more to say about it later on. In this example, it serves to separate the namespace name, std, from the name of the stream, cout.

The using directive at the beginning of the simple C++ program indicates that you want to refer to any of the things defined within the namespace called std without specifying the namespace name each time. Continuing this analogy, it makes your program file a sort of honorary member of the std family, so you can refer to everyone by his or her first name alone. One effect of this is to obviate the need to refer to cout as std::cout, making the program code little simpler. If you were to omit the using directive, you would have to write the output statement as

 std::cout << "The best place to start is at the 

Of course, although this does make the code look a little more complicated, itís really a much safer and therefore better way to write the code. The effect of the using directive is to allow you to refer any name in the namespace without qualifying it with the namespace name. This implies that you could do so accidentally. By explicitly qualifying cout with its namespace name, you avoid the need to make all the names in the namespace accessible in your program. This means that thereís no possibility of clashes between names that you choose for things you might define in your program and names that are defined in the namespace.

The program code for this example is therefore somewhat better if you write it as follows:

// Program 1.1 A simple C++ program
#include <iostream>
int main() {
  std::cout << "The best place to start is at the 
  return 0;

However, although this is much safer code, if you had a lot more references to std::cout in the code it might begin to look very cluttered. You also have the irritation of repeatedly typing std:: in many places throughout the program. In this case, you can use a form of the using directive that just introduces a single name from a  namespace into your program source file. For instance, you can introduce the name cout from the std namespace into your program file with the following directive:

using std::cout;

With this directive, you can get the best of both worlds. You can use the name cout from the std namespace in its unqualified form, and you protect yourself from accidental conflicts in your code with other names in the std namespace because theyíre simply not accessible without using the std qualifier. The program now looks like this:

// Program 1.1A A simple C++ program
#include <iostream>
using std::cout;
int main() {
  cout << "The best place to start is at the beginning";
  return 0;

Of course, you can introduce just some names from the std namespace into your program file by means of a using directive for each name. You might do this for names that you refer to frequently in your code. You can then access other names from std that you refer to relatively rarely by their fully qualified names.

Thereís much more to namespaces and using directives than youíve seen here. Youíll explore them in depth in Chapter 10.


There are reserved words in C++, called keywords, that have special significance within the language. The words return and namespace that you saw earlier are examples of keywords.

Youíll see many more keywords as you progress through the book. You must ensure that the names that you choose for entities in your program arenít the same as any of the keywords in C++. Youíll find a list of all the keywords that are used in C++ in Appendix B.

Keywords are case sensitive, as are the identifiers that you choose in your program.

This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN  1590592271). Check it out at your favorite bookstore today. Buy this book now.

>>> More Practices Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Calculating Development Project Costs
- More Techniques for Finding Things
- Finding Things
- Finishing the System`s Outlines
- The System in So Many Words
- Basic Data Types and Calculations
- What`s the Address? Pointers
- Design with ArgoUML
- Pragmatic Guidelines: Diagrams That Work
- Five-Step UML: OOAD for Short Attention Span...
- Five-Step UML: OOAD for Short Attention Span...
- Introducing UML: Object-Oriented Analysis an...
- Class and Object Diagrams
- Class Relationships
- Classes

Developer Shed Affiliates


Dev Shed Tutorial Topics: