Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C / C ++ language variable scope: local variables, global variables, file-level variables     - Generated characters using Java Videos (Programming)

- Ubuntu 10.10 install Oracle 10g graphic tutorials (Database)

- Terminal Linux command prints - echo (Linux)

- Spring AOP for logging (Programming)

- Python console achieve progress bar (Programming)

- 8 Git tips (Linux)

- systemd Power Management (Linux)

- The security configuration of Linux (Linux)

- MacBook Air install Ubuntu dual system (Linux)

- CentOS 6 Install Linux kernel source (Linux)

- C language preprocessor command (Programming)

- Subquery Oracle study notes (Database)

- Differences Shell scripting languages and compiled languages (Programming)

- Can not remember how to solve the problem under Ubuntu brightness setting (Linux)

- Atlassian Linux Shell Scripting the confluence remote backup methods (Linux)

- How to Disable Linux IPv6 (Linux)

- Linux using DenyHosts prevents ssh cracks (Linux)

- Oracle TAF Analysis (Database)

- CentOS7 iptables (Linux)

- Some of the bibliographic management tools to good use on linux (Linux)

 
         
  C / C ++ language variable scope: local variables, global variables, file-level variables
     
  Add Date : 2018-11-21      
         
         
         
  C / C ++ language variables into the global and local variables. On the basis of this division is visible range of variables or called scope.

1 local variables

Refers to the local variable is defined in {} variables, it is also within the scope of this range. Although the common local variables are defined in the body of the function, but also can artificially increase a pair of braces to define the scope of variables.

As follows:

void f ()
{
    float x = 0;
    {
        int a;
    }
}
Do not underestimate the scope of this problem, which influence than pure C ++ C larger. When the C language in a local variable goes out of scope, the compiler inserts a POP instruction takes to clean up variable stack space. In C ++, in addition to POP instruction, but also to call the destructor.

class MyClass
{
    MyClass () {}
    ~ MyClass () {}
};

void f ()
{
    {
        MyClass a;
    } // Here will be inserted into the C ++ compiler invoked ~ MyClass () code
    do_someting ();
}
C ++ compiler target before the end of a scope inserted automatically call ~ MyClass () assembler code.

Local variable scope is enforced by the compiler, so that once the action occurs outside access, a compile-time error will help programmers to debug errors.

2 Global Variables

Scope of global variables is the entire project, which is to participate in all linked files are visible. This can cause a problem - name conflict. For example, the following projects have three source files main.c, 1.c, 2.c.

main.c

#include < stdio.h>
int main (int argc, char ** argv)
{
    return 0;
}
1.c

int a = 1;
2.c

int a = 2;
Each file is compiled through, but will be given a link, because 1.c and 2.c use global variables of the same name. To this end, the global variable C language are given a very bad image. Even without the use of global variables doctrine prevailed over a wide range.

However, global variables in many cases it is necessary, at least it makes the problem becomes easy to use. For example, when a variable is a function of many parameters.

void f1 (int a);
void f2 (int a);
void f3 (int a);
Thus each function call is required to pass the variable a, when the increase in the number of such parameters, will make people become mad. Such as

void f1 (int a, int b, int c, int d, int e);
void f2 (int a, int b, int c, float g);
void f3 (int a, int b, int c, int d);
In this case, you need to save the state of the program are common, such as GDI libraries, OpenGL libraries. In this case the use of global variables to maintain state data is a very good choice. C ++ see this need, it is simply the state of these data and algorithms function to bind together to form a class concept, which simplifies code design.

3 file-level variables

Many times, in fact, the visible range variable programmers need neither the whole project, nor is an internal function, but is visible in the current file. C language aims to provide a static global variable. static global variable. The name can not fully reflect the scope of the variable range, it is a very bad name. And from the static keyword it is confusing.

static int a = 100;
Perhaps the designers of C language is to save the use of keywords, the keywords used in many different places have a completely different meaning. This design should be the eyes of the beholder thing, I personally feel that if you use other keywords here identified as internal, it would be easier to understand.

internal int a = 100;
In C #, like the existence of similar keywords to denote scope.

Closer to home, static global variable modified only in the definition of its internal file is valid, it can not be referenced within other files. The above examples of the following:
main.c

#include
int main (int argc, char ** argv)
{
    return 0;
}
1.c

static int a = 1;
2.c

int a = 2;
At this time, the project will be successful link. Because only a global scope called a global variable value of 2, a 1 is the only valid within 1.c.

4 C and C ++ compiler const constant bit different

C ++ compiler will automatically increase the static constant const keyword to the file level scope. The C compiler does not. The following code:

main.c

#include < stdio.h>
int main (int argc, char ** argv)
{
    return 0;
}
1
1.c

const int a = 1;
1
2.c

int a = 2;
Using the C ++ compiler can be successfully compiled and linked successfully, but using the C compiler is being given at the time of connection. For code portability, it is best to manually write on the static const.

main.c

#include < stdio.h>
int main (int argc, char ** argv)
{
    return 0;
}
1.c

static const int a = 1;
2.c

int a = 2;
The code is in C and C ++ compiler can compile and link successfully.
     
         
         
         
  More:      
 
- Linux terminal program running in the background (Linux)
- TPCC-MySQL Benchmark (Database)
- Elaborate .NET Multithreading: Thread Pool (Programming)
- Configuring s3c-linux-2.6.28.6-Real6410 appears Unable to find the QT3 installation (Linux)
- Iptables Instructions (Linux)
- Linux Troubleshooting: How to save the status of the SSH session is closed (Linux)
- Enterprise Hadoop cluster architecture - DNS installation (Server)
- How to view the Linux QPS (Linux)
- Some safety precautions of Linux servers (Linux)
- Use $ BASH ENV variable to mention the right way under Linux (Linux)
- Ubuntu 14.10 Apache installation and configuration (Server)
- Installation and configuration under Linux SVN server - Backup - Recovery - Scheduled Tasks (Server)
- Linux and Windows virtual machine shared folders ---- Linux install VMware tools (Linux)
- Storm basic framework for analysis (Programming)
- Ubuntu 14.04 installation configuration GNOME Classic interface (Linux)
- How to manage KVM virtual environments with command-line tools in Linux (Server)
- C # compiler to achieve functional use in the runtime (Programming)
- MySQL Data Types (Database)
- CentOS 7 server environment to quickly build Linux (Server)
- Configuring the remote Linux server SSH key authentication to automatically login in Mac OS X (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.