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

  • Home
  • SEARCH
  • 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 8934139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:42:51+00:00 2026-06-15T09:42:51+00:00

Consider the following Objective-C code: @interface ClassA : NSObject { } -(void) printVal; @end

  • 0

Consider the following Objective-C code:

@interface ClassA : NSObject {

}
-(void) printVal;
@end


@implementation ClassA
-(void) printVal {
    int val;
    NSLog(@"%i", val);
}
@end


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    ClassA* cA = [[ClassA alloc] init];
    [cA printVal];
    [cA printVal];
    [cA printVal];

    [pool drain];
    return 0;
}

Why is this output:

2012-11-29 22:12:06.586 TestOne[20266:903] 0
2012-11-29 22:12:06.587 TestOne[20266:903] 32767
2012-11-29 22:12:06.588 TestOne[20266:903] 32767

In other words, why is val not reinitialized to 0 when it is redeclared, and why does it receive the value 32767 every subsequent time the method is called?

  • 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-06-15T09:42:52+00:00Added an answer on June 15, 2026 at 9:42 am

    That is garbage value.

    Initialise it with some value.

    EDIT:

    Storage class specifiers has some default value as

    for auto- garbage value.

    for static- 0.

    for global/extern- 0;

    for register-garbage.

    In your case it is auto.

    OHHH!!! I fotgot to answer your second part 🙁

    Why initially it came as 0 and later on some 32767.

    And you rightly pointed out in that link, now no need to explain in this answer. What can i do is refer to that link only.

    How does an uninitiliazed variable get a garbage value?

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

Sidebar

Related Questions

Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider following code: main.cpp: #include <iostream> typedef void ( * fncptr)(void); extern void externalfunc(void);
Consider the following code : class ThreadJoinTest{ public static void main(String[] arguments){ TimeUnit unit;
Consider following code: public sealed class Program { public static void Main() { System.Console.WriteLine(Hi);
Consider the following code snippet: class Test { public int Length{ get; set; }
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
Consider following code - namespace N1 { class A { //some implementation } class
Consider following piece of code void foo( bool forwad ) { vector<MyObject>::iterator it, end_it;
Consider following model situations: DLL interface is not changed but implementation does. Is it
Consider following code: My problem is: 1) I can't seem to cast the errors

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.