Wednesday, December 19, 2007

C Interview Questions - Part 14


8.1 Why doesn't

strcat(string, '!');

work?

8.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" */
...
}

8.3 If I can say

char a[] = "Hello, world!";

why can't I say

char a[14];
a = "Hello, world!";

8.4 I can't get strcat to work. I tried

char *s1 = "Hello, ";
char *s2 = "world!";
char *s3 = strcat(s1, s2);

but I got strange results.

8.5 What is the difference between these initializations?

char a[] = "string literal";
char *p = "string literal";

My program crashes if I try to assign a new value to p[i].

8.6 How can I get the numeric value (i.e. ASCII or other character set code) corresponding to a character, or vice versa?

8.7 Does C have anything like the ``substr'' (extract substring) routine present in other languages?

8.8 I'm reading strings typed by the user into an array, and then printing them out later. When the user types a sequence like \n, why isn't it being handled properly?

8.9 I think something's wrong with my compiler: I just noticed that sizeof('a') is 2, not 1 (i.e. not sizeof(char)).

8.10 I'm starting to think about multinational character sets, and I'm worried about the implications of making sizeof(char) be 2 so that 16-bit character sets can be represented.

No comments: