In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). For example,
int age = 14;
Here, age is a variable of the int data type, and we have assigned an integer value 14 to it.
Note: The int data type suggests that the variable can only hold integers. Similarly, we can use the double data type if we have to store decimals and exponentials.
The value of a variable can be changed, hence the name variable.
int age = 14; // age is 14
age = 17; // age is 17
Rules for naming a variable
A variable name can only have alphabets, numbers, and the underscore _.
A variable name cannot begin with a number.
It is a preferred practice to begin variable names with a lowercase character. For example, name is preferable to Name.
A variable name cannot be a keyword. For example, int is a keyword that is used to denote integers.
A variable name can start with an underscore. However, it's not considered a good practice.
Note: We should try to give meaningful names to variables. For example, first_name is a better variable name than fn.
Literals in C++ are fundamental constants used to represent fixed values directly in the code. They play a crucial role in data assignment, calculations, and data representation. Below is an overview of the different types of literals available in C++.
C++ supports several types of literals, each serving different purposes:
Integer literals represent whole numbers and can be expressed in different bases:
Decimal: Standard base 10 (e.g., 42)
Octal: Base 8, prefixed with 0 (e.g., 052)
Hexadecimal: Base 16, prefixed with 0x (e.g., 0x2A)
Integer literals can also have suffixes to specify their type:
u or U for unsigned
l or L for long
ll or LL for long long
Example:
int decimal = 42; // Decimal literal
unsigned int octal = 052; // Octal literal
long long hex = 0x2A; // Hexadecimal literal
Floating-point literals represent numbers with fractional parts and can be expressed in decimal or exponential form. By default, they are treated as double precision.
Example:
float pi = 3.14f; // Float literal
double e = 2.718; // Double literal
long double g = 9.8L; // Long double literal
Character literals are single characters enclosed in single quotes. They can also include escape sequences (e.g., newline \n, tab \t).
Example:
char letter = 'A'; // Character literal
char newline = '\n'; // Escape sequence for newline
String literals are sequences of characters enclosed in double quotes. They can contain plain text as well as escape sequences.
Example:
const char* greeting = "Hello, World!"; // String literal
const char* multiline = "Line 1\nLine 2"; // String with newline
Boolean literals represent truth values and are defined as either true or false.
Example:
bool isTrue = true; // Boolean literal
bool isFalse = false; // Boolean literal
The auto keyword in C++ lets the computer figure out the type of a variable for you. Instead of writing the type yourself, you can just use auto, and the computer will decide what type it should be based on the value you assign.
Here's how it works in a simple way:
Basic Usage: If you write auto x = 10;, the computer sees the 10 and knows that x should be an int (because 10 is an integer).
auto x = 10; // x is an int
auto y = 5.5; // y is a double (decimal number)
auto name = "Tom"; // name is a const char* (a string)
Easy in Functions: In functions, auto can be used to let the computer decide the return type, so you don’t have to think about it.
auto add(int a, int b) {
return a + b; // Compiler figures out the return type is int
}
Example: (Make sure to compile this example with the C++20 standard)
#include <iostream>
int main() {
// Basic example with different types
auto num = 10; // `num` is an int
auto price = 15.99; // `price` is a double
auto name = "Alice"; // `name` is a const char*
// Printing the values
std::cout << "num: " << num << "\n";
std::cout << "price: " << price << "\n";
std::cout << "name: " << name << "\n";
return 0;
}
In C++, the scope of a variable refers to the region of the program where the variable can be accessed or used. Understanding variable scope is crucial for managing visibility and lifetime of variables within your code. There are primarily two types of variable scopes in C++: local scope and global scope.
Local Scope
A variable defined within a function or a block (enclosed in curly braces {}) is known as a local variable. The key characteristics of local variables include:
Accessibility: Local variables are only accessible within the function or block where they are declared. They cannot be accessed outside that specific scope.
Lifetime: Local variables exist only during the execution of the function or block. Once the execution is complete, they are destroyed.
Example:
#include <iostream>
using namespace std;
void myFunction() {
int x = 5; // Local variable
cout << x << endl; // Accessible here
}
int main() {
myFunction();
// cout << x; // Error: x is not accessible here
return 0;
}
A global variable is defined outside of all functions and is accessible from any function within the same file. The characteristics of global variables include:
Accessibility: Global variables can be accessed from any function, making them available throughout the program after their declaration.
Lifetime: Global variables exist for the entire duration of the program's execution.
#include <iostream>
using namespace std;
int x = 10; // Global variable
void myFunction() {
cout << x << endl; // Global variable accessible here
}
int main() {
int x=20;
cout << x <<endl; //Local variable accessible here
cout << ::x << endl; // Global variable accessible here
return 0;
}