how to recognize whether code is C or C++?

S

S Claus

Hi

here is a question, just out of curiousity:

If you are given a bunch of .c files, is there a way to recognize (by
just looking at them) whether the code in them is written in C or in C+
+?

What would you look for?

Thanks in advance,
 
P

Phlip

S said:
Hi

here is a question, just out of curiousity:

If you are given a bunch of .c files, is there a way to recognize (by
just looking at them) whether the code in them is written in C or in C+
+?

What would you look for?

class
iostream
istream, ostream, fstream
virtual
template
 
P

Phlip

S said:
class
iostream
istream, ostream, fstream
virtual
template

Then it might still be C++ without any of these things. It could have a function
inside a struct, for example. But such code - essentially "C-style C++" - is
questionable anyway...
 
J

Juha Nieminen

S said:
If you are given a bunch of .c files, is there a way to recognize (by
just looking at them) whether the code in them is written in C or in C+
+?

Parse it with a C parser and look if it gives errors.
 
V

Vladimir Jovic

S said:
Hi

here is a question, just out of curiousity:

If you are given a bunch of .c files, is there a way to recognize (by
just looking at them) whether the code in them is written in C or in C+
+?

What would you look for?

Thanks in advance,

Can you see what compiler is used?

But by just looking at the code, (as other suggested) I would search for
c++ only stuff, like cout, class, templates, etc
 
R

Richard Herring

Vladimir Jovic said:
Can you see what compiler is used?

But by just looking at the code, (as other suggested) I would search
for c++ only stuff, like cout, class, templates, etc

All of which are *not* reserved words in C, and might therefore be used
as variable names.
 
C

Christopher Dearlove

Phlip said:
class
iostream
istream, ostream, fstream
virtual
template

Looking for class and template is good obviously. I'm
not sure I'd bother checking virtual because while you
can put it in a struct that's not common and other things
are better quick checks.

But I'd first check all the #includes for any C++ headers.
Any file that has one is C++, and if any of the .h files
are thus C++, apply recursively. As most people put
their #includes near the top of a file, that's quick to check.

Looking for std:: and using will trap most C++ library
uses as well. (I suppose you should allow for whitespace
between std and :: - but does anyone put any there?)

The latter two are a broader brush than looking for
streams. (Most of my C++ source files don't use
streams.)
 
H

hanukas

All of which are *not* reserved words in C, and might therefore be used
as variable names.

But certaily not in the same way as in C++, so, look for the keywords
and see how they are used. OR, simply, "just look at the code" as was
suggested already.
 
D

Dhilip_kumar

Hi

here is a question, just out of curiousity:

If you are given a bunch of .c files, is there a way to recognize (by
just looking at them) whether the code in them is written in C or in C+
+?

What would you look for?

Thanks in advance,

Depends on the input set.

#include <stdio.h> /* C header
#include <iostream.h>

class xyz {
..
..
..
..
virtual xyzfun...
..
..
public:
..
..
..
private:

protected:
..
..
..

};
friend family relatives;
*/

int main()
{
printf("My C++ program \n");
return 0;
}

Now?

I agree with Juha Nieminen. Thats the best.

How will one determain if a given text is English, French or a mix? We
keep looking for key words? No. Make an English read the text.
 
V

Vladimir Jovic

Richard said:
All of which are *not* reserved words in C, and might therefore be used
as variable names.

Yes, thank you.

Few weeks ago, I tried to include a C header to a C++ program, but
couldn't because it had a variable named "class" in one of defined
structures.
 
P

Phlip

Richard said:
All of which are *not* reserved words in C, and might therefore be used
as variable names.

The OP said "looking at" the code. I was not suggesting a raw grep that
triggered a boolean result.
 
P

Phlip

Christopher said:
Looking for class and template is good obviously. I'm
not sure I'd bother checking virtual because while you
can put it in a struct that's not common and other things
are better quick checks.

I seem to recall that in C you cannot use 'virtual' as a keyword, and cannot put
a method inside a struct.
 
C

Christopher Dearlove

Phlip said:
I seem to recall that in C you cannot use 'virtual' as a keyword, and
cannot put a method inside a struct.

Indeed. But I would make a strong prediction that almost all uses of virtual
are inside a class rather than a struct, and therefore looking for class
will
more readily find C++. Yes, looking for virtual will find more C++, but the
only additional cases you'll find will be virtual inside struct, and as I
predict
there won't be many, I'd use a different heuristic to find more cases.

I'm not going to try it, but I suspect that the overwhelming majority of C++
will be found from
- standard C++ headers
- using
- std::
- class
- template
- inclusion of any user header categorised as C++ using the above, applied
recursively
 
C

Christopher Dearlove

Vladimir Jovic said:
Few weeks ago, I tried to include a C header to a C++ program, but
couldn't because it had a variable named "class" in one of defined
structures.

