C++ variables program
Variables are used in C++ to store and use the data within a program. In C++, variables are declared as follows:
Syntax:-
datatype variableName = value;
Example:-
#include <iostream> // Include the input/output headers that are required.
int main() {
// Declare variables
int a; // declaration of the integer variable "a"
float s; // declaration of the float variable "s"
char g; // The definition of a character variable called "g"
// Initialize variables
a = 25; // Assign a value to the "a" variable
s = 5.75; // Assign a value to the "s" variable
g= 'A'; // Assign a value to the "g" variable
// Print values
std::cout << "Age: " << a << std::endl;
std::cout << "S: " << s << std::endl;
std::cout << "Grade: " << g << std::endl;
return 0;
}
- We include the
<iostream>
header, which is necessary for using input/output streams likestd::cout
(output) andstd::cin
(input). - Inside the '
main'
function, we declare variables using their data types (e.g., 'int
', 'double
', 'char'
) followed by their names. - We use the assignment operator to initialize the stated variables with values.