Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this explanation accurate for C code that won't compile?

Tags:

c

Alleged interview question and answer here.

Will the following code compile (in C)?

#define X 8;
int main(void)
{
    ++X; // will this line compile?
}`

I am no expert in C, but I know some C++ and thought: of course not, you cannot increment the number 8, it is an rvalue. Of course the preprocessor replaces the X with the 8 before trying to compile, and when it does try to compile, it will fail for that very reason. Then again, I am the one reading interview question websites so then I thought who knows...

Here is the explanation given:

"Strictly speaking, the operand of the prefix (or postfix) increment operator must be a non-modifiable lvalue. Now that we know what an lvalue is, we must ask ourselves if X is an lvalue. X is a macro, which means that it does not identify a place in memory – macros use simple text replacement via the preprocessor. Because macros don’t exist in a region of memory, they are not lvalues. This means that X can not be used as an operand of the prefix increment operator. Thus, the code shown above will not compile."

Is this explanation as bunk as I think it is?

How many errors can you find above? I think maybe that should be the interview question...

And this is just funny:

"Intuitively, you might be able to say that the code above will not compile – without knowing exactly why. However, in an interview situation, you will be expected to provide some reasoning like what’s given above. Simple yes or no answers just won’t cut it in an interview." (!)

like image 560
Jimmy Avatar asked Aug 16 '11 08:08

Jimmy


People also ask

What does it mean when code doesn't compile?

Learn to write better code. Failing to compile means you have written something that isn't legal in the language you are using. It could be something as simple as a typo, a letter missing or transposed, in which case most modern IDE's and programmers editors will highlight for you as you type.

Can I use G ++ for C?

g++ is used to compile C++ program. gcc is used to compile C program. g++ can compile any . c or .


1 Answers

The explanation given is incorrect:

X is a macro, which means that it does not identify a place in memory – macros use simple text replacement via the preprocessor.

Precisely because macros are just simple text replacement, they can expand to an lvalue, or for that matter anything else. For example:

int x;
#define X x

int main() {
    ++X;
}

is OK. It's true that the macro itself doesn't have a place in memory, but that's irrelevant to whether ++X; is well-formed, since ++X; doesn't mean, "increment the macro X", it means "expand the macro X and then stick ++ on the front, ; on the back, and perform syntactic and semantic analysis on the result".

What the explanation says about "macros", it should say about integer constants. 8 is not an lvalue, and that's what matters here.

With that change, the explanation is OK [Edit - as Chris points out in a comment, it's still not OK, it writes "the operand of the prefix (or postfix) increment operator must be a non-modifiable lvalue": that should read "modifiable"]

like image 143
Steve Jessop Avatar answered Sep 20 '22 23:09

Steve Jessop