Variable-defined type
Types of | meaning |
bool | Storing true or false |
char | Typically a single octet (one byte). This is an integer type |
int | Most natural machine integer size |
float | Single-precision floating-point values |
double | Double-precision floating-point values |
void | It indicates the type missing |
wchar_t | A wide range of character types. |
Variable definition in C ++
variable definition tells the compiler as a storage location and capacity variables created. Variable definition specifies the data type, and of the type comprising one or more lists of variables, as shown in FIG.
1 type variable_list;
Here, the type must be a valid C ++ data types, including characters, w_char, int, float, double , bool , or any other user-defined objects, and variable_list may comprise one or more identifiers names separated by commas. Here shows some valid statements
1 int i, j, k; 2 char c, ch; 3 float f, salary; 4 double d;
In int i, j, k this line; to declare and define variables i j and k,; instructs the compiler to create a type int named i, j and k variables.
It can be initialized in the declaration variables (assign initial values). Followed by the initialization program equals a constant expression, as shown below
1 type variable_name = value;
This is an example
. 1 extern int d = . 3 , f = . 5 ; // the d and f Statement 2 int d = . 3 , f = . 5 ; // type d and f initialization values, d =. 3, f =. 5 . 3 byte = z 22 is ; // initialization z type definition of the z = 22 bytes . 4 char X = ' X ' ; // initialization z type definition of the ASCII value of 58 x = x
No definition of the initialization procedure: Variables with static storage duration initialized with implicit NULL (value of 0 returns all); initial values of all other variables are defined
Variable declaration in C ++
Variable declarations for the compiler to provide the assurance that there is a variable of a given type and name have to compile the compiler proceed further without a complete variable details. Variable declaration makes sense only at compile time, the compiler needs real variables defined in the linker.
When you use multiple file and available when a file link to a program which define variables, variable declaration is useful. You will use the extern keyword to declare a variable anywhere. Although you can declare multiple variables in a C ++ program, but it can only be defined once in a file, function or block of code.
Try following examples, wherein the variables are declared at the top, it has been defined in the main function
. 1 #include <the iostream> 2 the using namespace STD; . 3 . 4 // variable declarations: . 5 extern int A, B; . 6 extern int C; . 7 extern a float F; . 8 . 9 int main () { 10 // variable declarations: . 11 int a, B; 12 is int C; 13 is a float F; 14 15 // true definition 16 a = 10 ; . 17 B = 20 is ; 18 c = a + b; 19 20 cout << c << endl ; 21 22 f = 70.0/3.0; 23 cout << f << endl ; 24 25 return 0; 26 }
When the above code is compiled and executed, it will produce the following results
1 30 2 23.3333
The same concept applies to function declarations, which provide you with the function name in the statement, and can be given that the actual definition in any other location. E.g
1 // variable declarations 2 int FUNC (); . 3 int main () { . 4 // function Call . 5 int I = FUNC (); . 6 } . 7 . 8 // function definition . 9 int FUNC () { 10 return 0 ; . 11 }