Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cpp variables #5499

Merged
merged 20 commits into from
Oct 18, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 70 additions & 3 deletions content/cpp/concepts/variables/variables.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: 'Variables'
Description: 'A variable refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data. To create a variable, the type must be specified and a value must be assigned to it.'
Description: 'A variable refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data.'
Subjects:
- 'Computer Science'
- 'Game Development'
Expand All @@ -12,11 +12,11 @@ CatalogContent:
- 'paths/computer-science'
---

A **variable** refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data.
A **variable** refers to a storage location in the computer’s memory that one can set aside to save, retrieve, and manipulate data. Variables act as containers for storing information that can be changed or updated during the execution of a program.

## Declare a Variable

To create a variable, the type must be specified and a value must be assigned to it:
To declare a variable, the type of data the variable will hold must be specified, followed by the variable name. Optionally, a value can be assigned to the variable at the time of declaration:

```pseudo
type name = value;
Expand Down Expand Up @@ -46,8 +46,75 @@ The output would be:
I am 30 years old.
```

## Data Types in Variables

Each variable in programming has a type, which defines the kind of data it can hold. Here are some common data types:

- **int**: for integers (whole numbers), e.g.,

```cpp
int score = 10;
```

- **float**: A single-precision floating-point number, typically occupying 4 bytes (32 bits). It offers less precision and is used when memory efficiency is more important than accuracy, e.g.,

```cpp
float pi = 3.14159;
```

- **double**: A double-precision floating-point number, typically occupying 8 bytes (64 bits). It provides more precision and is the default choice when you need to store decimal numbers in C++.

```cpp
double pi = 3.14159265358979323846;
```

- **char**: A single character, e.g.,

```cpp
char letter = 'A';
```

- **bool**: for boolean values (true or false), e.g.,

```cpp
bool isAdmin = true;
```

## Example

The below example shows the usage of variables:

```cpp
#include <iostream>

int main() {
int age = 25;
double temperature = 36.6;
char initial = 'R';
bool isSunny = true;

std::cout << "Age: " << age << std::endl;
std::cout << "Temperature: " << temperature << "°C" << std::endl;
std::cout << "Initial: " << initial << std::endl;
std::cout << "Is it sunny? " << (isSunny ? "Yes" : "No") << std::endl;

return 0;
}
```

The code above generates the following output:

```shell
Age: 25
Temperature: 36.6°C
Initial: R
Is it sunny? Yes
```

## Codebyte Example

Run the codebyte to understand how variables work:

```codebyte/cpp
#include <iostream>

Expand Down
Loading