How To Define Variable in C++:

How To Define Variable in C++:

Share It With Your Firends:

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
#include<iostream>
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.

Share It With Your Friends

Leave a Reply

Recent Posts

  • All Post
  • A.I.
  • AI
  • c
  • c++
  • computer
  • cryptocurrency
  • database
  • digital marketing
  • finance
  • hacking
  • HTML
  • java
  • Marketing
  • network
  • other
  • programming
  • python
  • react
  • social
  • Tools
  • Uncategorized
  • web devlopment
    •   Back
    • drawing In Python
DIY AI Voice Assistant In Python

September 8, 2023

Build your own AI assistant with Python! Learn to recognize voice commands, generate responses, and more. Create your virtual companion…

share it

Recent Posts

  • All Post
  • A.I.
  • AI
  • c
  • c++
  • computer
  • cryptocurrency
  • database
  • digital marketing
  • finance
  • hacking
  • HTML
  • java
  • Marketing
  • network
  • other
  • programming
  • python
  • react
  • social
  • Tools
  • Uncategorized
  • web devlopment
    •   Back
    • drawing In Python

Additional Content

CodeWithAM

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus luctus.

Products

Automated Chatbot

Data Security

Virtual Reality

Communication

Support

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Codewitham