C and C++, or two languages with similar syntax, or one?

Please compose all posts in Emacs.

Moderators: phlip, Moderators General, Prelates

C and C++?

One language
5
8%
Two languages
41
67%
Claiming those two are one language is like saying an otter and a duck are the species!
15
25%
 
Total votes: 61

User avatar
Aperfectring
Posts: 252
Joined: Fri Sep 07, 2007 3:47 am UTC
Location: Oregon (happily)

C and C++, or two languages with similar syntax, or one?

Postby Aperfectring » Mon Feb 25, 2008 12:41 am UTC

I think of them as two languages.

I see C++ as the language that a person should use if they need (or want) the object-oriented aspects. Someone who doesn't have a firm grasp of object-oriented concepts can't (in my opinion) claim to be fluent in C++.

I see C as the language that a person should use if they want low level control. Someone who doesn't have a firm grasp of pointer (especially function pointers), casting, and bitwise operations can't (in my opinion) claim to be fluent in C.

Those two groups can be, and many times are, mutually exclusive.
Odds are I did well on my probability exam.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: C and C++, or two languages with similar syntax, or one?

Postby EvanED » Mon Feb 25, 2008 1:25 am UTC

I voted two, for I agree with you. [Edit: unless I misclicked, as there is only one vote for that option now, and one for the last one.]

Ever since I got into a debate on thedailywtf forums as to the validity of writing "C/C++", that has usually been jarring for me. I somehow get the feeling that for most occurrences, if it was changed to "C++ and Java", the "and" would have been explicitly written out...

User avatar
Aperfectring
Posts: 252
Joined: Fri Sep 07, 2007 3:47 am UTC
Location: Oregon (happily)

Re: C and C++, or two languages with similar syntax, or one?

Postby Aperfectring » Mon Feb 25, 2008 2:07 am UTC

I voted the otter/duck option.
Odds are I did well on my probability exam.

User avatar
TomBot
Posts: 228
Joined: Sun Jul 29, 2007 1:17 am UTC
Location: Illinois (UIUC)
Contact:

Re: C and C++, or two languages with similar syntax, or one?

Postby TomBot » Mon Feb 25, 2008 2:08 am UTC

Well, C++ is a strict superset of C. If you can write C, you can also write C++, though you aren't using all the features of C++. And if you can write C++, you can write C, just by not using certain features. So in that sense, they're the same, though my first sentence is more precise.

I haven't actually heard of a recent compiler that does one and not the other - I did once use an old Microsoft compiler like that, but it was (C) 1989. I think the only cases where C++ isn't a superset of C are: C++ doesn't let you implicitly cast a void pointer to any other pointer (as is common when malloc()ing), and C++'s // comment format can cause parsing differences in certain uncommon cases. (Though most C/C++ compilers allow // comments in C.)

Code: Select all

float f = 1.0 //* */ 2
+ 1;
// in C you get  1.5, in C++ you get 2.

User avatar
Aperfectring
Posts: 252
Joined: Fri Sep 07, 2007 3:47 am UTC
Location: Oregon (happily)

Re: C and C++, or two languages with similar syntax, or one?

Postby Aperfectring » Mon Feb 25, 2008 2:26 am UTC

TomBot wrote:Well, C++ is a strict superset of C. If you can write C, you can also write C++, though you aren't using all the features of C++. And if you can write C++, you can write C, just by not using certain features. So in that sense, they're the same, though my first sentence is more precise.


While that is mostly true, I argue that when you write C++ as C, you are writing C, and just using a C++ compiler, and vice versa. However, the C99 standard added a bunch of new functionality to C, which was not in standard C++. Although in practice compiler writers added the functionality to both, it was not standards compliant for C++.

TomBot wrote:I haven't actually heard of a recent compiler that does one and not the other - I did once use an old Microsoft compiler like that, but it was (C) 1989. I think the only cases where C++ isn't a superset of C are: C++ doesn't let you implicitly cast a void pointer to any other pointer (as is common when malloc()ing), and C++'s // comment format can cause parsing differences in certain uncommon cases. (Though most C/C++ compilers allow // comments in C.)

