C++ Variables, Literals, and Constants

C++ is a statically typed programming language that requires variables to be declared before they are used in a program. In C++, variables have a data type that determines the kind of values they can store.

Declaring Variables in C++

Declaring a variable involves specifying its data type and name. The data type determines the kind of values that can be stored in the variable. For example, if you want to store an integer, you would declare a variable as int.

1int variable_name;

Naming Variables in C++

In C++, variable names must follow a set of rules:

  1. Variable names can only contain letters, digits, and underscores.

  2. Variable names cannot start with a digit.

  3. Variable names cannot contain spaces or special characters.

  4. Variable names cannot be a reserved keyword in C++ (such as int, float, etc.).

It's also a good practice to use descriptive and meaningful names for variables, such as age instead of a, and to use camelCase or snake_case to make the names more readable.

Assigning Values to Variables in C++

Once a variable has been declared, you can assign a value to it using the assignment operator (=). For example, the following code declares an integer variable named age and assigns it the value 25:

1int age = 25;

You can also declare a variable and assign a value to it in the same line of code, as shown above. Alternatively, you can declare a variable and assign a value to it in separate statements:

1int age; 2age = 25;

Integers

Integers are whole numbers without a fractional component. In C++, integers can be declared using the following syntax:

1int variable_name;

For example, the following code declares an integer variable named age and assigns it the value of 25:

1int age = 25;

Floating-point Literals

Floating-point literals are numbers with a fractional component. In C++, floating-point numbers can be declared using the following syntax:

1float variable_name; 2double variable_name;

For example, the following code declares a floating-point variable named temperature and assigns it the value of 25.5:

1float temperature = 25.5;

Characters

In C++, characters are declared using single quotes. For example, the following code declares a character variable named grade and assigns it the value 'A':

1char grade = 'A';

Escape Sequences

Escape sequences are special characters that are used to represent newline, tab, or other special characters in a string. In C++, escape sequences start with a backslash (\) and are followed by a letter. The most commonly used escape sequences are \n for a newline and \t for a tab.

For example, the following code declares a string literal that contains a newline:

1cout << "Hello\nWorld";

This will print "Hello" on one line and "World" on the next.

String Literals

String literals are sequences of characters enclosed in double quotes. In C++, string literals can be declared using the following syntax:

1string variable_name;

For example, the following code declares a string literal named name and assigns it the value "William Doe":

1string name = "William Max";

Constants

Constants are values that cannot be changed once they have been assigned. In C++, constants are declared using the const keyword. For example, the following code declares a constant named PI and assigns it the value of 3.14:

1const float PI = 3.14;