FAQ: Variables - Step 1: Declare a Variable

This community-built FAQ covers the “Step 1: Declare a Variable” exercise from the lesson “Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Step 1: Declare a Variable

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

pls i would love to know why we have to compile and execute

1 Like

because a
machine cannot compile and execute by themself so we do

where do you insert the compile and execute code? i don’t know where to put it in the code. before the main? and is it both g++ variable.cpp and ./a.out?

Hello @kevinblomquist662045, and welcome to the forums.

The commands to compile, and then execute your compiled code do not go in the code itself. The commands are executed in the console (far right pane on the Codecademy Learning Environment).

You give a left mouse click in the console. That should place a blinking cursor next to the $ prompt. That is where you’ll type g++ variable.cpp. Then click <enter>. Wait a second or two, and a new $ prompt will appear on the next line down. That is where you type ./a.out and click <enter>. The first command compiles your code, and creates an executable file named a.out. Typing ./a.out tells the computer to execute the file.

1 Like

How is score an integer? Aren’t integers whole numbers?

score is the name of the variable. It was declared with the type int, so it may only be assigned to integer values. In the example, it is declared, but not assigned to any value.

1 Like


I do not understand Compile and Execute is to do ? Someone please help me through this tutorial, I tried typing : ./a.out at the end of the program but it still doesn’t work

Delete the ./a.out statement from your code.
Then, click the Run button (this will save your code).
Then, click in the far right terminal window (the window with $ and the cursor). This is the bash terminal. First, type in g++ variable.cpp and press enter (this will compile your program). Then, type in ./a.out and press enter (this will execute your program).

Outside of codecademy environment, you will write your programs in some editor (text editor or other editor) and save the code in a file with .cpp extension. Then, if you have the compiler installed on your computer, you will compile your code and then execute your code.

1 Like

I love how I declared the EXACT SAME variable in the file and when running the g++ variable.cpp AS DIRECTED, I am greeted with this junk instead as an error:

It will turn green after you compile and execute.

Edit: I can’t even copy/paste the junk for it to work… Codecademy at it’s finest, time to leave them for someone self given I can no longer proceed due to this poorly coded web app