1. Introduction to C++: A Comprehensive Guide with Code Examples - 2023 2. Mastering Data Types in C++: A Comprehensive Guide with Codes and Examples (2023) 3. Learn About Variables and Types of Variables in C++ | Codzify.com 4. Control Statements in C++: A Comprehensive Guide for 2023 5. C++ Tutorial: Understanding Switch Statements with Codes and Examples in 2023 6. Understanding Memory Allocation and Pointers in C++: A Beginners Guide 7. Functions in C++ 8. Call by value and Call by Reference in C++ in depth 9. Array in C++ 10. 2d arrays in C++ 11. Classes and Objects in C++ 12. Static Functions in C++ 13. Constructors and Destructors in C++ - A Complete Guide with Examples 14. Mastering Copy Constructor in C++ - Shallow vs Deep Copy with Examples | Codzify 15. Understanding Friend Functions in C++ Made Simple! 16. Inline Functions in C++ 17. this Pointer in C++ 18. Mastering Inheritance in C++: Types and Examples Explained 19. Types of Inheritance in C++ 20. Polymorphism in C++ Explained with Codes and Examples in 2023 21. Templates in C++ 22. Getting the Value of a MultiMap in C++: Step-by-Step Guide with Examples 23. Multimap Find and Replace Operator in C++: Step-by-Step Guide - Codzify Topics 24. Exploring the Next_Permutation Algorithm without STL in C++ - Codzify Topics 25. C++ - The Difference Between Map and HashMap in STL 26. Updating Values in a std::multimap in C++ - Codzify Topics 27. Which data structure sorts the elements on insertion in C++ STL? 28. Can we implement Red Black Tree in c++ by STL containers? 29. How to Dynamically Declare an Array of Objects with a Constructor in C++ - A Step-by-Step Guide 30. What is the difference between a pointer and an object in C++? 31. Mastering Red-Black Trees with STLs Internal Implementation: A Step-by-Step Guide

Learn About Variables and Types of Variables in C++ | Codzify.com

Article by: Manish Methani

Last Updated: October 21, 2021 at 2:04pm IST
5 min 1 sec read

Introduction:

Variables are fundamental to programming in C++. They allow programmers to store and manipulate data in their programs. In this tutorial, we will provide a comprehensive guide on variables in C++. We will cover what variables are, how to declare and define them, and how to use them in your code.

What are Variables?

In C++, a variable is a named location in memory that holds a value. The value stored in the variable can be changed during program execution. Variables are used to store various types of data, such as integers, floating-point numbers, characters, and more.

Declaring and Defining Variables

To use a variable in C++, you need to declare and define it. Declaration informs the compiler about the name and type of the variable, while definition allocates memory for the variable. Here is an example:

int age; //declaration
age = 25; //definition

In this example, we declare a variable called age of type int. We then define age to have a value of 25. You can also declare and define a variable at the same time, like this:

float height = 5.7; //declaration and definition

In this example, we declare and define a variable called height of type float, with an initial value of 5.7.

Using Variables

Once you've declared and defined a variable, you can use it in your program. Here is an example:

#include <iostream>
using namespace std;

int main() {
   int age = 25;
   cout << "My age is " << age << endl;
   return 0;
}

In this example, we declare and define a variable called age of type int with a value of 25. We then print out the value of age using cout.

Types of Variables in C++

  1. Local Variables:

Local variables are defined within a function or a block of code and can only be accessed within that function or block. Once the function or block ends, the local variable is destroyed. Here is an example:

#include <iostream>
using namespace std;

int main() {
   int num = 10; // local variable
   cout << "The value of num is " << num << endl;
   return 0;
}

In this example, we define a local variable num of type int with a value of 10. We then print out the value of num using cout.

          2. Global Variables:

Global variables are defined outside any function and can be accessed by any function or block of code within the program. They have a lifetime that extends throughout the entire program. Here is an example:

#include 
using namespace std;

int num = 10; // global variable

int main() {
   cout << "The value of num is " << num << endl;
   return 0;
}

In this example, we define a global variable num of type int with a value of 10. We then print out the value of num using cout.

       3. Static Variables:

Static variables are declared with the keyword static. They retain their value between function calls and have a lifetime that extends throughout the entire program. Here is an example:

#include <iostream>
using namespace std;

void count() {
   static int num = 0; // static variable
   num++;
   cout << "The value of num is " << num << endl;
}

int main() {
   count();
   count();
   count();
   return 0;
}

In this example, we define a static variable num of type int with an initial value of 0 within the function count(). We then call count() three times and increment the value of num by 1 each time. We then print out the value of num using cout.

Conclusion

In conclusion, variables are a crucial part of programming in C++. They allow programmers to store and manipulate data in their programs. In this tutorial, we've covered the basics of declaring and defining variables, as well as how to use them in your code. By mastering variables in C++, you'll be well on your way to becoming a proficient programmer.

Learn Flutter, FlutterFlow,
Firebase & Angular.

Looking to learn how to create the production-ready apps
from scratch? Well, you are at the right place.

₹1299 or $15.56

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Explore Curriculum
₹1299 or $15.56

Flutter Mobile App Development Course

Explore Curriculum
₹1299 or $15.56

The Complete Angular Course

Explore Curriculum
₹1299 or $15.56

Dart Programming for Absolute Beginners

Coming Soon
FREE

Next.js course

Start Watching
FREE

Fundamentals of Computer Programming Languages

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer