What if any programming principles are better studied in C rather than C ++?

As a person who wanted to increase my fundamental programming skills, I decided to learn C ++ instead of C. What makes me ask the question: are there any fundamental skills that I leave in C that cannot be learned by learning C ++ ?

Related question: Should I learn C before learning C ++?

+4
source share
19 answers

If all you've ever used is object-oriented programming languages ​​such as C ++, then it would be wise to practice a little C. I find that many OO programmers tend to use objects like crutch and don't know what to do as soon as you remove them. C will give you clarity to understand why OO programming came about in the first place and help you understand when it is useful and when it is simply outwitted.

In the same vein, you will learn that you like not to rely on libraries to do something for you. By noting which features the C ++ developers have turned into libraries, you get a better idea of ​​how to abstract your own code when the time comes. It would be bad if you felt that you need to abstract everything in any situation.

However, you do not need to learn C. If you know C ++, you can drag yourself through most projects. However, looking at lower level languages ​​will improve your programming style even at higher levels.

+15
source

I don’t think there are any skills that you can learn in C, but not in C ++, but I would definitely suggest learning C first. No one can put C ++ in the head; it may be the most complex non-esoteric language ever created. C, on the other hand, is pretty simple. It is relatively easy to insert C into the head. C will definitely help you get used to things like pointers and manual memory management, much faster than C ++. C ++ will help you understand OO.

Also, when I say “Learn C”, it’s good to use the C ++ compiler and maybe use things like iostreams, if you want, just try to limit yourself mainly to C functions first. If you struggle and learn all the strange features of C ++, such as templates, RAII, exceptions, links, etc., you will be completely confused.

+7
source

Compare the following code examples (sorry, my C ++ is a bit rusty).

C ++:

int x; std::cin >> x; 

WITH

 int x; scanf("%d", &x); 

So what do you see above? In C ++, a value is inserted into a variable. That's cool, but what does he teach us? In C, we notice a few things. X has a funny and in front of him, which means that he is transmitted. This should mean that the scanf function really needs to know where x is located. Not only that, but he needs a format string, so there really shouldn't be any ideas about what address you give him and in what format he receives the information?

C allows you to discover all these wonderful things about how the computer and the operating system work if you work hard. I think this is cool.

+5
source

There is, of course, one thing: memory management . Take the following code, for example:

 struct foo { int bar; char **box; }; struct foo * fooTab = (struct foo *) malloc( sizeof(foo) * 100 ); for(int i = 0; i<100; i++) { fooTab[i].bar = i; fooTab[i].box = (char **) malloc( sizeof(char *) * 10 ); for(int j = 0; j<10; j++) { fooTab[i].box[j] = (char *) malloc( sizeof(char) * 10 ); memset(fooTab[i].box[j], 'x', 10); } } 

Using C ++, you simply do not allocate memory this way. As a result, such skills may remain untrained. This will certainly lead, for example, to lower debugging skills. Another disadvantage may be: reduced optimization skills.

C ++ code would look like this:

 struct foo { int bar; static int idx; vector< vector<char> > box; foo() : bar(idx), box(10) { idx++; for ( auto it = box.begin(); it != box.end(); it++) { it->resize(10,'x'); } } }; int foo::idx = 0; foo fooTab[100]; 

As you can see, there is simply no way to learn raw memory management with C ++ style code.

EDIT: "C ++ Style Code" I mean: RAII constructors / destructors, STL containers. In any case, I probably exaggerated, it would be better to say: it is much more difficult to study raw memory management with C ++ style code.

+5
source

I find it better to learn C memory management before trying to dive into C ++.

In C, you just have malloc () and free (). You can think of things as pointers. This is pretty straightforward: multiply sizeof () by # elements needed for the corresponding type, and use pointer arithmetic to jump everywhere.

In C ++, you have various types of reinterpret_cast <> (vs. dynamic_cast, etc.) that are important, but only make sense when you realize that various castings are designed to trap traps during multiple inheritance and others are so beautiful C ++ - isms. This is some kind of pain in order to understand (learn) why you need to put the word "virtual" on the destructor on objects that manage memory, etc.

Only one reason is why you should study C. I also find it easier (at first) to understand printf () and the potentially overloaded "<lt;" operator, etc., but for me the main reason is memory management.

+3
source

I would not say that you missed something fundamental. However, C and C ++ compatibility for a few non-fundamental examples.

Several C99 add-ons are not supported in C ++ or conflict with C ++ functions such as variable macros, complex literals, variable-length arrays, and native complex number types. The long long int data type and the bounding qualifier defined in C99 are not included in the current C ++ standard, but some compilers, such as the GNU compiler compilation [3], provide them as an extension. The long, long data type, along with the variational templates with which certain functionality of variational macros can be achieved, will be in the next standard C ++, C ++ 0x. On the other hand, C99 has reduced some other incompatibilities by incorporating C ++ functions such as // comments and mixed declarations and code.

+2
source

Simplicity C perfectly focuses the mind .

With fewer subjects to study in C, there is a reasonable chance that the student will learn this better. If you start with C ++, there is a danger of overcoming the other end without knowing anything about everything.

+2
source

