Using C and C++ in modern systems

2901-ss.jpg

Before the C era

Early versions of computers in the mid-1940s had so little memory and computing resources that their programming was possible mostly on machine code. In 1954, a team led by John Backus at IBM invented the FORTRAN language which was the first widely used, high-level general-purpose programming language. LISP (1958), invented by John McCarthy and COBOL (1959), created by the Short-Range Committee, are still used in some areas. In the late 1950s, a committee of American and European computer scientists published a “new language for algorithms” - the ALGOL 60 report (“ALGOrithmic Language”), which played an important role in the further development of many programming languages. In 1964, John George Kemeny and Thomas Eugene Kurtz from Dartmouth College invented the BASIC programming language. This programming language became the working tool for millions of users on all types of computers. In 1970, Niklaus Wirth invented PASCAL; billions of lines of useful code were written in this programming language. As they say, there are no bad programming languages, there are only certain tasks for which these languages were created.

C

The C programming language was developed by Dennis Ritchie between  1969 and  1973 in the Bell Labs laboratories, and by 1973 most of the UNIX kernel, originally written in assembler PDP-11/20, was copied to it. C language was developed to solve the problems of previous languages such as B, BCPL, etc. “The C programming language” (sometimes called K & R after the initials of its authors) is a book written by Brian Kernighan and Dennis Richie, the latter of whom originally developed and implemented C language, and also jointly participated in the development of the Unix operating system. The first edition of the book was published early in 1978. The language was first standardized as ANSI C, and then this standard was adopted by the ISO Committee for International Standardization as ISO C, also known as C90.  C90 was updated to  C99 as new features were added. The C language was developed as a system programming language for which you can create a one-pass compiler.

C++

The language C++ originated in the early 1980s, when Bjarne Stroustrup, an employee of Bell Labs, came up with a number of improvements to C for his own needs. When, at the end of the 1970s, Stroustrup began working at Bell Labs on the task of queuing theory, he found that attempts to use the modeling languages existing at that time were ineffective, and highly efficient machine languages were too complicated. Recalling the experience of his dissertation, Stroustrup decided to supplement the language C with the possibilities available in the Simula language. Stroustrup added the ability to work with classes and objects to the C language. The first commercial release of C ++ took place on October 1985. And in 1998, the international standard of the C ++ language was ratified: ISO / IEC 14882: 1998 “Standard for the C++ Programming Language.”

Where to use C for the development of modern information systems

C is an efficient and compact programming language, great for writing OS kernels, device drivers, compilers, embedded applications, etc. It is a lightweight, flexible language that is fairly easy to learn. Thus, it can be assumed that it is better than C ++ for a certain type of programming. The main features of C are the following:

  • a simple language base from which many essential features have been moved to the standard library, such as math or file functions;
  • focus on procedural programming;
  • A type system that prevents meaningless operations;
  • use of preprocessor for the abstraction of operations of the same type;
  • memory access through the use of pointers;
  • a small number of keywords;
  • passing parameters to a function by value, not by reference (passing by reference is emulated using pointers);
  • the presence of pointers to functions and static variables;
  • name scopes;
  • structures and unions are user-defined collective data types that can be manipulated as one.

However, the C programming language does not have:

  • nested functions;
  • direct return of several values from functions;
  • automatic memory management tools;
  • built-in object-oriented programming;
  • functional programming tools.

Where it is better to use C++

C ++ has many other functions compared to C, including object-oriented programming (OOP). OOP provides the best tool for managing complex and large software solutions. C ++ is the most powerful and one of the most complex languages in the world. C++ can provide better efficiency and performance; however, with more complex features comes more responsibility. Many modern high-level programming languages like C++, C#, and Java are generally considered as successors of C. In C++, the object-oriented layer plays a crucial role, which increases the speed and productivity of the developer. Everything that is in C is present in C++. C++ is not new and was itself the basis for many languages, such as Python, Perl, and PHP. Nevertheless, it adds a few modern elements, in particular, the OOP, which make it a head taller than C. C++ is object-oriented. This leads to improved performance and code organization, which is successfully used to create complex applications. It has been successfully applied to fast server-side applications and software. It has the advantages of both high and low-level programming languages. C++ integrates well and works effectively with other languages. Virtually any system can compile and even more, can run C ++ code efficiently. In C++, the allocation and release of dynamic memory easier by using the keywords "new" and "delete". In C, you must use the functions alloc, calloc, and malloc to dynamically allocate memory, which requires extra effort and care when working with memory. C++ is an object-oriented language that allows the reuse of code and modules by using the standard library and many of the available solutions in the form of open-source and commercial libraries. C++ has a standard template library that includes various algorithms, data structures, and iterator, desktop user interfaces, 3D graphics, etc.

C vs C++

We cannot compare C and C++ directly because they are different languages based on different concepts. However, the following table shows some of the distinctions between these programming languages.

C C++

