Skip to main content
1 of 2
Giuppox
  • 163
  • 1
  • 7

There are a few things that is worth pointing out from strictly the C++ point of view.

The code you have written, while still being valid C++, seems structured kind of in a C-fashioned way; there are a lot of C++ specific features that may be used to improve it.

C Headers

In C++ including stdio.h should be avoided. The C++ standard library provides the iostream header for IO operations. For example, the two following snippets are equivalent:

// C-fashion
#include <stdio.h>

void print_sum(int a, int b) {
    printf("%i\n", a + b);
}
// C++ fashion
#include <iostream>

void print_sum(int a, int b) {
    std::cout << a + b << std::endl;
}

I'm not sure whether the inclusion of C headers is still possible in C++, if it's just a compiler extension, or if it's just intended for backwards compatibility.

Allocation

Avoid using C dynamic allocation in C++ (malloc, calloc... etc stdlib.h functions). C++ introduces the new keyword. Refer to this SO question.

Headers

In the rare occasions where you really need a C header included. C++ has its own bindings for that.

#include <cstdio>  // C's stdio.h
#include <cstdlib> // C's stdlib.h

Take a look at this.

Naming convention

This is just a suggestion, you are free to use whatever naming convention you feel comfortable with as long as your code is not supposed to be part of a pre-existing project. Consistency is the first thing. I would rather use lowercase_with_underscores names for local variables and functions. I'd suggest you to take a look at Google C++ Style Format.

Apart from this there's the define thing of mindoverflow's answer

Giuppox
  • 163
  • 1
  • 7