The Database Managers, Inc.

Contact The Database Managers, Inc.


Use an RSS enabled news reader to read these articles.Use an RSS enabled news reader to read these articles.

Answers for C++ Language Lawyer Questions

by Curtis Krauskopf

Language lawyer questions sometimes seem pointless, but they come into practice when you're modifying a program written by someone else. The previous developer might have misunderstood a part of the language or the language might have changed since the code was written. Being able to know the details of the language and even know how the language has changed over the years shows that you're able to understand the existing programs that the customer or employer has.

Q1) Show an example of a valid C program that fails to compile in C++.

A1) Use any C++ keyword that doesn't exist in C. The class keyword is a good example: int class = 5;

Q2) Show an example of a valid C program that compiles cleanly in C++ but gives a different result.

A2) This one is a little bit trickier. We want some feature in C++ that wasn't backwards compatible with C. Thinking outside of the box, you could come up with:


#ifdef __cplusplus
  printf("I am C++\n");
#else
  printf("I am C\n");
#endif

The interviewer might accept that the __cplusplus compiler directive was not defined in a normal C program.

However, keeping with the spirit of the question, there are a couple reasonable solutions available:

1. C++ comments use both // and /* */ whereas standard C uses only //. If you can construct an expression in a C program that contains /* and */, it will compile cleanly in both compilers but give different runtime results.

2. Literal characters in C (such as 'a'), are of type int. Therefore, in C, sizeof('a') is the same as sizeof(int). Literal characters in C++ are of type char. On all Borland C++ compilers, sizeof(int) != sizeof(char).

David Tribble's web site contains a detailed synopsis of the incompatibilities between C and C++.

Q3) Why won't this compile?


for (int i = 0; i < 10; i++) {
  for (int k = 0; k < 10; k++) {
    if (e == 0) goto next_i;
  }
  next_i;
} 


A3) The next_i label needs a colon (:) after it, like this: next_i:

Q4) Is there a "placement delete"?

A4) No, but you can write your own. Stroustrup discusses this on his C++ FAQ page at att.com.

Previous Answers More C++ Answers
Jump to Questions Page:  1  2  3  4  5  6  7  8  9  10  11  12  13 
 
Jump to Answers Page:  1  2  3  4  5  6  7  8  9  10  11  12  13 
Services | Programming | Contact Us | Recent Updates
Send feedback to: