I can't figure out why a gives a value of 0, Can anyone explain? Thanks in advance.
The output for this program is 770

include<stdio.h>
include<stdlib.h>
int a,b; 
void mystery(){ 
while(b>0){ 
int a; 
a=a+10; 
b=b-1; 
} 
}

int main(){ 
a=7; b=a; 
printf("%d\n",a); 
mystery(); 
printf("%d\n",a); 
printf("%d\n",b);

system("pause") 
return 0; 
}

Delete line 6, you're hiding the global variable with a local variable. Any changes to the local variable are lost on each iteration of the loop.

commented: ok thankyou much, im supposed to know why 0 is output.The code isnot supposed 2 run +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.