Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with using static in C code

Status
Not open for further replies.

sacrpio

Member level 3
Joined
May 24, 2004
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
500
STATTIC IN C

Hi friends,
Here a problem in C?

void mani()
{
static int a=5;
if(--a>0)
main();
printf("%d", a);
}

After execution this code is printing 0 0 0 0 0. Why? Could anybody explain.
What I am understanding is it should print o only.

Thanks
 

Re: STATTIC IN C

First, I see several C mistakes. Maybe this is what you want:

Code:
#include <stdio.h>

int main(void)
{
  static int a=5;
  if (--a > 0)
    main();
  printf("%d", a);
  return 0;
}

That function calls itself reentrantly four times while decrementing 'a'. It prints "0" just before it returns each time, so the final output is "00000". Why do you think it should print only "o"?
 

Re: STATTIC IN C

I've done some modifications which makes it clearer what happens. Main is called 5 times without printing anything until a = 0. Imagine it as a chain. Then the last main is finished by printing a = 0. This happens until all called functions are returned (5 times).

Nice example for a recursive function :roll:


Mik

Code:
void main()
{
static int a=5;
static int b=0;

printf("entering main %d\n", ++b);
if(--a > 0)
   main();
printf("%d\n", a);
printf("leaving main %d\n", b--);
}
 

Re: STATTIC IN C

Like auto variable, static variables are also local to the block in which they are declared. The difference between them is that static variables don't disappear when the function is no longer active. If the control comes back to the same function again the static variables have the same values they had last time around.

HERE UR CALLING THE SAME FUNCTION MAIN FOR 5 TIMES UNTILL THE VARIABLE 'A' IS 0; AT LAST THE VALUE OF 'A' IS 0. THEN IT IS SIMILAR TO FACTORIAL CALCULATION WHERE WHEN U CALL FACT IT WILL MUL USING LAST IN FIRST OUT PRINCIPLE IS USED.
HERE WE ARE CALLING THE FUNCTION FIVE TIMES THUS THE OUTPUT CONTAINS FIVE ZEROS.
 

STATTIC IN C

Hi,
void mani()
{
static int a=5;
if(--a>0)
main();
else
printf("%d", a);
}

now it will print only 0, u have 0 0 0 0 0 printed due to returns u had from all the calls to main.
 

Re: STATTIC IN C

Code:
void mani() 
{ 
static int a=5; 
if(--a>0) 
main(); 
printf("%d", a); 
}

my freind you called the main function 5 different time inside it selves;
watch this

1. a os static so it value will be static if you call the fucntiojn again;
1st time you run the program a=4 then call main to the 2nd time
2. in the 2nd main a=3 then call main to the 3rd time
3. in 3rd main a=2 then call main to the 4th time
4. in 4th main a=1 then call main to the 5th time
5. in the 5th main a=0 then program will resume and will not call main again
then print value of a which now is 0
6. return to 4th main to print value of a which is 0 too
7. return to 3rd main to print value of a which is 0 too
8. return to 2nd main to print value of a which is 0 too
9. finally retun to 1st main to print value of a which is 0 too

so you got "0 0 0 0 0"

i hope that helps you
thanks


shafee001
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top