It really depends on which of the C ++ subsets you learned. For example, you can pass in C ++ using only the iostreams libraries; which leaves you with no experience with the C stdio library. I'm not sure what I would call stdio a fundamental skill, but it is certainly an area of ​​experience that you need to know about.

Or, perhaps, manual access to C-style zero-terminated strings, and the set of str*() functions can be considered a fundamental skill. The practice of preventing buffer overflows in front of dangerous API functions is certainly worth it.

+1
source

It's just a shot in the dark, but maybe you use function pointers more in C than in C ++, and perhaps using function pointers, how the skill can be enhanced by starting to learn C. Of course, all these positive C ++ but as soon as you learn the lessons, it can be easy to just focus on them and skip other basic things.

You can, for example, create your own inheritance and polymorphism system in pure C using simple structures and function pointers. This requires more inventive thinking, and I think that it creates a deeper understanding of what is happening in the box.

If you start with C ++, there is a chance that you will miss these little details that are in C ++, but you will never see them, since the compiler does it for you.

A few thoughts.

+1
source

Depends on how you approach C ++. I usually recommend starting at a high level with STL containers and smart pointers, and this will not help you learn low level details. If you want to study them, you probably should just go to C.

+1
source

Playing the devil's lawyer because I think C is a good starting point ...

Personally, I started to study with C, algorithms, data structures, memory allocation, file manipulation, graphic routines ... I would call them elementary programming particles.

Then I learned C ++. To simplify, C ++ adds an object-oriented programming layer - you have the same goals as you, regardless of the language, but the approach and constructions you create to achieve them are different: classes, overloading, polymorphism, encapsulation and etc ...

For my part, there was no educated solution, it was just how my programming course was structured, and it got a good training program.

Another simplification ... C is basically a subset of C ++. You can "do C" with C ++, avoiding using the language features of C ++. In terms of language features. Libraries are different. I do not think that you will get more than just programming 101 without starting to use and create libraries, and this is enough to keep you busy for life.

If your goal is ultimately to learn C ++, then starting with "C" may be a logical beginning, the language is "less", but there are so many in "C" that you probably want to narrow your focus. You are engaged in a big animal, but if you get guidance, I see no good reason, despite the path that I took.

+1
source

Most C code can be compiled as C ++ code with minimal modifications. For examples of very few things that cannot be changed, see Why artificially restrict your code to C? . I do not think that any of them can be classified as fundamental, though.

0
source

C is essentially a subset of C ++. There are some differences that are related to some other answers, but essentially most C compiles as C ++ with a little modification.

The advantages of learning C are that it is a much more compact language and its learning is therefore faster. C also requires you to work at a relatively low level of abstraction, which is good for understanding how the computer really works. I personally grew up with assembly and switched to C from there, which I now see as a final advantage in understanding how compilers actually map a high level of abstraction to real machine code.

In C ++, you can quickly gain a foothold in higher levels of abstraction. This is useful for performance, but not necessarily for understanding.

So my advice is: learn C ++ if you have motivation, but first focus on the basic low-level constructs that are common to C. Only then go to higher levels such as object orientation, templates, extensive class libraries and and etc.

0
source

optimization theory

primarily because the ASM output of the C compiler is usually more predictable and usually correlates well with C. This allows you to learn / study why it is better to structure the code in a certain way, showing the actual generated commands.

0
source

You should study the data structures in C. First, it will make you really understand pointers that will help you deal with leaky abstractions in higher-level languages. Secondly, this will be an obvious benefit of the TOE.

0
source

Sytle Coding

By coding style, I don't mean {} and spaces, I mean how you organize data, classes, files, etc.

I started learning C ++, which usually contributes to creating classes and organizing things in a certain way. I could write c code just fine when it was needed, but it never looked like the c style code that someone who started with c wrote.

One of my OS classes required us to write a file system in C. My partner was originally a C guy, and I was amazed at the difference in code that we wrote. Obviously, Mine tried to use C ++ style encoding in c.

This is similar to when you first learn perl or ruby ​​or something else, and just try to write the same C # code, except for translating into this new language, and not using these languages.

While C is a subset of C ++, C programs and C ++ programs are so different that they are not really comparable.

0
source

Yes, simple structured programming.

With OOP all over the world, it's easy to forget that you can only create a sound system with modules and functions. The C module is pretty weak, but still ...

This is also one of the benefits of functional programming in my book. Many functional programming languages ​​have a strong modular system. If you need a more general function than simple functions can offer, there are always higher order functions.

0
source

If you want to learn new skills, and you have a good foundation of structured programming (SP), you should go for C ++. learn to think about this problem in an object-oriented (OOP) way (to define objects, methods, Inheritance, etc.), sometimes this is the hardest part. I was in the same situation as you did a few years ago, and I chose C ++, because studying a new paradigm, a new way of thinking and design, is sometimes more complicated than the code itself, and if you do not like C ++ or OOP , you can go back to C and SP, but at least you will have OOP skills.

0
source

I would choose C to learn about low-level programming primitives.

For OOP, there are so many other languages ​​that you need to learn before C ++, which are much easier to learn. This is not the year 1990 when the only OOP languages ​​were Smalltalk and C ++, and only C ++ was used outside the academic community. Almost every new programming language invented since then has objects, and almost all of them are less complex than C ++.

0
source

Source: https://habr.com/ru/post/1285701/


All Articles