Skip to main content

C++ Basics

Made by: CodingHome.
Published by: AT Products LLC.

Strings

A string is a sequence of characters representing text. Strings are typically enclosed in quotation marks and can be stored in variables for later use in a program.

C++
#include <iostream> #include <string> int main() { // Declare a string variable std::string str = "codinghome"; }

Integers and Floating Point Numbers

Simply said, Integers are used for numbers without a decimal, and Float is used for numbers with decimals, simply said. There is more to that but we are keeping it simple.

C++
#include <iostream> int main() { // Declare an integer variable int num = 10; // Declare a floating-point variable float fnum = 10.5; }

Print

Printing an output to the terminal/console:

C++
#include <iostream> int main() { // Declare an integer variable int num = 10; // Declare a floating-point variable float fnum = 10.5; std::cout << "num: " << num << std::endl; // <-- Prints the integer variable (variable num) std::cout << "fnum: " << fnum << std::endl; // <-- Prints the floating point variable (variable fnum) return 0; }

Other Data Types

As previously listed:

string
- Integers (whole number) (2 or 4 bytes).
int
- floating point number (4 bytes).
float
- Stores strings (Text) (use ').

But there are other data types in C++, such as:

double
- Stores strings (Text) (use ").
char
- floating point number (8 bytes).
bool
- true (1) or false (0) (1 byte).

Comments

Below is an example of comments.

C++
// This is a single line comment /* * This is a multi-line comment * that spans multiple lines */