C++ Variables

Variables are used to store some values (also known as data values). Each variable has a specific type in C++, and it determines the size and layout of the variable's memory. The variable are called 'symbolic' variable because they are named.

There are two values associated with a symbolic variable.

rvalue :- Its data value that is stored in a location in memory. It is also known as rvalue.

lvalue :- Its location value is the address in the memory on which the data is stored. It is also known as lvalue.

There are different types of variable in C++.

1. int :- It stands for the word named "integer" which stores whole number without decimals.

For Example :- 5, -5

2. char :- It stands for the word named "Character" which stores single characters.

For Example :- 'L' or 'f'

3. bool :- It stands for the word named "Boolean" which stores values either true or false.

4. double :- It is used to stores double-precision floating point value.

For Example :- 5.0, -5.5

5. string :- It stores text that is written under double quotation marks.

For example :- "Letsfindcourse30"

5. void :- The void type specifies an empty set of values. An object of type void cannot be declared.

Declaring Variables

To declare a variable, you must specify the variable type and you can specify the value to that variable at that time or later on.

Syntax :

  type variable = value;


Where the type can be (int, double, char, bool) and the name of the variable can be (eg var_1, lfc) and the equal sign assign the value to the variable.

Now lets see some examples - we are creating a variable named "number" and its type is "int" and the value is "25".



Example :

  int number = 25;
cout << number;


We can also first declare a variable and then we can specify its value later on.



Example :

  int number;
number = 25;
cout << number;

Constant

We use the keyword constant if we do not want to change the value of the variable or if we do not want to override our existing values. Constant is a keyword that is presented as const.

Example :

const int number = 25;  // number will always be 25
number = 5; // error: assignment of read-only variable 'number'


Exercise:-

1. Variable1= expression; which one is evaluated first?

A. variable1
B. expression
C. Both are evaluated equally
D. None of them

View Answer


2. Values stored in a computer system's memory as a sequence of

A. Characters
B. Strings
C. Bits (0 and 1)
D. Float numbers

View Answer



Program :-

C++ Program to Check Whether a character is Vowel or Consonant.

#include<iostream>
using namespace std;
int main()
{
    char c;
    int isLowercaseVowel, isUppercaseVowel;
    cout << "Enter an alphabet: ";
    cin >> c;
    // evaluates to 1 (true) if c is a lowercase vowel
    isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
    // evaluates to 1 (true) if c is an uppercase vowel
    isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
    // evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
    if (isLowercaseVowel || isUppercaseVowel)
        cout << c << " is a vowel.";
    else
        cout << c << " is a consonant.";
    return 0;
}


Output :

Enter an alphabet: u
u is a vowel.





Visit :


Discussion


* You must be logged in to add comment.