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
Why many companies are switching their current business language to PHP? Where PHP basically used? PHP is rapidly gaining popularity and numerous companies are switching their

Inline Functions Imagine a c program, which reads disk records having employee information. If this is a payroll application each employee record data is probably processed by

Declaration of Variables: Variables are declared as follows: int a; float b;   Assigning value to variables: int a = 100; Declaring and assigning is called ini

#padovan string in java   program in java // aakash , suraj , prem sasi kumar kamaraj college program 1 : package test.padovanstring; public class PadovanStrin

Need Squid Web Proxy with ffmpeg to convert flv to h.264 Project Description: We need a squid proxy setup with the modification that will transcode FLV to h.264 in real-time,

How must runtime errors be handled in C++ - Runtime errors in C++ can be handled using exceptions. - This exception handling mechanism in C++ is developed to handle errors i

Implement the search property from catalogue menu option.  After selecting this option the user should be asked to specify the property using the following sub-menu: 1. Specif

Class Definition The following is the general format of defining a class template: class tag_name                  {                    public  :               // Must

write an algorithm for multiplication of two sparse matrices using linked lists

Hello, Do you have any idea for programming project to simulate a network attack (threat) mechanism or a defense mechanism. Can I get some suggestions for this. Please include th