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

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;

}

Posted Date: 9/20/2012 7:34:45 AM | Location : United States







Related Discussions:- Declaration of variables in cpp, Assignment Help, Ask Question on Declaration of variables in cpp, Get Answer, Expert's Help, Declaration of variables in cpp Discussions

Write discussion on Declaration of variables in cpp
Your posts are moderated
Related Questions
Write a program to print a digital clock which displays the time for 2 hours .

What is the difference between JavaScript and PHP? The difference lies with the execution of languages. PHP is server side scripting language, which means that it cannot intera

Your task is to create a programming system for a ferry. The ferry transports passengers and vehicles (cars, busses, lorries and bicycles). The ferry has space for 200 passengers a

write a c++ code to implement use of a constructor

write a C code that plots an object on the window and on the user''s click of mouse on the window, the object starts rotating continuously until the user presses the mouse again

A: In C++ there is a main difference among a template and a macro. Merely a macro is a string which the compiler replaces along with the value that was defined. For example #define

Ask4. Write a query to display the columns listed below. The query should list each customer in which the video rental is overdue. The Days_Overdue column should calculate the numb

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Bitwise Operations 1. Write a function that has an int parameter n, makes an integer mask having the bit 1 at the nth place from the rightmost bit, and returns the mask. For ex

Convert a COV/CPE (Micosoft Fax Cover Page) file to a TIF image file Project Description: I would like a small API written that will take a COV/CPE file and replaced the corr