0% found this document useful (0 votes)
2 views4 pages

C++ for Programmers_ Basic Syntax in C++ Cheatsheet _ Codecademy

This document is a cheatsheet for basic syntax in C++ programming, covering user input, variables, arithmetic operators, and data types like double. It explains the use of references and pointers for memory manipulation, as well as the structure of C++ programs including input/output operations. Additionally, it highlights the importance of comments in code for clarity and documentation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

C++ for Programmers_ Basic Syntax in C++ Cheatsheet _ Codecademy

This document is a cheatsheet for basic syntax in C++ programming, covering user input, variables, arithmetic operators, and data types like double. It explains the use of references and pointers for memory manipulation, as well as the structure of C++ programs including input/output operations. Additionally, it highlights the importance of comments in code for clarity and documentation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10/7/24, 1:42 PM C++ for Programmers: Basic Syntax in C++ Cheatsheet | Codecademy

Cheatsheets / C++ for Programmers

Basic Syntax in C++

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

A variable refers to a storage location in the computer’s // Declare a variable


memory that one can set aside to save, retrieve, and
int score;
manipulate data.

// Initialize a variable
score = 0;

Arithmetic Operators

C++ supports different types of arithmetic operators that int x = 0;


can perform common mathematical operations:
+ addition
- subtraction x = 4 + 2; // x is now 6
* multiplication x = 4 - 2; // x is now 2
/ division x = 4 * 2; // x is now 8
% modulo (yields the remainder)
x = 4 / 2; // x is now 2
x = 4 % 2; // x is now 0

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.

Chaining the Output

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";

References and Pointers

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.

std::cout << *ptr << "\n";

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

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

You might also like