Type of language

Procedural oriented

Object-oriented

Level of abstraction

Lowest level

Low level

Memory management

Manual

Manual (new, delete)

Weight

Very lightweight, compiled

Lightweight, compiled

Performance

Very fast

About 1-5% less than C

Portability

Very high

Almost very high

Areas of applications

Good for embedded programming, system-level, high-speed systems, device drivers

Good for server-side applications, 3D graphics, gaming, networking

Typing

Weak and static

Static, nominative
Paradigm Imperative Multi-paradigm

Most valuable benchmarks are published. The performance of C is very high, and C++ performance almost is on the same level (of course if you use OOP in the right way).

Limpack BenchmarkC and C++ performance

Image source.

Any other high-level programming languages will be slower compared to C or C++. Some languages that use interpreters are dramatically slow. Web Solutions

C++ OOP: the object approach

When Bjarne Stroustrup created the first version of C++ it was called “C with classes”. So Classes and object-oriented paradigm is a base concept of C++. Let’s describe the main concepts of OOP. OOPs Concepts Image source. Class Classes are the basis of C ++. It is a user-defined data type. The class defines a new data type that combines code and data. Syntax class is similar to the structure. However, a class can also include functions, not just data. Object An Object is an instance of a Class. The object in Object-oriented programming (OOP) - the key concept of object-oriented design and programming technology; the embodiment of the abstract model of a separate entity (an object or concept) having a clearly expressed functional purpose in a certain area, belongs to a definite class and is characterized by its properties and behavior. Objects are the basic elements for constructing a program, and in OOP are treated as a set of objects that are in definite relationships and exchange messages. Encapsulation Encapsulation is one of the three main mechanisms of object-oriented programming. The point is that an object contains not only data but also the rules of their processing, executed in the form of executable fragments (methods).  Access to the state of the object is strictly forbidden, and from outside it can only be interacted with through a given interface (open fields and methods), which reduces connectivity. Abstraction The abstraction in object-oriented programming is the use of only those characteristics of the object that represent it with sufficient accuracy in this system. The basic idea is to present an object with a minimum set of fields and methods, and with sufficient accuracy for the problem being solved. Abstraction is the basis of object-oriented programming and allows you to work with objects, without going into their particular implementation. The fundamental idea is to separate irrelevant details of the implementation of the subroutine and characteristics that are essential for its correct use. Polymorphism Polymorphism is a concept in programming which uses a common interface for processing various specialized types. In contrast to polymorphism, the concept of monomorphism requires unambiguous comparison. Inheritance In object-oriented programming, inheritance is the mechanism of the formation of new classes based on the use of existing ones. The properties and functionality of the parent class pass to the class of a descendant (a child). The definition of a new class (derived class, subclass) can be based on the definition of an existing (base class, superclass).

C++11 standard and C++17 standard

The new standard C++11 includes additions to the core language and an extension of the standard library, including most of the TR1 - except, probably, the library of special mathematical functions. There was a need to develop the core of the C ++ language and significant improvements were made: added support for multithreading, improved support for generic programming, the unification of initialization, and work to improve its overall performance. The current ISO C++ standard is officially known as ISO International Standard ISO/IEC 14882:2017(E) – Programming Language C++. Improving usability for  the new C ++ standard touched on the following topics: initialization lists, exception handling, universal initialization, type inference, for-cycle collection, lambda functions and expressions, alternative syntax functions, improved object constructors, qualifiers default and delete, explicit substitution of virtual functions and finality, null pointer constant, strong typing enumerations, angle brackets, explicit conversion operators, template typedef, removing restrictions from the union, and identifiers with special meaning. The following were also added for extended functionality: templates with a variable number of arguments, new string literals, custom literals, multi-threaded memory model, intra-thread storage, explicit default job and removal of special methods, type long long int, static diagnostics, working size of with data elements in classes without creating an object, object alignment control and alignment requests, resolving implementations with the garbage collector, and attributes. But that is not all. In the C++17 standard, global changes were introduced: the specification of exceptions is now part of the type system, new with excessive alignment, mandatory copying, stricter calculation order, constant evaluation for all non-type template arguments, in “for” can be begin and end of different types.

Conclusion

Linux, Php, and Git are popular projects developed with C. On the other side OpenOffice, firefox, Clang, and Photoshop were developed with C++. Both languages are great to create complex and effective systems. As Linus Torvalds says: “C++ is a horrible language. It’s made more horrible by the fact that a lot of substandard programmers use it, to the point where it’s much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do *nothing* but keep the C++ programmers out, that in itself would be a huge reason to use C.” C and C++ are just two approaches to solving programming work. These two tools are designed to solve different problems, and choosing between them is quite simple. If you want to program the Unix kernel, drivers for the operating system, or program embedded systems, then your choice is C. If you want to create a desktop application, a server-side solution, a library for computer vision, or a neural network or AI, then choose C++.