Part 17 - Constants

For a complete table of contents of all the lessons please click below as it will give you a brief of each lesson in addition to the topics it will cover. https://github.com/mytechnotalent/Reverse-Engineering-Tutorial

So far we have created, debugged and hacked a simple string echo to the standard terminal. We will expand upon that example by adding a constant.

A constant in C++ is a value that will not change throughout program execution (unless hacked). It is used such that you have a declaration early in the code so that if your future program architecture ever changes you can redefine the constant in one place rather than having to update code all through your code base.

It is standard practice to code our constants in all CAPS so that when we see it referenced somewhere in the code we know that value is a constant.

We start with our second program in C++ which is our “Constant” program. Let’s dive in and break each line down step-by-step and see how this language works. We will call this example2.cpp and save it to our device.

#include <iostream>
 
int main(void) {
            cons tint YEAR = 2017;
 
            std::cout << YEAR << std::endl;
 
            return 0;
}

To compile this we simply type:

g++ example2.cpp -o example2

We simply then type:

./example2

SUCCESS! We see “2017” printed to the standard output or terminal!

Let’s break it down:

We utilize the const keyword to indicate a constant to which we assign it the integer value of 2017.

We then utilize the cout function to print it to the standard output or terminal and add a new line with the endl function.

That’s it! Very simple.

Next week we will dive into Debugging Constants.

results matching ""

    No results matching ""