String Functions and Character Arrays

Chapter 6 - Working with Data

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Identifiers
Identifiers are the names you use to represent variables, constants, types, functions, and labels in your program. You create an identifier by specifying it in the declaration of a variable, type, or function. You can then use the identifier in later program statements to refer to the associated item.
An identifier is a sequence of one or more letters, digits, or underscores that begins with a letter or underscore. Identifiers can contain any number of characters, but only the first 31 are significant to the compiler. (However, other programs that read the compiler output, such as the linker, may recognize even fewer characters.)
C and C++ are case sensitive. This means that the C compiler considers uppercase and lowercase letters to be distinct characters. For example, the compiler sees the variables NAME_LENGTH and Name_Length as two unique identifiers representing different memory cells. This feature enables you to create distinct identifiers that have the same spelling but different cases for one or more of the letters.
The selection of case can also help you understand your code. For example, identifiers declared in #include header files are often created using only uppercase letters. Because of this, whenever you encounter an uppercase identifier in the source file, you have a visual clue as to where that particular identifier’s definition can be found.
While it is syntactically legal, you should not use leading underscores in identifiers you create. Identifiers beginning with an underscore can cause conflicts with the names of system routines or variables and produce errors. As a result, programs containing names beginning with leading underscores are not guaranteed to be portable. Use of two sequential underscore characters (__) in an identifier is reserved for C++ implementations and standard libraries.
One stylistic convention adopted by many C programmers is to precede all identifiers with an abbreviation of the identifier’s data type. For example, all integer identifiers would begin with an “i,” floats would begin with an “f,” null-terminated strings would begin with “sz,” pointer variables would begin with a “p,” and so on. With this naming convention, you can easily look at a piece of code and see not only which identifiers are being used, but also their data type. This makes it easier to learn how a particular section of code operates and to do line-by-line source debugging. The programs throughout this book use both variable naming conventions since many of the programs you encounter in real life will use one format or another.
The following are examples of identifiers:
i
itotal
frange1
szfirst_name
lfrequency
imax
iMax
iMAX
NULL
EOF
See if you can determine why the following identifiers are illegal:
1st_year
#social_security
Not_Done!
The first identifier is illegal because it begins with a decimal number. The second begins with a # symbol, and the last ends with an illegal character.
Take a look at the following identifiers. Are they legal or not?
O
OO
OOO
_____
Actually, all four identifiers are legal. The first three use the uppercase letter “O.” Since each has a different number of O’s, they are all unique. The fourth identifier is composed of five underscore (_) characters. Is it meaningful? Definitely not. Is it legal? Yes. While these identifiers meet the letter of the law, they greatly miss its spirit. The point is that all identifiers, functions, constants, and variables should have meaningful names.
Since uppercase and lowercase letters are considered distinct characters, each of the following identifiers is unique:
MAX_RATIO
max_ratio
Max_Ratio
The C compiler’s case sensitivity can create tremendous headaches for the novice C programmer. For example, trying to reference the printf( ) function when it was typed PRINTF( ) will invoke “unknown identifier” complaints from the compiler. In Pascal, however, a writeln is a WRITELN is a WriteLn.
With experience, you would probably detect the preceding printf( ) error, but can you see what’s wrong with this next statement?
printf(“%D”,integer_value);
Assuming that integer_value was defined properly, you might think that nothing was wrong. Remember, however, that C is case sensitive—the %D print format has never been defined; only %d has.
For more advanced applications, some linkers may further restrict the number and type of characters for globally visible symbols. Also, the linker, unlike the compiler, may not distinguish between uppercase and lowercase letters. By default, the Visual C/C++ LINK sees all public and external symbols, such as MYVARIABLE, MyVariable, and myvariable as the same. You can, however, make LINK case sensitive by using the /NOI option. This would then force LINK to see the preceding three example variables as unique. Use your PWB help utility for additional information on how to use this switch.
One last word on identifiers: an identifier cannot have the same spelling and case as a keyword of the language. The next section lists C and C++ keywords.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net