Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C vs C++ - advantages with c-language [closed]

Tags:

c++

c

C++, as the name suggests, is a superset of C. As a matter of fact, C++ can run most of C code while C cannot run C++ code.

There are several advantages with c++ compared with c - for instance

  • data can not be hidden in c language
  • c is more low level (which mean harder to understand and code - and that means more bugs)
  • c does not allow function overloading
  • c does not support exception handling
  • you can use functions inside structures in C++ but not in C

This list could certainly be much longer - but here comes my question: Is there ANY advantage with c-langauge compared with c++? Is there anything whatsoever that is better with c than with c++? Does c have anything that c++ lacks?

I do not know about this at all - but could c possibly be slighty faster than c++ due to fewer instruction-sets? A low-level language would possibly require fewer instructions by the processor.

like image 438
Björn Hallström Avatar asked Jan 01 '14 22:01

Björn Hallström


People also ask

What is C and advantages of C language?

C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc. C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation.

What is C AND WHY IS C is preferred for other languages?

C is one of the oldest and most fundamental programming languages, and it is extensively used all over the world. C is a fast, portable language with a large library. It is a middle-level language with the advantages of both low-level and high-level languages.

Is there any advantage of C over C++?

Much easier to interface with other languages. A lot of languages will let you call C functions directly. Binding to a C++ library is usually a much more elaborate job. Compiling C programs is faster than compiling C++ programs, because parsing C is much easier than parsing C++.


1 Answers

In simple, C and C++ are two different languages.

C++, as the name suggests, is a superset of C

No. This is not true. C++ is not a superset of C..

Is there ANY advantage with c-language compared with c++? Is there anything whatsoever that is better with c than with c++?

  • Static initialize is safe in C but not in C++, because in C++ static initialization can cause code to run, which depends on other variables having been statically initialized. It can also cause cleanup code to run at shutdown which you can't control sequence of (destructors).

  • C gives you better control over what happens when your code is executed. When reading seek out it is fairly straightforward to decipher one code is getting executed and when memory is just restart or primitive operations are performed.

  • C supports variable sized arrays on the stack. Which is much faster to allocate than on the heap. (C99 feature)

  • No name mangling. If you intend to read generated assembly code, this makes that much easier. It can be useful when trying to optimize code. De facto standard application binary interface (ABI). Code produced by different compilers can easily be combined.

  • Much easier to interface with other languages. A lot of languages will let you call C functions directly. Binding to a C++ library is usually a much more elaborate job.

  • Compiling C programs is faster than compiling C++ programs, because parsing C is much easier than parsing C++.

  • Varargs cannot safely be used in C++. They're not entirely safe in in C either. However they're much more so in the C++, to the point that they are prohibited in the C++ coding standards (Sutter, Alexandrescu).

  • C requires less runtime support. Makes it more suitable for low-level environments such as embedded systems or OS components.

  • Standard way in C to do encapsulation is to forward declare a struct and only allow access to its data through functions. This method also creates compile time encapsulation. Compile time encapsulation allows us to change the data structures members without recompilation of client code (other code using our interface). The standard way of doing encapsulation C++ on the other hand (using classes) requires recompilation of client code when adding or removing private member variables.

like image 178
haccks Avatar answered Sep 22 '22 14:09

haccks