Declaration of variables in cpp, C/C++ Programming

Assignment Help:

Declaration of Variables:

Variables are declared as follows:

int a;

float b;

 

Assigning value to variables:

int a = 100; Declaring and assigning is called initialization.

float b; Declaring a variable b.

b = 123.456; Assigning value to b.

int a=b=c;

 

Dynamic initialization of variables

int main ( )

{int a = strlen("apple");

cout<<"Total character in the string is"<

return 0;

}

Variables can be declared and initialized at the same time. int main ( )

{int a,b;

cin>>a>>b;

int c=a+b;

cout<<"c is" <

}

In C++  variables  can be declared  anywhere  in  the program  which  will  allow  to do dynamic initialization.  Remember for dynamic initialization the variables must be declared prior to dynamic initialization of new variable.

Reference Variable:

Syntax to define a reference variable is

datatype &reference_variable_name = variable_name;

int y;

int &x = y;

Now the value of x and y will be the same and also if any one of the value is changed the

change will be reflected in the other variable. y =10; Now x is also 10.

x = 20; Now y is also 20.

y = x + 10; Now x and y is 30.

 

Reference through pointer variable:

int x;

int *ptr = &x;

int &y = *ptr;  This is equivalent to int &y=x;

 

In function it is known as call by reference void f_ref(int &x)

{x = x +10;

}

int main ( )

{int m =10; f_ref(m); return 0;

}


Related Discussions:- Declaration of variables in cpp

What is a hash function, What is a hash function? Hash function: This ...

What is a hash function? Hash function: This is the method from the set 'K' of keys into the set 'L' of memory addresses.   H: K → L These are used to verify the address

C program to define power of a function, C program to define power of a fun...

C program to define power of a function: Write a program to use power of a function. void main() { int x,y,po=1,i,j; clrscr();   printf("Give x,y\n"); sca

Functions, what is the difference between call by reference and call by poi...

what is the difference between call by reference and call by pointer method?

Genetic disease, Many human diseases could be controlled by the knowledge o...

Many human diseases could be controlled by the knowledge of the gene’s structure and pattern. The human gene could be represented by four nucleotides. Each nucleotide is represente

How can we simulate the concept of multiple inheritance, Problem: (a) U...

Problem: (a) Using a class hierarchy of your own choosing, with at least TWO subclass levels, show (i) the use of abstract and concrete forms of Java class and method in you

Implement a binomial tree class, a) Implement a binomial tree class with a ...

a) Implement a binomial tree class with a method that calculates the value of an option passed in to the class. The binomial tree should not rely on specific features of the differ

Verifone pos offline mod vx670, Verifone pos offline mod vx670 Project D...

Verifone pos offline mod vx670 Project Description: I want a programmer who will build custom application for Offline VX670 to collect and save info+pin 1) Press the am

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd