Variables

Variables

Hola Prepsters are you ready to learn a very fascinating Problem known as variables.

So let me start by explaining what a variable is to all of you because I know you may be curious. Let’s work together to solve this problem.
  • A variable is a memory unit that is capable of storing data which can be modified (rewritten) at any point of time in a program.
  • Simply a variable is a name given to a memory location
 
How to solve a fascinating problem known as Variables.

Problem Statement:

Variables in any programming language should be named in such a way that the name itself clarifies its purpose. However, we cannot have a variable name having multiple words separated by space. Therefore, we use different approaches of defining variable names as given below:

In C++: this_is_a_variable

In Java: thisIsАVariable

Your task is to convert a C++ variable name into a Java variable name and vice versa and then return the converted variable name.

Note: Assume that a Java variable name never contains” “before any alphabet. In other words, if the given variable name contains “_ “before any alphabet, treat the given variable name as a C++ variable name and generate the output as a Java variable name and vice versa.

Input Specification:

input1: A string value representing the variable name

Output Specification:

Output: Return a string value containing the transformed variable name.

Example 1:

input1: modify_variableName

Output: modifyVariableName

Explanation:

While traversing the input string, an “is encountered first as compared to the “N. So, the input string is treated as a C++ variable and hence “modifyVariableName” is returned as the output string after removing the and converting the next alphabet. i.e – V to its uppercase.

Example 2:

Input1: thisisAvariable

Output: this_is_a_variable

Explanation:

The input string is treated as a Java variable and hence this_is_a_variable’ is returned as the output string after placing an underscore before all uppercase alphabets and finally converting the uppercase alphabets to lower case.

Output

modifyVariableName

For similar questions click on the given button.

Variables in C++

Variables

Here, in this page we will discuss about the variables in C++. A variable is a memory unit that is capable of storing data which can be modified (rewritten) at any point of time in a program. Simply a variable is a name given to a memory location.

Variables in C++ img

Variables in C++

A variable may have the following –

  • Variable Declaration
  • Variable definition (initialization)

In C++, all the variables must be declared before use.

Let us look at both of them in detail –

Variables in C++

Variable Declaration & Definition

Declaration

Variable declaration is the notification to the program/programmer that a particular type of memory may be required and we plan to call that memory with some name.

  • Memory creation (as per specified datatypes) happens at the time of declaration itself.
  • But the variables may have garbage values.
  • Variables can not be used before declaration

Example

int a,b,c;

Example program to demonstrate variable declaration

Run
#include<iostream>
using namespace std;

int main()
{
    int var; // variable declaration
    
    cout << "Value:" << var << endl; // garbage value
    cout << "Address of var: " << &var << endl; // a's assigned address
    cout << "Size of var: " << sizeof(var) << " bytes"; // allocated memory in bytes
    
    return 0;
}
Output
Value var: 10
Value: var2: 10.25

Variable Definiton/Initialization

In this stage, the user assigns value as per the requirement within the memory bounds i.e garbage value is overridden

Example

//declaration
int a;
float b;

// definition/initialization later
a = 10;
b = 78.9;

Example program to demonstrate variable initialization

Run
#include<iostream>
using namespace std;

int main()
{
    int var; //variable declaration
    cout << "Value: " << var << endl; // garbage
    
    var = 3;  // variable initialization/definition
    cout << "var value: " << var << endl;
    
    var = 5 + var;  //data overriding
    cout << "New var value: " << var << endl;
    
    return 0;
}
Output
Value: 0
var value: 3
New var value: 8

Declaration cum initialization 

Variable can be initialized at the time of declaration itself

Example

Run
#include<iostream>
using namespace std;

int main()
{
    // declaration & initialization at same time
    int var = 10;
    float var2 = 10.25;
    
    cout << "Value var: " << var << endl; 
    cout << "Value: var2: " << var2 << endl; 
    
    return 0;
}
Output
Value var: 10
Value: var2: 10.25

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Variables in C

Variables in programming

