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

Minimum shelfs, Write a program that finds the minimum total number of shel...

Write a program that finds the minimum total number of shelves, including the initial one required for this loading process

C, A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P...

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a string of the c

Assignment, Write a program that computes the cost of a long distance call....

Write a program that computes the cost of a long distance call. The cost of the call is determined according to the following rate schedules. a. A call made between 8:00 AM and

Write a recursive function with cylinders, I need to make a fractal trees i...

I need to make a fractal trees in OpenGL C++ . The method used is to write a recursive function with cylinders of different sizes and angles to create the tree trunk and branches.

Compiler Design - Limit In The Method Instructions, Raj is a newbie to the ...

Raj is a newbie to the programming and while learning the programming language he came to know the following rules: · Each program must start with ''{'' and end with ''}''

Polindrome, A palindrome is a string that reads the same from both the ends...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

C programs, 2. a) Define a structure to store roll no, name and marks of a ...

2. a) Define a structure to store roll no, name and marks of a student. b) Using the structure of Q2. a), above write a ''C'' program to create a file "student.dat". There must be

Basic C++ Velocity and Momentum Program, Write a program that does the foll...

Write a program that does the following: Calculates the Velocity and Momentum of an object. The formula for the velocity is V=d/t and the formula Momentum is m=mass*velocity. Your

Input and output in c++, C++ improves on many of C's features and provides ...

C++ improves on many of C's features and provides object-oriented programming capabilities used for software production, quality and reusability. C++ was developed by Bjarne Strons

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