Understanding local variables and global variables in LoadRunner

Source: Internet
Author: User



In the C language, the scope of variable validity is scoped to the variable. Not only for formal parameter variables, all the quantities in C language have their own scopes, which can be divided into local variables and global variables by different scopes.



A sample script that applies global variables and local variables in LoadRunner is as follows:






Figure 4.9 LoadRunner 9.5 Web(http/html) Protocol script code



The above code is written in LoadRunner 9.5, see figure 4.9 to the left, which contains a file name of "Globals.h", you can define a global variable in the header file, here we define a global integer variable, which is named "Icount", the initial value is 10, See Globals.h header file contents:


#ifndef _globals_h
#define _globals_h

//--------------------------------------------------------------------
Include Files
#include "Lrun.h"
#include "web_api.h"
#include "Lrw_custom_body.h"

//--------------------------------------------------------------------
Global Variables

int icount=10;//global variable

#endif//_globals_h


The action section code is as follows:


int a=8,b=15;    /*a,b AS global variable */
  
int max (int a,int b)/*a,b as local variable */
{
       return a>b?a:b; 
} 
  
int INCB () 
{
       ++b; 
       lr_output_message ("a=%d,b=%d in INCB () function", A, b); 
} 
  
Action () 
{
       int a=10;//local variable 
        lr_output_message ("a=%d,b=%d", b); 
       INCB (); 
       lr_output_message ("a=%d,b=%d", b); 
       lr_output_message ("Max (%d,%d) =%d", A,b,max (A, b)); 
       lr_output_message ("global variable icount=%d in Globals.h", icount); 
       return 0; 
}



In the Action section, the start declares two integer global variables, a and assigns the initial value 8,b to the initial value of 15. Next, the two integer comparison function max () is declared, and in the Max () function, there are two integer parameters, a and B. The INCB () function, which implements a global variable plus one, and then outputs global variables A and B. Action (), first declares the local variable A, and assigns an initial value of 10, Next, output local variable A and global variable B, and by calling the INCB () function to add one to the global variable B, colleagues output A and B. Here's a question for you, which is whether the value of a should output a value of global variable A or local variable a? Please give your readers a serious consideration. The subsequent output of Max (A, A, b) also has the above problem, and finally outputs the value of the global variable icount in the "globals.h" file.






The results of the above script execution are as follows:



Running VUser ...
Starting Iteration 1.
Starting action action.
ACTION.C (+): a=10,b=15
ACTION.C (one): A=8,b=16 in the INCB () function
ACTION.C (+): a=10,b=16
ACTION.C: Max (10,16) =16
ACTION.C: Global variable in globals.h icount=10
Ending action action.
Ending Iteration 1.
Ending VUser ...




Based on the results of the implementation, we can draw the following conclusions:



1. A global variable is a variable defined outside a function that does not belong to a source program file whose scope is the entire source program. A local variable is defined within a function, and its scope is limited to the function.



2. When the authority variable and the global variable have the same name, the global variable does not work within the scope of the local variable, as in the Action () function section, the value of a is 10, not the global variable 8, which answers the question we mentioned earlier. Of course, if a local variable with the same name is not declared in that part, the output is the value of the global variable, such as the value of the global variable icount in variable B and "Globals.h", then the value of the global variable is output.



Understanding local variables and global variables in LoadRunner


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.