In computer terms a variable is the name of the memory location of the data which can be accessed with the help of that name and can be rewritten or used for calculations later on. Variable is a storage area. which is used to store the data. variable data is contained, which can be changed at any time during program execution. After declaring the variable it is given a value, this value is to be assigned in the many form. Like x = 5; or a=10;    Before using the variable it is necessary to declare in C. Values of variables are changeable. You can delete a value and enter another value.  You can also do this on compile time and dynamically(during program execution). For example:  Let’s say we have to store a student’s name and his roll number in variable. For this we take two variables, store the name of the student in a variable while the other variable stores the roll number.

variables in c

Rules for Variable:-

  • These stores the values inside it.
  • Variable is also the name of a memory location.
  • Variable is case-sensitive. For example, int a or int A both are different variable.
  •  Variable starts with any alphabet (a-z, A-Z) or underscore(_).
  • Variables name can be alphanumeric. for example, a1=5,  var1, var2
  • Variable does not allow space.
  • Variable name does not have any C keywords.
  • The name of any variable can not be start with any number.
  • Any upper case and lower case character can be used in any variable name.

 

Datatype of Variables:

A variable should be given a type in the C language,  which determines what kind of data the variable will hold. it can be:

  • char : it can hold a character in it.
  • int : it is used to hold an integer.
  • double : it is used to hold a double value.
  • float : it is used to hold a float value.
Data Type

DECLARATION OF A VARIABLE

While programming you first need to tell the computer/compiler the variable’s name and it’s data type, when you do that the compiler creates an empty memory reserved for that data .

In other words, we can say that

  • When we declare a variable , then it allocates memory according to the data type of variable.
  • After declaration of th variable, it takes Garbage Value inside it.

Syntax  for  Single Variable Declaration

data_type single_variable_name;

Example:

int a;
float b;
char c;

Source code:

#include<stdio.h>
int main()
{
   int a=25;
   printf("value of a : %d", a);
   return 0;
}

Output:

value of a : 25

Syntax for Multiple Variable Declaration

data_type multiple_variable_name;

For eg.

int a,b,c;

Source code:

#include<stdio.h>
int main()
{
int a=25,b=4,c=67;
printf("value of a: %d",a);
printf("\nvalue of b: %d",b);
printf("\nvalue of c: %d",c);
return 0;
}

Output:

value of a: 25
value of b: 4
value of c: 67

Variable Initialization

  • When the variable is initialized, then it allocates the memory according to data type of variable.
  • In variable initialization, a variable takes only a value.

Here, a, b, c are variables and int, float, char are data types. We can also provide the value to the variable during it’s declaration.

Syntax for Single Variable Initialization

data_type single_variable_name = value;

Example:

int a = 18 ;
float b = 17.21 ;
char c = "A" ;

Scope of variables

By scope of a variable we mean which part of the code a variable is accessible (visible) to .A variable can have many scopes in c let’s discuss some of them .

According to Scope, variables is divided into two categories:-

Local Variables
Global Variables

Scope of variable

Local Variable

Local variables are those variables that are defined in a small block of the program such as function, control statement block etc. Such variables are used only by the same block.

  • A variable inside the function/block is a local variable .
  • Local Variables is inside the function.
  • The default value of the Local variables is ‘garbage value’.

Example :

#include<stdio.h> 
int main()
{
     int a,b,c; // local variables
     a =10;
     b = 30;
     c = a + b;
     printf("%d",c);
}

Here a, b, c all are local variables and can not be used by any other function except main. On execution of the program the compiler prints 40

 

Global variable

As oppose to local variable a global variable is out side every function and is accessible to all the functions and the value of a global variable can be changed by any function.

Global variables are those variable whose scope is in whole program. These variables are defined at the beginning of the program.

  • Global variables are out of the function.
  • Global variables have visibility throughout the program.
  • The default value of ‘Global Variables’ is ‘0’.

Example :

#include<stdio.h>
int d=20; // global variable
int main()
{
      int a,b,c; // local variables
      a = 10;
      b = 30;
      d = d + 10;
      c = a + b + d;
      printf("%d",c);
      return c;
}

On execution of the program the compiler prints 70 .