Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 840639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:39:27+00:00 2026-05-15T05:39:27+00:00

All, Consider the following code: void func(void) { int a; printf (%d, a); }

  • 0

All,

Consider the following code:

void func(void)
{
    int a;
    printf ("%d", a);
}

int main(int argc, char **argv)
{
    int a = 3;
    func();
    printf("%d", a);
}

According to my understanding, the output should be:

<junk value><3>

Can anyone please confirm my understanding? My basic query is, does the compiler refer to the outer scope for a variable that has been declared but not defined?

Regards,
darkie

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:39:28+00:00Added an answer on May 15, 2026 at 5:39 am

    Your understanding is correct. func‘s a is initialized with stack trash.

    Each time you say int a;, it will create a new variable, with no relation to similarly named variables from enclosing scopes or variables from other functions higher up the call-stack.

    In your justification, you’re confusing Scope and Extent. c uses lexical scoping, so while main‘s “extent” (or liftime) exists through the execution of func, it is an entirely different scope, so it refers to an entirely different variable.

    Note that an “outer scope” is usually the braces outside of yours, with the outermost scope being file level.

    int a; // global
    void func(int a) {  // parameter
      int a;  // function local
      while (0) {
        int a;  // scoped in the 'while'
        if (true) { 
          int a;  // scoped in the 'if'
        }
      }
    }
    

    Each one of those ‘a’ variables shadows the other ‘a’s above it.

    The exception (maybe the source of your confusion) being a variable declared extern int a;. This variable specifically refers to a variable from somewhere else (different translation unit). An external declaration may be used to get the behavior you were not expecting:

    An external variable may also be declared inside a function. In this case you must use the extern keyword, otherwise the compiler will consider it a definition of a local variable, which has a different scope, lifetime and initial value. This declaration will only be visible inside the function.

    The exceptions to the “stack trash” rule are that static and heap allocated variables are zero initialized (if I am not mistaken, this is enforced by the standard).

    And also note that “garbage value” might be 3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries
Consider the following code: void f(byte x) {print(byte);} void f(short x) {print(short);} void f(int
Consider the following code public static void method(String[] srgs){ try{ }catch(){ System.out.println(Hello World +
Consider the following code: namespace DisposeTest { using System; class Program { static void
Consider the following code : void ListenerImpl::attach(boost::shared_ptr<ISubscriber> subscriber) { boost::unique_lock<boost::mutex>(mtx); subscribers.push_back(subscriber); } void ListenerImpl::notify(MsgPtr
Consider the following code: private static void GetData<TConnection, TCommand>( string connectionString, DataTable dataFromDbf, string
Consider the following C# code: class Program { static public void Print(string toPrint) {
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
The project holding the code has all default MSVS2008 settings. Consider the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.