Home > Back-end >  Global variables and local variables with the same name but different types of case analysis?
Global variables and local variables with the same name but different types of case analysis?

Time:09-25

# include
using namespace std;

Int a=4;
Int main () {
Double a=2.3;
Printf (" % d, % f \ n ", a, a);

system("pause");
return 0;
}

Global variable a is of type int, the local variable a is double,

Based on the principle of the local variable covering global variables, the result should be "2,2.300000",

The actual result is as follows:

The compiler without any warning,

Could you tell me how to analysis everyone a great god this?

CodePudding user response:

 printf (" % d, % lf \ n ", a, a); 

Lf type double use % format output,

When global variable names and local variable names, with the same global variable name shielding local variables, namely the global variable is invalid;

CodePudding user response:

Please refer to the type double in memory storage:
https://blog.csdn.net/slience_j/article/details/51965009

CodePudding user response:

The
reference 1/f, confident boy reply:
 printf (" % d, % lf \ n ", a, a); 

Lf type double use % format output,

When global variable names and local variable names, with the same global variable name shielding local variables, namely the global variable is invalid;


Float and double types of output format specifier is % f! Float type is automatically converted to double type, lf and % % f the result is the same,

CodePudding user response:

reference better * man reply: 3/f
Quote: reference 1/f, confident boy reply:

 printf (" % d, % lf \ n ", a, a); 

Lf type double use % format output,

When global variable names and local variable names, with the same global variable name shielding local variables, namely the global variable is invalid;


Float and double types of output format specifier is % f! Float type is automatically converted to double type, lf and % % f the result is the same,

Depends on the compiler, why don't you try and see if the same,
  • Related