After all, why use C when programming in C++?

Asked

Viewed 1,480 times

4

I’m learning C++ and I can’t understand why people think C knows C++? Or are the differences between the two despicable?

I know the syntax is similar, but C++14 does things that C doesn’t, and aside from that C++14 syntax can sometimes be wrong in C and vice versa.

The very question is whether the difference can be dismissed?

Let’s assume my friend program in C I program in C++ if I try to compile his program in C++ has a great chance that the compiler will find a lot of syntax errors. Bjarne said in his book that C++ is like a C and object-oriented expansion, but the syntax of some of the things you can do in C can’t be done in C++.

As far as I read in Bjarne’s book at the beginning C++ was considered a type C with classes, and with the versions it has been getting a little further away from C, type with array. After I learned to use vector in C++ I prefer to use vector, because I don’t have to declare a fixed size, since with arrays C++ doesn’t work well if you don’t declare the array size, I can’t declare a int arr[linha][coluna] in C++, this is just an example, it seems that currently in some versions of C++ you can not use 100% of a C syntax because it will give error.

  • 2

    I didn’t really understand the question. C++14 is version, there already mixed version with language. C also has versions. There is something that C++14 does that earlier versions of C++ itself do not. It would be cool [Edit] and make the exact doubt clearer. And this whole thing with one thing that one does and another doesn’t, it doesn’t make much sense to me. I don’t know anything that can’t be done in any of them. If you are talking about something ready in the syntax of the language, it compensates [Edit] and explain better. Obviously if it’s a matter of syntax every language does "things the other one doesn’t do".

  • My biggest doubt is because I saw here that people who know C think they know C++ and then say they are different languages.

  • What doesn’t C do? C++ is an optimized C, which doesn’t mean it does things C can’t do either.

  • The whole arrays and vectors thing is bullshit. O C has no vectors, but Voce can make yours if you want. The following line compiles into c++ and in c. int matrix[2][2] = { {1,2}, {3,4} };. Would your question be as follows: "All the code you compile in C also compiled in C++?". Maybe that would be a better question.

  • I only discovered the question today, but I honestly didn’t understand why there was so much negativity. It is well written, it is an honest and just doubt of a student, and it was edited by the author to be improved. Those who were negative in the first review (if that was the case), should perhaps reconsider. :)

  • The problem with arrays is when you want the user to set the size of the array, and the data going into it

  • 2

    @Rhuancarlos I believe I should change the title of your question, was very confused, otherwise everything is ok

Show 3 more comments

1 answer

12


I’m learning C++ and I can’t understand why people think that maybe C knows C++?

I cannot make a very objective statement. I can confirm that this happens a lot. I can imagine that most cases are due to ignorance. It is more common in the area of what you imagine. It can also be because of pride/arrogance/stubbornness. A lot of people do it the way they want and they don’t care about anything else. In fact it is possible that it is a mixture of both, because those who think they already know everything and do not need to learn law in general are the ones who do not know.

But it could be pragmatism, too. I think you should program C++ as C++ and not as C. Generally speaking, good professionals of both think so too. But nothing prevents mixing styles and in some rare cases it is possible to really have a real advantage in making C within C++.

Or are the differences between the two despicable?

Not negligible in terms of syntax, but quite small. Almost everything that is valid in standard C is valid in standard C++ . Already style is well, but quite different.

I know the syntax is similar, but C++14 does things that C doesn’t, and aside from that C++14 syntax can sometimes be wrong in C and vice versa.

C+=20, C++17, C++14 are compatible with C++11, C++98, and earlier versions of the language. All these versions are 99.9% (number kicked, but it’s about that) compatible with C89, 99.8% with C99 and 99.6% of C11. This is how it will be with C++23 and the next versions that will come out. Maybe the compatibility of C and C++ will decrease a little, but it is tiny.

C++ specific syntax does not work in C, after all it is a super set.

Note that everything that one language does, the other also does, after all both are programming languages, and therefore Turing Complete. What exists is a different way of doing.

Let’s suppose my friend program in C I program in C++ if I try to compile his program in C++ has a great chance that the compiler finds a lot of syntax errors.

Generally speaking it is not to have problems. The few things incompatible actually shouldn’t even be used anyway. But if the code is C, keep playing with C. Or fully port and give up the original. If you put something of C++ in this code, it can no longer be used as a C code.

Bjarne said in his book that C++ is like a C and object-oriented expansion, but the syntax of some of the things you can do in C can’t be done in C++.

You need to interpret the text, see the details. That’s right, but it’s a simplification. In the book there are the few differences.

After I learned to use vector in C++ I prefer to use vector because I don’t have to declare a fixed size.

C++ has the type Array also for fixed size. It is usually best to use it when you want it. Of course the array C also works. There are cases to use C shape.

With arrays C++ does not work well if you do not declare the array size

In C as well. There is a way that gives some flexibility, but is not available in all compilers, it is complicated to do right and is not usually advantageous. It is more common for people to create their own structure or use a library when they need a variable size. C++ has ready, not always the most suitable.

It seems that currently in some versions of C++ can not use 100% of a C syntax because will give error.

Yes, 100% no, but almost that. Or 100% of what should actually be used in C.

My biggest doubt is because I saw here that people who know C think they know C++ and then say they are different languages.

Maybe C knows a part of C++. If it programs as it should in C++ is something else. But in fact, it’s a common mess. Some people think that the two languages are the same. Others think that C++ is an improved C, some even think it is optimized. There are those who think it is the object-oriented C, which is a damaging simplification. In fact they are very different languages with some binary and syntax compatibility between them.

Completion

C and C++ are very different languages. With a high degree of compatibility, C++ is almost a superset de C. Knowing C helps to know C++, but it’s better to learn to use the language from scratch to pick up each other’s style. So actually knowing C can get in the way of learning C++. There’s a lot of material out there, including books that teach C++ like it’s C.

Many people talk nonsense about the subject and few people really know how to program in one or the other and even less in both. I myself am not deeply aware of both, error in the details, but not error in general. I always need to consult sources before doing anything more sophisticated in any of them. What I don’t do is mix them up needlessly.

There’s a phenomenon in our environment (in any one, of course) where a lot of people think they know something without knowing it. This is called Dunning-Krugger effect. It is common for people not to know basic mathematics and Portuguese and to think that it is not necessary to be able to program. The person has no logical understanding, can not interpret text, follow instructions and have discernment of how to adapt them, search information, and even so leaves generating codes. So give a discount to the person who makes these errors of assessment of what is one thing and what is another.

Almost every complex project written in C that I know works perfectly on a C compiler++.

If you have more specific questions, you can ask them clearly.

  • 3

    This answer only proves that the AP question is fair. It can help many other students in the future. :)

  • Thanks for specifying, killed all my doubts, I decided to learn C++ from scratch, and I feel advantages in this, but sometimes I come across pseudo codes of C++ but I must get used to gradually and understand the style of C++

  • C++ IS NOT C-SUPERCONPIN. Several C-codes have no value or function differently in C++!

  • 1

    Yeah, half a dozen things don’t work well, it’s just I didn’t want to be pedantic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.