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

INTRODUCTION TO C++

NEW LINE
The escape sequence \n (backward slash and the letter n) generates a new line in a
text string.
Example:
std::cout << "Hello\n";
std::cout << "Hello again\n";

BASIC OUTPUT
std::cout is the “character output stream” and it is used to write to the standard
output. It is followed by the symbols << and the value to be displayed. It is
pronounced "see out".
Example:
std::cout << "Hello World!\n";
<< is an operator that comes after the "character output stream"
; is a punctuation that tells the computer that you are at the end of a statement

STRUCTURE OF C++ PROGRAM

Include libraries: #include <iostream>


The main () function: int main()
Beginning of fucntion: {

what the program does

End of function: }

Every C++ program must have a function called main (). A function is basically a
sequence of instructions for the computer to execute.
This main () function has all of the instructions for the program. This is where we
will be writing our code.

RETURN STATEMENT
The return statement is used to end a fucntion. If the program reaches tis
statement, returning a value of 0 is an indication to the operating systems that
the code executed successfully. THIS LINE OF CODE IS OPTIONAL.
Example:
return 0;

PROGRAM ON C++

When you program in C++, you mainly go through 4 phases during development:

CODE->SAVE->COMPILE->EXECUTE

Code: writing the program


Save: saving the program
Compile: compiling via the terminal
Execute: executing via the terminal
And repeat (debug error if needed)

HOW TO COMPILE

Compile: You call on the compiler by using ther terminal, which is the black panel
to the right of the code editor.
To compile a file, you need to type g++ followed by the file name in the terminal
and press enter.
The commpiler will then translate the C++ program 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 and press enter.
In this caase, our compiled file name is a.out. Putting it all together, we end uo
with the following:
./a.out

The executable file will then be loaded to computer memor and the computers Central
Processing Unit (CPU) executes the program one instruction at a time.

NAMING EXECUTABLES

Compile: to give the output executable file a specific name, we need to write g++
and te 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

LEAVNG COMMENTS FOR HUMAN READERS

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

You can also use a single line comment after a line of code:
std::cout << "hi"; // comment for human reader

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! */

You might also like