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

C Programming and Data Structures 1. Define and explain flow chart with an example. 2. Write an algorithm to print all even numbers in descending order and draw the flowcha

Write a C function to solve the system of linear equations A x = y where A is an N by N matrix in the format of pointer-to-pointers and y is a vector in the format of a pointer. Th

While spending the summer as a surveyor’s assistant, you decide to write a program that transforms compass headings in degrees (0-360) to compass bearings. A compass bearing consis

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

Binary logical bit-wise operators  There are three logical bit-wise operators :                   &             and                 |              or

I need help programing this... In this exercise, you will create a program for the sales manager at Computer Haven, a small business that offers motivational seminars to local comp

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Write a function for finding out highest and lowest marks obtained by a student from an array contained student name and marks. For Example : GetHighestLowestMarks{Student{n}, Ma

I need CSGO Wallhack and Human Aimbot Project Description: Need a CSGO hack which can ESP Wallhack and Aimbot realistically. Must be VAC Proof, and ESL proof, as wel