What is the difference between local and global scope in c++

Posted on 2009-02-17
Last Modified: 2013-12-14

What is the difference between local and global scope?
Please provide an example if possible, only a beginner so simple english would be good

Many Thanks
Question by:AA321
    LVL 86

    Accepted Solution

    'Local scope' basically means local to the function a symbol has been declared in, 'global scope' means global to the file a symbol has been declared in. As a simple example:
    #include <iostream>
    usinf namespace std;
    int global = 23;  // global to this file
    int main () {
      int local = 42; // local to 'main()'
      cout << "Global: " << global << " Local: " << local << endl;
      return 0;

    Open in new window

    LVL 86

    Assisted Solution


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now