Going from C to C++

C and C++ Going from C to C++

The C and C++ languages are very close. It may even surprise you that every program which can be compiled using a C compiler can also be compiled using a C++ compiler. However, there are several things which we write differently in C++. Now, you may be thinking that I'm contradicting myself since writing something differently usually discards all compatibility. Nonetheless, the C++ language provides backward compatibility for the C language, and as we're going to see, it's going to make our work much simpler.

Compatibility

In the introduction, I said that every program which can be compiled using a C compiler is also a valid C++ program. To make this statement more accurate, let's say that C++ prohibits a few, and precisely documented constructs, which are supported by C compilers. However, there's no reason to worry. The C++ developers made it so because the C languages support some operations whose results aren't clear at first sight. If you write your programs properly, you won't run into any issues. For completeness' sake, I'm going to describe these differences, i.e. the constructs which are forbidden in C++ aren't in the C language.

  • Implicit conversions from pointers of the void type to any other pointer type.
void* ptr;
int* num = ptr;
  • New C++ keywords (template, new, delete, class, and others)
  • Prohibition of the goto or switch statement to jump over a variable definition.
goto label1;
int variable;
label1:
// the rest of the program
  • Redeclaration of a global variable.
  • We had to specify void to the parameters of the parameterless functions. If we omitted it, C assumed that the function had a variable number of parameters. We don't have to specify void in C++ and this sort of function isn't able to accept any parameters.
  • C++ treats constant values more strictly than C.
  • The C99 and C11 standards added a few constructs to the C language which aren't in the C++ standard. However, this doesn't mean that compilers don't support them!

Why did we learn C at all?

First of all, C is more suitable for system programming. Second of all, C++ allows for method overloading. This topic will be covered in the C++ course. Nevertheless, I'll cover it briefly now. We are able to declare multiple functions with the same name, but with different parameters:

int sum(int addend)
{
     return addend + addend;
}
int sum(int addend1, int addend2)
{
     return addend1 + addend2;
}

The compiler uses so-called name mangling to be able to distinguish between such functions. Internally, it adds various prefixes based on the function header. This means we don't know the real name of the function until the compiler gets to it. The C language doesn't use name mangling so we're able to do things like call C functions straight from the assembly language. However, there's one more thing of even higher importance.

Since the languages are so similar, most of the libraries available are written for C. It makes sense, though. Why write the same library twice if we can just take the library in C and use it with C++ without any issues? Now that that's been said, you know you'll be able to put your knowledge of C in use with C++.

This is why I recommend learning the C language first before moving on to C++. this is coming from a person who's been working with C++ for many years. There are no problems with going from C to C++. I'll describe all the changes you may encounter in this article.

So how to start with C++?

First of all, we're going to need an IDE for development. You may continue to use NetBeans, or download Visual Studio (recommended) or Eclipse. A tutorial on how to install Visual Studio is available in the second lesson of the C++ basic constructs course.

Feel free to read the rest of the differences you may encounter in C++. Some topics link straight to C++ programming course lessons. Those are topics with major changes for C++ so I won't be able to describe all that in just a few paragraphs. Those lessons will only include the appropriate material. Once you read them, you may jump straight to the object-oriented programming in C++ part of the course.

Memory management

C and C++ differ in memory management. From C, we know the malloc() and free() functions. Malloc() allocates space on the heap while free() frees that memory. C++ introduces the "new" and "delete" keywords which serve the same purpose. Keep in mind that these approaches cannot be combined!! The memory allocated using the C++ syntax (meaning using new) cannot be freed using the free() function and vice versa (memory retrieved using malloc() cannot be freed using delete). Instead of describing the syntax in detail, I'll direct you to the lesson about this: Managing dynamic memory in C++. It has the same exact contents as the corresponding article in C, however, it uses the newest syntax for allocating resources. Also, C++ introduces new "type" - references, which are covered in the References in C++ lesson.

Input and output

C++ uses objects to handle input and output. It doesn't matter if you don't know much about objects now. All you'll have to do before you continue is read the "Hello World" lesson in the C++ basic constructs and the article about User input sanitation in C++. You'll find detailed descriptions of objects in the C++ OOP course.

Working with strings

C++ has a special data type for storing text: string. It can contain text of any length. Strings can be concatenated naturally using the + operator. It's quite simple actually. All it is is an object which manages a char array internally (like the ones from the C language). More on this in the Strings in C++ article.

Defining structures

We used the typedef keyword to define structures in C. This way, we ensured that we didn't have to specify it's a structure at any other place. C++ adds typedef implicitly, so we may simply specify the structure name. The same rules apply for all typedef uses we know from C (e.g. enums).

struct person
{
        int age;
        int height;
}
int main()
{
        person carl;
        //.....
}

Libraries

We know a lot of libraries for the C language. The good news is that we can use them in C++ as well with no problems. You can even write library names in the same way as before and it'll work. The program will work correctly and the compiler won't have any problems with it. However, C++ introduces new library names, prefixed with "c" and without the ".h" extension. However, like I said, both of the approaches will work.

#include <stdlib.h>
#include <cstdlib> // the equivalent syntax

Forward variable declaration

We're used to declaring all of the needed variables at the beginning of a function or a block of code in the C language. C wouldn't even allow the following syntax:

for (int i = 0; ...)

According to the C standards, the i variable should be declared before the for loop. In C++ there are no restrictions like this, so we can declare variables anywhere in the code. Of course, we still have to declare a variable before we actually use it, otherwise, the compiler would throw an error.

You have now finished the article about the differences between the C and C++ languages. Feel free to jump straight to the OOP in C++ course.


 

 

Article has been written for you by David Capka
Avatar
Do you like this article?
No one has rated this quite yet, be the first one!
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn College The author learned IT at the Unicorn College - a prestigious college providing education on IT and economics.
Thumbnail
All articles in this section
C++ - Online courses
Activities (4)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!