Anyone who wrote any such code in the last 20 years should have known
better.
 
A

Alf P. Steinbach

* Jeff Schwab:
Better than what? Writing perfectly valid C code?

It's unnecessary to make the header code incompatible with C++ on such a obvious
issue.

It's like when designing a door to your house: even if you and your wife are
both short people (short people, short people! :)[1]) you design that door so
that other people can just walk in without risking banging their head.

Or, I would, if I were that short and were designing a door.


Cheers & hth.,

- Alf

Notes:
[1] <url: http://www.google.com/search?q="short people">
 
P

Phlip

Jeff said:
Better than what? Writing perfectly valid C code?

Whereas C++ was designed for transparent interoperability with C, yes. Remain
aware, on the C side, how not to break that portability.
 
C

Christopher Dearlove

Jeff Schwab said:
Rather the reverse. virtual typically appears in abstract interfaces.
Since those interfaces have no private members, they are often declared as
structs.

That I'd regard as poor style (although of course that doesn't mean it
may not often occur). Putting aside that the sole purpose of this is to
save a few keystrokes, and keystrokes are cheap, and that it conflicts
with about the only useful reason to have both struct and class, to allow
struct to be used for basic C-style structs (and another case I note below)
and class when not, there is a more serious reason.

Lets's suppose I have

struct Base
{
// virtual declarations.
};

Now I write some code, in foo.cpp

#include "base.h";
#include "foo.h"

void foo(const Base & base)
{
// ...
}

and in foo.h

void foo(const Base &);

But I need a forward declaration of Base before that (no point including
base.h).
And the natural thing to use is

class Base;

However the last compiler I ran that code (or something similar) through
gave
me a warning that I'd mixed class and struct. And warnings are not good,
it's generally recognised as good style to avoid them.

So now I need to have it published whether Base is actually defined
using struct or class in order to painlessly forward declare it, regardless
of that this conveys no information ... no, thank you, let's stick to class
everwhere (as that is the usual convention for something that's not a
C-style struct, or possibly something never used in an interface, which
ironically ,in view of where this came from, means that private base
classes without virtual may have no problem being structs).

While on this topic there is another problem, which is if Base is actually
a typedef, which doesn't forward declare well (something that could do
with fixing). Currently also defining a base_fwd.h may be the least bad
solution
to that. Of course you could also do that just to specify struct Base, but
that
I think would be the worst idea in this posting.
 
C

Christopher Dearlove

Jeff Schwab said:
Better than what? Writing perfectly valid C code?

Better than writing perfectly valid C that you should know is invalid
C++ (anyone who isn't aware of that there is a C++ and it uses class
a lot should know better). It's just professional to recognise that
there are dual purpose compilers, and also that in any but the
most extreme cases there is the possibility of your C code being
reused as C++ one day (or just you may upgrade to a compiler
that decides to warn you about class) because few of us can see
the future clearly enough to be certain otherwise.
 
A

Alf P. Steinbach

* Jeff Schwab:
It's unnecessary to make the header code compatible with C++ without any
particular reason.

Agreed, not a good idea to add unnecessary work for dubious future return.

But it's no work at all avoiding an obvious incompatibility.

So it's just poor craftsmanship, indicative of general incompetence, to have
that particular incompatibility.

It's like when designing a door to your house: even if you and your
wife are both short people (short people, short people! :)[1]) you
design that door so that other people can just walk in without risking
banging their head.

That depends on the header. If the header is meant to be used in C++
code, then there are plenty of other issues that need to be taken into
account, as well. If, OTOH, the header is meant only to be used from C,
then why waste mind-space trying to write in a shared subset with some
other language?

There's no extra work in avoiding the keyword 'class'.

And even a case where you absolutely know the code will only be used with a C
compiler, avoiding a keyword like 'class' helps to establish a general coding
style that promotes compatibility for other later code, which is good.

Should we all start avoiding variables called "id",
just in case we ever need source compatibility with Objective-C++?

Uhm, there's a good argument that no-one should be a house-painter, cause if
everyone chose to be house-painters, well, bye bye food supply chain. :)

Or in other words, the "all" argument is invalid, arguing against a position
that hasn't been advocated by anyone.

You can read more about it here: <url:
http://www.nizkor.org/features/fallacies/straw-man.html>.


Cheers & hth.,

- Alf
 
N

Noah Roberts

Jeff said:
In the third place, if you really just want to be consistent, the thing
to change is the forward declaration, not the definition. The following
two declarations are semantically identical:

class base;
struct base;

They may seem the same, and should be the same, but at least one
compiler has alway rejected it if it doesn't match the actual definition.
In the fourth place, forward declarations smell bad. There's rarely any
good reason to start declaring code to work with classes whose
definitions have not even been seen.

Oh really? Guess Sutter shouldn't have recommended them on his GotD
site and his Exceptional C++ books.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top