Code: Select all

float f = 1.0 //* */ 2
+ 1;
// in C you get  1.5, in C++ you get 2.

The C99 specification adds the "//" comment to C. Before C99 this was just another instance of the compilers not being standards compliant to make life easier on coders transitioning between the two languages. C++ is not, in my opinion, a superset of C, it is a branch of C from the past, which added object-oriented components.

Edit: *sigh* I swear that I meant to hit the preview button first.
Last edited by Aperfectring on Mon Feb 25, 2008 2:28 am UTC, edited 1 time in total.
Odds are I did well on my probability exam.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: C and C++, or two languages with similar syntax, or one?

Postby EvanED » Mon Feb 25, 2008 2:26 am UTC

TomBot wrote:Well, C++ is a strict superset of C. If you can write C, you can also write C++, though you aren't using all the features of C++. And if you can write C++, you can write C, just by not using certain features. So in that sense, they're the same, though my first sentence is more precise.

You forgot the word "almost" in there.

Getting most non-trivial C programs to compile under a C++ compiler is a decidedly non-trivial task.

I haven't actually heard of a recent compiler that does one and not the other...

GCC uses two separate executables for the front end, as does LLVM.

C++ doesn't let you implicitly cast a void pointer to any other pointer (as is common when malloc()ing)

Not just void* to T*, but interconversions between pointer types as well.

In addition to the following list, there are a lot of C99 features that C++ doesn't support by the standard. (But since no one seems to use most of C99 I guess it doesn't matter...) Other things that are valid C89 but invalid C++:
  • Assuming an enumerated type is the same size as an integer
  • Assuming character constants ('a') are an int instead of a char
  • Many more reserved keywords
  • C is much more lax about when you can use undeclared functions
  • An empty parameter list (such as int foo()) is the same as int foo(...) in C, but int foo(void) in C++
  • Declaring a global (or file-static) variable more than once is legal C, illegal C++
  • Along the same lines, declaring a typedef more than once in a translation unit is legal C, illegal C++
  • Int -> enumerated conversions are implicit in C, not in C++
  • Aggregate type names are in a separate namespace in C, not in C++; in C, it is legal to have a struct named foo and a typedef of another type named foo
  • For the same reason, it is possible to have code that silently changes meaning depending on which language it is compiled under (example)
  • Character strings are of type char* in C, but const char* in C++
  • C is more flexible on the size of initialized character arrays (e.g. char foo[3] = "foo"; is legal C but illegal C++; the array needs to be at least size 4)
  • If you have struct1 nested inside struct2, you can instantiate struct1 directly in C, but have to specify struct2::struct1 in C++ (same with other types nested in structs, such as enums)
  • C apparently allows declarations of structure types in a function argument list or return type (e.g. void foo(struct point {int x, y;} arg)); C++ does not

List from here. Also see here, though most of those are C99-specific.

(Edit: added a couple things to the list and the second link)

coppro
Posts: 117
Joined: Mon Feb 04, 2008 6:04 am UTC

Re: C and C++, or two languages with similar syntax, or one?

Postby coppro » Mon Feb 25, 2008 4:41 am UTC

EvanED wrote:[*] Aggregate type names are in a separate namespace in C, not in C++; in C, it is legal to have a struct named foo and a typedef of another type named foo
Actually, that's a bit misleading. In C++, classes, structs, and unions have their own namespace for names, but an unqualified lookup will find a class name if there isn't some other name shadowing it:

Code: Select all

class foo {};
void bar () {
  foo foobar; //valid, because foo finds the class
  int foo;
  foo = 4; //valid, because foo finds the int
}

void baz() {
  typedef int foo;
  foo barbaz = 4; //valid, because the lookup for foo finds an int.
  class foo foobaz; //valid, because the typename is qualified with the class-key
}

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: C and C++, or two languages with similar syntax, or one?

Postby EvanED » Mon Feb 25, 2008 5:31 am UTC

