C++ for Programmers_ Basic Syntax in C++ Cheatsheet _ Codecademy
C++ for Programmers_ Basic Syntax in C++ Cheatsheet _ Codecademy
User Input
std::cin , which stands for “character input”, reads user int tip = 0;
input from the keyboard.
Here, the user can enter a number, press enter , and
std::cout << "Enter amount: ";
that number will get stored in tip .
std::cin >> tip;
Variables
// Initialize a variable
score = 0;
Arithmetic Operators
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/basic-syntax-in-cpp/cheatsheet 1/4
10/7/24, 1:42 PM C++ for Programmers: Basic Syntax in C++ Cheatsheet | Codecademy
double Type
double is a type for storing floating point (decimal) double price = 8.99;
numbers. Double variables typically require 8 bytes of
double pi = 3.14159;
memory space.
std::cout can output multiple values by chaining them int age = 28;
using the output operator << .
Here, the output would be I'm 28.
std::cout << "I'm " << age << ".\n";
C++ provides two powerful features for memory int year = 2021;
manipulation:
References: aliases to existing variables
Pointers: store memory address as its value int &ref = year;
Reference variables are created using the & symbol.
& is also used to access the memory address of a
int *ptr = &year;
variable.
Pointer variables are created using the * symbol. * is
also used to obtain the value pointed to by a pointer std::cout << &year << "\n";
variable.
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/basic-syntax-in-cpp/cheatsheet 2/4
10/7/24, 1:42 PM C++ for Programmers: Basic Syntax in C++ Cheatsheet | Codecademy
Input and output make C++ programs more interactive. #include <iostream>
#include <iostream> must be placed at the
beginning of the program to access input and
output. int main() {
std::cout is the “character output” and it is used int age;
together with << to print to the terminal.
std::cout << "How old are you? ";
std::cin is the “character input” and it is used
std::cin >> age;
together with >> to read user input.
std::endl or \n can be used to insert a new std::cout << "You are " << age << "
line. years old.";
return 0;
}
Variables
Variables are used to store and retrieve data. When int number = 100;
declaring a variable, it must be given a data type and a
name.
Multiple variables of the same type can be declared in a char letter;
single statement using a comma-separated list. letter = 'c';
Variables can be declared with the const keyword,
which prevents their value from being changed later.
const int pi = 3.14;
Program Structure
C++ programs run line by line and generally follow the #include <iostream>
same program structure:
using namespace std;
#include statements at the beginning of the
program, which allow access to library
functionalities. int main() {
main() function, which is run when the program
cout << "Hello, world!";
is executed.
return 0 at the end of the main() function, return 0;
which indicates that the program ran without }
issues.
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/basic-syntax-in-cpp/cheatsheet 3/4
10/7/24, 1:42 PM C++ for Programmers: Basic Syntax in C++ Cheatsheet | Codecademy
Comments
Comments are notes left by the programmer that explain // I am a single-line comment
complex code. Comments do not affect the performance
of a program because they are ignored by the compiler.
In C++, there are two types of comments: /*
Single-line: begin with // . I am a
Multi-line: begin with /* and end with */ .
multi-line
As a rule of thumb, comments should always go above
the code they are commenting on. comment
*/
Print Share
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/basic-syntax-in-cpp/cheatsheet 4/4