Codzify Web Stories

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

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.

Previous Next



NEWSLETTER

Coding Bytes by Codzify

Welcome to Coding Bytes, your weekly source for the latest coding tutorials and insights in small byte sized content.

Join 798+ Subscribers

Subscribe on LinkedIn


Monetize your Passion Today!

Join Codzify Affiliate Program, Earn 30% Commission on Course Sales, Get ₹449.7 or $5.41 per course Sale! No Follower Requirement. Monetize Your Passion Today!

Register NOW!

Codzify Youtube Channel

Ready to level up your Full Stack App development Skills? Check out Codzify Youtube Channel for interatcive video content.

Subscribe Codzify







Curated Mock Tests

Prepare for your next Interview or Competitive Exam with our expert-led curated Mock tests in GATE(CSE) DSA, Flutter, HTML, C etc.

Explore Mock Tests

Codzify Web Stories

Codzify Logo

Explore Coding in Simplified Way with Codzify.com


Terms and Conditions    Cookie Policy   Refund Policy