How To Define Variable in C++:

A variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.

In C++, there are different types of variables, for example:

  • int – stores integers, without decimals, such as 123 or -123
  • double – stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char – stores single characters. Char values are surrounded by single quotes
  • string – stores text, such as “Hello World”. String values are surrounded by double quotes
  • bool – stores values with two states: true or false
using namespace std;

//Based on scope we have local and global variables in c++:
//Local variable is declared inside a function whereas Global variable is declared outside the function.

int main() {
    //Built In Data Types
    int a = 5; 
    float f = 45.5f;    // Integer (whole number without decimals)
    double b = 5.99;    // Floating point number (with decimals)
    char c = 'D';         // Character
    //string d = "Hello";     // String (text)
    bool e = true;       // Boolean (true or false)

    //Declaring Multiple Variables
    int x = 5, y = 6, z = 50;
    cout << x + y + z;

    //Constant In C++
    const int myNum = 15;  // myNum will always be 15
    //myNum = 10;  // error: assignment of read-only variable 'myNum'
    return 0;

Declare Many Variables:

#include <iostream>
using namespace std;

int main() {
  int x = 5, y = 6, z = 50;  
  cout << x + y + z;
  return 0;

We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function we will use two variables to store the results, and the function will take pointer type data. So we have to pass the address of the data.

Leave a Reply

For News Subscribe Us!

Can curiosity may end shameless explained. True high on said mr on come. An do mr design at little myself wholly entire though. Attended of on stronger or mr pleasure.

You have been successfully Subscribed! Ops! Something went wrong, please try again.

© 2022 Code With AM