top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: array in global and inside funtion

+2 votes
380 views
#include <stdio.h>
char buff[10];

main()
{
   char buff1[10];
   int i = 10;
   while(i-- > 0 ) 
   {
      printf("%d %d\n", buff[i], buff1[i] );   
   }       
}

Here, buff will display all zeros where as buff1 through garbage values. How ?

posted Mar 9, 2014 by sivanraj

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

Global variables are initialized to zero while local are not so you see buff is all zero and buff1 is all garbage.

answer Mar 9, 2014 by Salil Agrawal
how about the storage for buff1 ? is it in stack segment ?
Yes that is correct, local variables(non static) would be stored in the stack of the function.
Similar Questions
0 votes

I have a recursive function as below,

void func()
{
       if(counter>10)
              return;
       func();
}

Here, I need to come out of the function when counter reaches specific number(10 as per example).
Now, the condition is, I can't take this counter as global or static or can't pass this counter as parameter.

Any suggestion to solve this given above terms.

+5 votes

Can we realloc a string inside a structure?

struct info
{
     char name[20];
     int age;
     char address[20];
}; 

struct info d[10];

I want to realloc name, how can I do it?

...