Compile and Execute: G++ Hello - CPP

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

 std::cout 

is the “character output stream”. It is pronounced “see-out”.

 << is an operator that comes right after it.

 "Hello World!\n" is what’s being outputted here. You need double quotes
around text. The \n is a special character that indicates a new line.

 ; is a punctuation that tells the computer that you are at the end of a
statement. It is similar to a period in a sentence.

 Compile and Execute


 Compile: A computer can only understand machine code. A compiler can
translate the C++ programs that we write into machine code. To compile a file,
you need to type g++ followed by the file name in the terminal:
 g++ hello.cpp
 The compiler will then translate the C++ program hello.cpp and create a
machine code file called a.out.
 Execute: To execute the new machine code file, all you need to do is type ./ and
the machine code file name in the terminal:
 ./a.out
 The executable file will then be loaded to computer memory and the computer’s
CPU (Central Processing Unit) executes the program one instruction at a time.
 Compile and Execute (Naming Executables)
 Compile: Sometimes when we compile, we want to give the output executable
file a specific name. To do so, the compile command is slightly different. We still
need to write g++ and the file name in the terminal. After that, there should be -
o and then the name that you want to give to the executable file:
 g++ hello.cpp -o hello
 The compiler will then translate the C++ program hello.cpp and create a
machine code file called hello.
 Execute: To execute the new machine code file, all you need to do is type ./ and
the machine code file name in the terminal:
 ./hello
 The executable file will then be loaded to computer memory and the computer’s
CPU will execute the program one instruction at a time.
As we write a C++ program, we can write comments in the code that the
compiler will ignore as our program runs. These comments exist just for human
readers.
Comments can explain what the code is doing, leave instructions for developers
using the code, or add any other useful annotations.

There are two types of code comments in C++:

 A single line comment will comment out a single line and is denoted with
two forward slashes // preceding it:

// Prints "hi!" to the terminal


std::cout << "hi!";
You can also use a single line comment after a line of code:

std::cout << "hi!";  // Prints "hi!"


 A multi-line comment will comment out multiple lines and is denoted
with /* to begin the comment, and */ to end the comment:

/* This is all commented.


std::cout << "hi!";
None of this is going to run! */
Review
Congratulations! 🙌

In this lesson, you have learned:

 Compilation and execution using the terminal.

g++ hello.cpp -o hello


./hello

 Single line comments can be created using //.


 Multi-line comments can be created using /* */.

Introduction to Variables
The "Hello World!" program simply writes to the screen. It does not read anything, calculate
anything, or allow for user input. That’s no fun!

Real programs tend to produce results based on some input that the user of the
program gives, rather than just outputting the same thing every time.

To read something from the keyboard, we first need somewhere in the computer’s
memory to store data. That is where variables come in.
A variable is simply a name that represents a particular piece of your computer’s
memory that has been set aside for you to store, retrieve, and use data.

In this lesson, we will learn about some of the basic data types:

 int: integer numbers


 double: floating-point numbers
 char: individual characters
 string: a sequence of characters
 bool: true/false values

Every variable has a type, which represents the kind of information you can store inside
of it. It tells your compiler how much memory to set aside for the variable, and it defines
what you can do with the variable.
"Every variable in C++ must be declared before it can be used!"
Suppose we are building a game and we want to keep track of a player’s score
that goes from 0 to 10. We need a variable!

Before we can use a variable, we must declare, or create, it. To declare a variable,


we need to provide two things:

 A type for the variable.


 A name for the variable.

So to declare an integer variable called score, we need to write:

int score;

 The int is the type of the variable.


 The score is the name of the variable.
 The ; is how we end a statement.

In C++, variable names consist only of upper/lower case letters, digits, and/or
underscores.

Note: C++ is known as a strongly typed language. If you try to give an integer


value a decimal number, you are going to get unexpected results, warnings, or
errors.
Step 2: Initialize a Variable
After we declare a variable, we can give it a value!
Suppose that we have declared an int variable called score, to set it to 0, we can simply
write:

score = 0;

 The score is the name of the variable.


 The = indicates assignment.
 The 0 is the value you want to store inside the variable.

Note: In C++, a single equal sign = does not really mean “equal”. It means “assign”. In
the code above, we are assigning the score variable a value of 0.
Combining Step 1 and Step 2
We can both declare and assign a value to a variable in a single initialization statement.

Suppose we have these two lines:

// Declare a variable
int score;

// Initialize a variable
score = 0;
We can actually combine these two lines into a single line of code:

int score = 0;


This means we are declaring an integer called score and setting it equal to 0.

Note: We only need to declare a variable one time! And it is highly suggested to
initialize a variable before using it later.
Arithmetic Operators
Computers are incredible at doing calculations. Now that we have declared variables,
let’s use them with arithmetic operators to calculate things!

Here are some arithmetic operators:

 + addition
 - subtraction
 * multiplication
 / division
 % modulo (divides and gives the remainder)

For example:
int score = 0;
// score is 0

score = 4 + 2;


// it is now 6

score = 4 - 2;


// it is now 2

score = 4 * 2;


// it is now 8

score = 4 / 2;


// and now 2

score = 5 % 2;


// and now 1
Note: The order of operations can be specified using parentheses. For example, the use
of parentheses in score = 5 * (4 + 3) sets score equal to 5 * 7 rather than 20 + 3.
But how would we know what that value is?

You can output the value by simply adding this code underneath:

std::cout << score << "\n";


Notice how when we want to output a variable, we don’t add double quotes
around its name.
Chaining
Now that we have outputted a variable and have also outputted things using
multiple couts. Let’s take a closer look at cout again.

If we have the code below:

int age = 28;

std::cout << "Hello, I am ";


std::cout << age;
std::cout << " years old\n";
It will output:

Hello, I am 28 years old


Notice how we use quotes around the characters in "Hello, I am " but not in age.

 We use quotes when we want a literal string.


 We don’t use quotes when we refer to the value of something with a name (like a
variable).

So now, is it possible to write the cout statements within a single line?

Yep! You can use multiple << operators to chain the things you want to output.

For the same code above you can also do:

int age = 28;

std::cout << "Hello, I am " << age << " years old\n";
This is called chaining.
User Input
Like we mentioned in the introduction, another way to assign a value to a variable is
through user input. A lot of times, we want the user of the program to enter information
for the program.

We have cout for output, and there is something called cin that’s used for input!

std::cout << "Enter your password: ";


std::cin >> password;
The name cin refers to the standard input stream (pronounced “see-in”,
for character input). The second operand of the >> operator (“get from”) specifies where
that input goes.

To see how it works, we have to try it with a program.


Challenge: Temperature (Part 1)
Now that we’ve learned about the basics of variables and cin, let’s write a program!

The mad scientist Kelvin has mastered predicting the weather in his mountain-side
meteorology lab.

Recently, Kelvin began publishing his weather forecasts on his website, however, there’s
a problem: All of his forecasts describe the temperature in Fahrenheit degrees.

Let’s convert a temperature from Fahrenheit (F) to Celsius (C).

The formula is the following:

C = (F - 32) / 1.8C=(F−32)/1.8

You might also like