Saturday 30 August 2008

Variable Declaration in C++


To declare a variable as a character, use the C++ keyword char followed by a valid C++ name. Here is an example:

char Gender;
With a byte variable declared like that, you can give the variable a starting value. Giving a value to a variable is referred to as initializing it. Initializing a variable is done with the = operator. The syntax used is:
VariableName = Value;
Since a char variable represents one symbol, to initialize it, enclose the initial value in single quotes. Here is an example:
char Answer = 'y';
You can also initialize a variable by including its value between parentheses as follows;

   char Answer('y');

No comments: