Is It True That There Is No Need to Learn C Because C++ Contains Everything

Is it true that there is no need to learn C because C++ contains everything?

Overview:

It is almost true that C++ is a superset of C, and your professor is correct in that there is no need to learn C separately.

C++ adds the whole object oriented aspect, generic programming aspect, as well as having less strict rules (like variables needing to be declared at the top of each function). C++ does change the definition of some terms in C such as structs, although still in a superset way.

Examples of why it is not a strict superset:

This Wikipedia article has a couple good examples of such a differences:

One commonly encountered difference is
that C allows implicit conversion from
void* to other pointer types, but C++
does not. So, the following is valid C
code:

int *i = malloc(sizeof(int) * 5);  

... but to make it work in both C and
C++ one would need to use an explicit
cast:

int *i = (int *) malloc(sizeof(int) * 5)

Another common portability issue is
that C++ defines many new keywords,
such as new and class, that may be
used as identifiers (e.g. variable
names) in a C program.

This wikipedia article has further differences as well:

C++ compilers prohibit goto from crossing an initialization, as in the following C99 code:

 void fn(void)
{
goto flack;
int i = 1;
flack:
;
}

What should you learn first?

You should learn C++ first, not because learning C first will hurt you, not because you will have to unlearn anything (you won't), but because there is no benefit in learning C first. You will eventually learn just about everything about C anyway because it is more or less contained in C++.

Does any programmer have to know C? Yes, why? No, why?

No, you don't have to know C. But knowledge of C (or any other "close to the machine but not assembler" language) greatly enhances your potential as a programmer. Because you will understand a lot more of the inner workings.

And of course knowledge of assembler is also valuable. But in the above paragraph, I wanted to target the low end of computer language. Just because in modern languages, we take so much for granted (OO, extensive libraries, garbage collection to name a few). And yes it helps us programmers to work more efficiently. But it hides some of the machine aspects that we sometimes need to see and that's why it is so important that we need to know the inner workings.

Should I learn C before learning C++?

There is no need to learn C before learning C++.

They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own.

Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.

If you learn C++ you will eventually learn most of C with some differences between the languages that you will learn over time. In fact its a very hard thing to write proper C++ because intermediate C++ programmers tend to write C/C++.That is true whether or not you started with C or started with C++.

If you know C first, then that is good plus to learning C++. You will start with knowing a chunk of the language. If you do not know C first then there is no point focusing on a different language. There are plenty of good books and tutorials available that start you from knowing nothing and will cover anything you would learn from C which applies to C++ as well.

Please see further reasoning in this answer.

Is C code still considered C++?

In general, yes C code is considered C++ code.

But C is not a proper subset in a strict sense. There are a couple of exceptions.

Here are some valid things in C that are not valid in C++:

int *new;//<-- new is not a keyword in C
char *p = malloc(1024); //void * to char* without cast

There are more examples too, but you get the idea.

I previously wrote a more extensive answer in a similar question here.

Should I still learn C if I already know Assembly?

Often one of the main reasons given for learning C is that it brings you closer to programming at a low level

Well, scratch that one off, look at the others and decide for yourself. If you want to:

  • remain close to low level but gain portability
  • be able to work with the huge codebase that C has already
  • just learn something new

then you might want to learn C.

Learning C properly: yes or no?

If you are serious about programming, I think learning C is really important. It's the foundation of many languages and you'll encounter lots of source code written in C. However, we could tell so of Java, C++, etc..

IMHO, I think you should just continue to write your modules and learn new things as you need them. That's the best way of learning things

As a programmer with no CS degree, do I have to learn C++ extensively?

For practical advancement:

From a practical sense, pick a language that suites the domain you want to work in.

There is no need to learn C nor C++ for most programming spaces. You can be a perfectly competent programmer without writing a line of code in those languages.

If however you are not happy working in the exact field you are in now, you can learn C or C++ so that you may find a lower level programming job.

Helping you be a better programmer:

You can learn a lot from learning multiple languages though. So it is always good to broaden your horizons that way.

If you want more experience in another language, and have not tried it yet, I would recommend to learn a functional programming language such as Scheme, Lisp, or Haskell.

Should I learn C before learning Javascript?

No.

JavaScript may be one of the klunkiest languages ever, but it has one huge advantage over C: You can play with it. (I spent 10+ years coding in C. I had some fun, but I'd never call what I did "playing".)

My suggestion: Open up your favorite Web page, save it to disk, open up the JavaScript (or download it, if need be), and play. You'll learn a lot that way.

EDIT: Downvoters: Yes, there is a lot to like about JavaScript. But there's also a lot not to like.

Learn C first before learning Objective-C

I would learn C first. I learned C (and did a lot in C) before moving to Obj-C. I have many colleagues who never were real C programmers, they started with Obj-C and learned only as much C as necessary.

Every now and then I see how they solve a problem entirely in Obj-C, sometimes resulting in a very clumsy solutions. Usually I then replace some Obj-C code with pure C code (after all you can mix them as much as you like, the content of an Obj-C method can be entirely, pure C code). Without any intention to insult any Obj-C programmer, there are solutions that are very elegant in Obj-C, these are solutions that just work (and look) a lot better thanks to objects (OOP programming can make complex programs much more lovely than functional programming; polymorphism for example is a brilliant feature)... and I really like Obj-C (much more than C++! I hate the C++ syntax and some language features are plain overkill and lead to bad development patterns IMHO); however, when I sometimes re-write Obj-C code of my colleagues (and I really only do so, if I think this is absolutely necessary), the resulting code is usually 50% smaller, needs only 25% of the memory it used before and is about 400% faster at runtime.

What I'm trying to say here: Every language has its pros and cons. C has pros and cons and so does Obj-C. However, the really great feature of Obj-C (that's why I even like it more than Java) is that you can jump to plain C at will and back again. Why this is such a great feature? Because just like Obj-C fixes many of the cons of pure C, pure C can fix some of the cons of Obj-C. If you mix them together you'll receive a very powerful team.

If you only learn Obj-C and have no idea of C or only know the very basics of it and never tried how elegantly it can solve some common problems, you actually learned only half of Obj-C. C is a fundamental part of Obj-C. The ability to use C at any time and everywhere is a fundamental feature of it.

A typical example was some code we used that had to encode data in base64, but we could not use an external library for that (no OpenSSL lib). We used a base64 encoder, entirely written using Cocoa classes. It was working okay, but when we made it encode 200 MB of binary data, it took an eternity and the memory overhead was unacceptable. I replaced it with a tiny, ultra compact base64 encoder written entirely as one C function (I copied the function body into the method body, method took NSData as input and returned NSString as output, however inside the function everything was C). The C encoder was so much more compact, it beat the pure Cocoa encoder by the factor 8 in speed and the memory overhead was also much less. Encoding/Decoding data, playing around with bits and similar low level tasks are just the strong points of C.

Another example was some UI code that drew a lot of graphs. For storing the data necessary to paint the graphs, we used NSArray's. Actually NSMutableArray's, since the graph was animated. Result: Very slow graph animation. We replaced all NSArray's with normal C arrays, objects with structs (after all graph coordinate information is nothing you must have in objects), enumerator access with simple for loops and started moving data between the arrays with memcopy instead of taking data from one array to the other one, index for index. The result: A speed up by the factor 4. The graph animated smoothly, even on older PPC systems.

The weakness of C is that every more complex program gets ugly in the long run. Keeping C applications readable, extensible and manageable demands a lot of discipline of a programmer. Many projects fail because this discipline is missing. Obj-C makes it easy for you to structure your application using classes, inheritance, protocols and so on. That said, I would not use pure C functionality across the borders of a method unless necessary. I prefer to keep all code in an Objective-C app within the method of an object; everything else defeats the purpose of an OO application. However within the method I sometimes use pure C exclusively.



Related Topics



Leave a reply



Submit