Fair enough. "My" point was that the following is legal in C, illegal in C++:

Code: Select all

struct S { int x; };
typedef int S;


This is true whether they are in a function or globally, as long as both are declared in the same scope.

btilly
Posts: 1877
Joined: Tue Nov 06, 2007 7:08 pm UTC

Re: C and C++, or two languages with similar syntax, or one?

Postby btilly » Mon Feb 25, 2008 7:40 am UTC

Heh, from my point of view C++ itself is two different languages. One for people who like OO, and another for people who like templates. :lol:
Some of us exist to find out what can and can't be done.

Others exist to hold the beer.

User avatar
Amnesiasoft
Posts: 2573
Joined: Tue May 15, 2007 4:28 am UTC
Location: Colorado
Contact:

Re: C and C++, or two languages with similar syntax, or one?

Postby Amnesiasoft » Mon Feb 25, 2008 9:42 am UTC

I'd call it one and a half languages. So I voted for two languages :P

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: C and C++, or two languages with similar syntax, or one?

Postby Sc4Freak » Mon Feb 25, 2008 12:20 pm UTC

From a syntax and inner-workings point of view, C++ is still very similar to C.

In terms of concepts and programming style, they may as well be different languages. If you are good at C, it doesn't mean you are good at C++. If you know C and don't know C++, you'll need to effectively relearn the language before you become fluent and are able to write good, idiomatic code.

I cringe every time I see someone trying to write C, but telling people that they're using C++ (effectively turning C++ into "C with Classes"). They may be syntactically similar, but I certainly consider them as completely different languages.

User avatar
rrwoods
Posts: 1509
Joined: Mon Sep 24, 2007 5:57 pm UTC
Location: US

Re: C and C++, or two languages with similar syntax, or one?

Postby rrwoods » Mon Feb 25, 2008 2:59 pm UTC

They are most certainly different languages with different purposes. C++ is *not* a strict superset of C.

C++ is an object-oriented language (or tries to be, depending on your perspective) used to write object-oriented code with object-oriented goals. C++ has polymorphism and dynamic binding. If you write your C++ code like your C code, then you need to drop C++ from your resume. Your potential employers will thank you.
31/M/taken/US
age/gender/interest/country

Belial wrote:The sex card is tournament legal. And I am tapping it for, like, six mana.

coppro
Posts: 117
Joined: Mon Feb 04, 2008 6:04 am UTC

Re: C and C++, or two languages with similar syntax, or one?

Postby coppro » Tue Feb 26, 2008 1:10 am UTC

rrwoods wrote:C++ is an object-oriented language (or tries to be, depending on your perspective) used to write object-oriented code with object-oriented goals. C++ has polymorphism and dynamic binding. If you write your C++ code like your C code, then you need to drop C++ from your resume. Your potential employers will thank you.
As Bjarne Stroustrup will tell you, C++ is a multiple paradigm language. You're free to code like C in C++, or you can try to code more like Java, except with things like memory management. C++ is not object-oriented; it's everything-oriented. C++ is very much the Perl of compiled languages (and not just in terms of the parseability!) - it's possible to do just about anything you want with it, though not necessarily easily (for proof, look at various Boost libraries).

User avatar
chaosspawn
Posts: 560
Joined: Thu Aug 09, 2007 6:38 pm UTC
Location: Waltham, MA

Re: C and C++, or two languages with similar syntax, or one?

Postby chaosspawn » Tue Feb 26, 2008 3:10 pm UTC

I voted 2, because I was taught the C++ is for objects paradigm. C I feel follows the basic main() and subroutine structure, of course it's also for inline assembly. Though being a Double-E rather than a CompSci means I tend to interact more with very low level programming. I view C as a macro language for assembly, and objects as structures with functions attached to them.
This space intentionally left blank.

User avatar
ash.gti
Posts: 404
Joined: Thu Feb 07, 2008 1:18 am UTC
Location: Probably a coffee shop.

Re: C and C++, or two languages with similar syntax, or one?

