gcc – Can you please give feedback on my C++ Code?


This Code uses the cstdio include for good performance

If you write C++ code, it is better to use the C++ way of doing things. While stdio is generally slightly faster than iostreams, if printing to standard output is not the performance critical part of your program, you shouldn’t switch to printf(), as you lose all of the benefits of iostreams, like type safety and interoperability with other C++ features.

Since you tagged the question C++20, you should know that std::format is even faster than stdio at formatting output, and combines the best of both worlds.

In the code you posted, you only output string literals, so no formatting is involved and likely all three methods will perform similarly.

I already mentioned that stdio is less type safe. And indeed you have an error: scanf("%i", ...) expects a pointer to an int, not an int8_t. This means you potentially overwrite the stack here. This problem would not have happened with iostreams, although std::cin >> input will likely not give the result you expect. When reading in an integer, just use int.

Again, this is likely premature and misguided optimization. A single int8_t will not be more efficient than an int, and in fact might be less efficient. Only if you store large amounts of values, such as in an array, will it make sense to use small integer types to save memory space and bandwidth.

You print strings without n at the end. This might cause the output to not appear immediately (standard output is usually line-buffered), and the next print statement will print on the same line, which is probably not what you want.