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.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

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
include_once 'codzify-footer.php'; ?>