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 6704503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:17:46+00:00 2026-05-26T07:17:46+00:00

This is a very small program which I wrote last night: void testWithFunction(int val)

  • 0

This is a very small program which I wrote last night:

void testWithFunction(int val) {
  int someA;
  printf ("SomeA: %x %d \n ", &someA, someA);
  someA = val;    
}

int main() {
 int j = 5;
 while ( j-- ) {
    int i;
    if ( j == 4) { i = 10; }
    printf("i: %x %d \n", &i, i);   

 }

  testWithFunction(5);
  testWithFunction(10);
  testWithFunction(0);
  // system("pause");
  return 0;  
}

The output to the above was:

i: 28ff20 10
i: 28ff20 10
i: 28ff20 10
i: 28ff20 10
i: 28ff20 10
SomeA: fffffffe 2686916
SomeA: 5 2686916
SomeA: a 2686916

I used to think that variables declared inside loops had local scope – they were destroyed when the loop terminate. For each new iteration, all its local variables would be created again. However with this example, it seems that the same variable is reused each time and so, the variable can even remember its last written value. The same applies for functions – Variables local to a function should be destroyed when the function terminates and should be re-created when the function is called again! However, the results suggest that the variables behave as if they are declared with the static keyword! This is something new to me. Please correct me – Is the static keyword implicit for all variables??

I have tried the same thing with structure type nodes and found the same result. So I dont think we should assume that the variables are re-allocated to the same memory space and hence they retain their value. ( I agree “destroy” doesnt actually deletes the old value)

Example 2:

#include <stdio.h>  
typedef struct node    
{  
    int val;  
    struct node *next;  
}node;  

int main()  
{  
 int i;  
 node newtemp,root,temp;  

 scanf("%d",&i);  
 while(i--)  
 {  
     node array[i];  
     node n;  

     int j;  
     for(j = 0; j < i; j++) {   
            printf (" array: %x  ", &array[j] );                  
       }  


     n.val = i;  
     n.next = NULL;  
     printf("node: %x\n", &n);  

 }  
return 0;  
}  

Output:
4
array: 28feb0 array: 28feb8 array: 28fec0 node: 28ff00
array: 28fec0 array: 28fec8 node: 28ff00
array: 28fec0 node: 28ff00
node: 28ff00

  • 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-26T07:17:47+00:00Added an answer on May 26, 2026 at 7:17 am

    It’s more along the lines of your code is basically just doing the same thing over again. Each time testWithFunction() is called, it allocates some space on the stack for the someA int. By chance, you always get the SAME chunk of stack space, so it appears that your variable is static. but it’s just a random chance side effect.

    If you’d called another function or did ANYTHING that affects the stack, you’d see the values randomly changing.

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

Sidebar

Related Questions

So I am obviously not a very good programmer. I have written this small
This very simple code gives me tons of errors: #include <iostream> #include <string> int
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Sorry if this is a stupid noob question. I'm doing a very small project
I'm writing a program which will need to do a very large number of
I currently have a small Java program which I would like to run both
I'm trying to learn Haskell and want to write a small program which prints
I am very newbie to Perl. I wrote a very simple Perl program (script):
I've been working on a very small program to grab details about Half Life

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.