Link to home
Start Free TrialLog in
Avatar of rongz
rongz

asked on

Declare a variable without initialization

Hello,
after declaration of char str[1024]; (without intialization or assignment), what value the str points to? Does char str[1024]; equal to char str[1024]="";? Thanks.
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rongz
rongz

ASKER

However, running the following code, I always get "hit" result. Why?

------------------------------------
#include <string.h>
#include <stdio.h>
void main(int argc, char **argv){
  char max[124];

  if (strcmp(max, "")==0)
    printf("hit\n");
  else
    printf("not hit\n");

}
Memory has a lot of zeros in it. You got lucky.
This typically works too, but you shouldn't depend on it:
--------8<--------
#include <stdio.h>

void foo();
void foo2();

int main()
{
      foo();
      foo2();
}

void foo()
{
char buf[256];

      strcpy(buf,"Hello, World\n");
}

void foo2()
{
char buf[256];

      printf(buf);
}
--------8<--------