Characters and Strings
1. Why doesn't
strcat(string, '!');
work?
A: There is a very real difference between characters and strings, and strcat() concatenates *strings*.
Characters in C are represented by small integers corresponding to their character set values.
Strings are represented by arrays of characters; you usually manipulate a pointer to the first character of the array. It is never correct to use one when the other is expected. To append
a ! to a string, use
strcat(string, "!");
2. I'm checking a string to see if it matches a particular value. Why isn't this code working?
char *string;
...
if(string == "value") {
/* string matches "value" */
...
}
A: Strings in C are represented as arrays of characters, and C never manipulates (assigns, compares, etc.) arrays as a whole. The == operator in the code fragment above compares two pointers -- the value of the pointer variable string and a pointer to the string literal "value" -- to see if they are equal, that is, if they point to the same place. They probably don't, so the comparison never succeeds.
To compare two strings, you generally use the library function strcmp():
if(strcmp(string, "value") == 0) {
/* string matches "value" */
...
}
3. If I can say
char a[] = "Hello, world!";
why can't I say
char a[14];
a = "Hello, world!";
A: Strings are arrays, and you can't assign arrays directly. Use strcpy() instead:
strcpy(a, "Hello, world!");
4. How can I get the numeric (character set) value corresponding to a character, or vice versa?
A: In C, characters are represented by small integers corresponding to their values (in the machine's character set), so you don't need a conversion function: if you have the character, you have its value.
5. I think something's wrong with my compiler: I just noticed that sizeof('a') is 2, not 1 (i.e. not sizeof(char)).
A: Perhaps surprisingly, character constants in C are of type int, so sizeof('a') is sizeof(int) (though this is another area where C++ differs).
Boolean Expressions and Variables
1. What is the right type to use for Boolean values in C? Why isn't it a standard type? Should I use #defines or enums for the true and false values?
A: C does not provide a standard Boolean type, in part because picking one involves a space/time tradeoff which can best be decided by the programmer. (Using an int may be faster, while using char may save data space. Smaller types may make the generated code bigger or slower, though, if they require lots of conversions to and from int.)
The choice between #defines and enumeration constants for the true/false values is arbitrary and not terribly interesting . Use any of
#define TRUE 1 #define YES 1
#define FALSE 0 #define NO 0
enum bool {false, true}; enum bool {no, yes};
or use raw 1 and 0, as long as you are consistent within one program or project. (An enumeration may be preferable if your debugger shows the names of enumeration constants when examining
variables.)
Some people prefer variants like
#define TRUE (1==1)
#define FALSE (!TRUE)
or define "helper" macros such as
#define Istrue(e) ((e) != 0)
These don't buy anything.
2. Isn't #defining TRUE to be 1 dangerous, since any nonzero value is considered "true" in C? What if a built-in logical or relational operator "returns" something other than 1?
A: It is true (sic) that any nonzero value is considered true in C, but this applies only "on input", i.e. where a Boolean value is expected. When a Boolean value is generated by a built-in operator, it is guaranteed to be 1 or 0. Therefore, the test
if((a == b) == TRUE)
would work as expected (as long as TRUE is 1), but it is obviously silly. In fact, explicit tests against TRUE and FALSE are generally inappropriate, because some library functions (notably isupper(), isalpha(), etc.) return, on success, a nonzero value which is not necessarily 1.
(Besides, if you believe that "if((a == b) == TRUE)" is an improvement over "if(a == b)", why stop there? Why not use "if(((a == b) == TRUE) == TRUE)"?) A good rule of thumb is to use TRUE and FALSE (or the like) only for assignment to a Boolean variable or function parameter, or as the return
value from a Boolean function, but never in a comparison.
The preprocessor macros TRUE and FALSE (and, of course, NULL) are used for code readability, not because the underlying values might ever change.
Although the use of macros like TRUE and FALSE (or YES and NO) seems clearer, Boolean values and definitions can be sufficiently confusing in C that some programmers feel that TRUE and FALSE macros only compound the confusion, and prefer to use raw 1 and 0 instead.
C language latest and recent interview questions for 2008 asked in latest placement papers of leading companies in India, USA, etc. Project notes for students /college admission interviews, important basic free download programs with detailed answers and solutions for freshers and walkins for all university bca, btech, engineering, computer applications, developers jobs, etc.
No comments:
Post a Comment