Abusing C/C++ comments

H

Hapary

Hello all,

I remember that I saw a piece of code a few years ago, it was using a
combination of both /* */ and // such that you could determine the
compile type of the source code. I don't remember exactly what was
happening, but I assume it was something like if the source code was
being compiled by a c compiler the value of a variable became 1 and if
it was compiled with c++ compiler the value became 0. Do you know how
can I find that ?

Any ideas ?

Thanks in advance,
 
M

Malcolm McLean

Hapary said:
Hello all,

I remember that I saw a piece of code a few years ago, it was using a
combination of both /* */ and // such that you could determine the
compile type of the source code. I don't remember exactly what was
happening, but I assume it was something like if the source code was
being compiled by a c compiler the value of a variable became 1 and if
it was compiled with c++ compiler the value became 0. Do you know how
can I find that ?
/* */ is a legal C++ comment whilst // is not a legal sequence in C. C++
comments comment each other out. I can't think of a way to achieve this.
The standard, acceptable way is to use the #ifdef __cplusplus construct
provided for the purpose.
 
K

Keith Thompson

Malcolm McLean said:
/* */ is a legal C++ comment whilst // is not a legal sequence in
C. C++ comments comment each other out. I can't think of a way to
achieve this.
The standard, acceptable way is to use the #ifdef __cplusplus
construct provided for the purpose.

// introduces a comment in C99.

In C90, // could be a division operator immediately followed by the
first character of a /* ... */ comment.

For example:

#include <stdio.h>
int main(void)
{
char *messages[] = {
"// comments are not accepted",
"// comments are accepted"
};
int i = 2 //**/ 2
- 1;
puts(messages);
return 0;
}
 
M

Malcolm McLean

Keith Thompson said:
// introduces a comment in C99.

In C90, // could be a division operator immediately followed by the
first character of a /* ... */ comment.
Aha, we can do it

x = 2 //* C comment */ 2;
;

Now we've got x = 2 in C++ and x = 1 in C.
 
R

Richard Heathfield

Ian Collins said:
It is in C99.

You can make it a legal sequence in C90, too, if you try hard enough:

a = b//*how silly*/c
;

is legal in both C90 and C99. IIRC C99 disambiguates the code by dealing
with the first legal comment syntax it encounters, so these two lines
have different meanings in C90 and C99.
 
I

Ian Collins

Malcolm said:
Aha, we can do it

x = 2 //* C comment */ 2;
;

Now we've got x = 2 in C++ and x = 1 in C.

No, you get a syntax error in C99 and C++ (the ; is part of the comment).
 
R

Richard Bos

Ian Collins said:
No, you get a syntax error in C99 and C++ (the ; is part of the comment).

There are two semicolons. In C90, it's two statements; in C99, one.

Richard
 
A

adam.wieckowski

Aha, we can do it

x = 2 //* C comment */ 2;
;

Now we've got x = 2 in C++ and x = 1 in C.

just remove the first semicolon

x = 2 //* C comment */ 2
;

Now it should work on both
 
K

Keith Thompson

Malcolm McLean said:
Aha, we can do it

x = 2 //* C comment */ 2;
;

Now we've got x = 2 in C++ and x = 1 in C.

Congratulations. Was there some reason you snipped my sample code and
added your own? Or did you just stop reading at that point?
 
M

Malcolm McLean

Keith Thompson said:
Congratulations. Was there some reason you snipped my sample code and
added your own? Or did you just stop reading at that point?
Stopped reading.
 
K

Keith Thompson

Malcolm McLean said:
Stopped reading.

Ok. In the future, please do me (and the rest of your readers) the
courtesy of reading the whole article before you post a followup.
You'll look less foolish.
 
M

Malcolm McLean

Keith Thompson said:
Ok. In the future, please do me (and the rest of your readers) the
courtesy of reading the whole article before you post a followup.
You'll look less foolish.
I tend to assume that code will reflect the English. In your case you said
that a double slash was a legal C construct if the second slash was part of
a comment. So I assumed that the code was designed to illustrate that. That
was my mistake, but the general rule is that people read code if they intend
to run it or alter it, comments if they intend to understand what it does.
 