Postby ash.gti » Tue Feb 26, 2008 5:58 pm UTC

I think they are 2 languages.

Although, technically speaking, C++ is supposed to be a superset of C in actuality thats not 100% true. Because there are a few things that fundamentally should work in both but only work in one. And the variety of extensions C++ adds to C makes it almost a new language with similar syntax.
# drinks WAY to much espresso

Grumpy Code Monkey
Posts: 99
Joined: Tue Feb 19, 2008 4:10 pm UTC
Location: Blue Texas

Re: C and C++, or two languages with similar syntax, or one?

Postby Grumpy Code Monkey » Tue Feb 26, 2008 9:45 pm UTC

They are two different languages. For C++ to be a proper or strict superset of C, every legal C program must also be a legal C++ program, with identical semantics. This is not the case. Someone upthread pointed out most of the incompatibilities between the two.

Yes, they share syntax and semantics to a large degree. So do Pascal and Ada (or Oberon).

jimrandomh
Posts: 110
Joined: Sat Feb 09, 2008 7:03 am UTC
Contact:

Re: C and C++, or two languages with similar syntax, or one?

Postby jimrandomh » Thu Mar 06, 2008 6:35 pm UTC

More than two languages. C++ is unique among programming languages in having many different dialects, which are neither mutually compatible nor mutually intelligible. Different projects use radically different subsets of C++, ranging from "C with iostream" to "garbage collection and STL containers everywhere". While "C with iostream" is almost the same language as C, C has no equivalent to boost::auto_ptr, dynamic_cast<>(), or even std::string.

If C++ is like C for you, then you're doing it wrong.

User avatar
Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: C and C++, or two languages with similar syntax, or one?

Postby Jplus » Wed Apr 21, 2010 1:32 pm UTC

I agree to the majority above that they are not one language. "C/C++" doesn't exit, they are separate languages with some very important differences (I voted for the otter/duck option).

Many people who really are programming in C claim to be programming in C++, "but without classes". C++ is nothing like "C with classes"! To make things worse, such people often state things like "I find OOP not so useful because it slows things down, but I like the C++ std::vector". In the meanwhile, they are using C-style strings, C-style I/O and C-style casts...

Some snippets of code that are to greater or lesser extend compatible to C++ but should be avoided:

Code: Select all

#include <string.h>  // use <string> instead, with the std::string class
// if you really want the C-style strings, use #include <cstring> !
#include <malloc.h>  // you should use no weird allocation libs at all, the new and delete operators are great

const char *s = "format";
printf ("This is a %s string\n", s);
/*
string s = "format";
cout << "This is not a " << s << " string" << endl;
*/

double something = 4.0;
int something_else = (int) something;  // int something_else = static_cast<int> (something);


Also, some people seem to think that C++ is in some way higher than C. It isn't. It just allows for a lot more flexibility, including the use of OOP techniques. C++ can deliver exactly the same performance as C, and even offers some posibilities for optimization that C doesn't have. C++ is technically and conceptually superior to C in all aspects.

The only advantage of C over C++ is it's smaller compiler. Use C if you have to compile your project on very limited hardware and C++ in all other cases.
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

stephentyrone
Posts: 778
Joined: Mon Aug 11, 2008 10:58 pm UTC
Location: Palo Alto, CA

Re: C and C++, or two languages with similar syntax, or one?

Postby stephentyrone » Wed Apr 21, 2010 10:24 pm UTC

Two different languages. My favorite difference: a pre-increment expression is an lvalue in C++, but not in C.

C looks very much like a subset of C++, but it's very much not.
GENERATION -16 + 31i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

User avatar
Q8OP
Posts: 4
Joined: Wed Apr 21, 2010 11:05 pm UTC
Location: Los Angeles
Contact:

Re: C and C++, or two languages with similar syntax, or one?

Postby Q8OP » Wed Apr 21, 2010 11:57 pm UTC

I think my signature says everything about what I voted.


Return to “Religious Wars”

Who is online

Users browsing this forum: No registered users and 6 guests