A

Al Balmer

, but the general rule is that people read code if they intend
to run it or alter it, comments if they intend to understand what it does.

Not me. As a maintenance programmer, I read comments as a last resort,
and even then I don't believe them. They're only a possible clue to
the original programmer's state of mind at the time.
 
F

Flash Gordon

Malcolm McLean wrote, On 10/04/07 23:50:
Keith Thompson said:
Malcolm McLean said:
news:[email protected]... [...]
Congratulations. Was there some reason you snipped my sample code and
added your own? Or did you just stop reading at that point?

Stopped reading.

Ok. In the future, please do me (and the rest of your readers) the
courtesy of reading the whole article before you post a followup.
You'll look less foolish.
I tend to assume that code will reflect the English. In your case you
said that a double slash was a legal C construct if the second slash was
part of a comment. So I assumed that the code was designed to illustrate
that. That was my mistake, but the general rule is that people read code
if they intend to run it or alter it, comments if they intend to
understand what it does.

That may be your rule. Personally I find that on language groups it is
best to read the code people provide so I know what they are talking about.
 
M

Malcolm McLean

Al Balmer said:
Not me. As a maintenance programmer, I read comments as a last resort,
and even then I don't believe them. They're only a possible clue to
the original programmer's state of mind at the time.
That's running or altering code.
 
D

Dave Vandervies

Not me. As a maintenance programmer, I read comments as a last resort,
and even then I don't believe them. They're only a possible clue to
the original programmer's state of mind at the time.

I can do worse than that; sometimes they're only a clue to how completely
clueless somebody who looked at the code later was.

A sample from the code I work with at my day job:
--------
//initialize to the min_threshold (maybe max would be better to
// avoid huge amounts of detections?)
/*Max threshold is only a good idea if you never want to adapt
below that, which kind of defeats the whole purpose of what
we're doing here. --DV
*/
for(i=0;i<N;i++)
values=min_threshold;
--------
The code was written by me; the // comment was written by another
programmer, and the /*...*/ comment was just added by me while making
a change to the code.

And no, he wasn't commenting it because he had to understand it to make
a change; it's entirely internal to a module in which all of the live
code past the external interface is mine.


dave
 
K

Keith Thompson

Malcolm McLean said:
Keith Thompson said:
Malcolm McLean said:
news:[email protected]... [...]
Congratulations. Was there some reason you snipped my sample code and
added your own? Or did you just stop reading at that point?

Stopped reading.

Ok. In the future, please do me (and the rest of your readers) the
courtesy of reading the whole article before you post a followup.
You'll look less foolish.
I tend to assume that code will reflect the English. In your case you
said that a double slash was a legal C construct if the second slash
was part of a comment.

Not quite. What I said was that *in C90* a double slash could be a
division operator immediately followed by the first character of a /*
.... */ comment. In C99 (which is also C), a double slash can
introduce a comment.
So I assumed that the code was designed to
illustrate that.

It was, after allowing for the C99 correction above.
That was my mistake, but the general rule is that
people read code if they intend to run it or alter it, comments if
they intend to understand what it does.

Here's what I wrote upthread, excluding quoted text. I encourage you
to read it all this time.
| // introduces a comment in C99.
|
| In C90, // could be a division operator immediately followed by the
| first character of a /* ... */ comment.
|
| For example:
|
| #include <stdio.h>
| int main(void)
| {
| char *messages[] = {
| "// comments are not accepted",
| "// comments are accepted"
| };
| int i = 2 //**/ 2
| - 1;
| puts(messages);
| return 0;
| }

In your followup, you snipped everything starting with "For example:",
and wrote this:
| Aha, we can do it
|
| x = 2 //* C comment */ 2;
| ;
|
| Now we've got x = 2 in C++ and x = 1 in C.

So you wrote a code snippet that illustrates exactly the same point
that mine did (except that you incorrectly ignored the fact that C99
supports // comments). You also gave the false impression that I had
merely made a general comment and that you had jumped in to provide
the specific example that I had failed to provide.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top