UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » string definition differences

#1 2007-11-01 04:15 PM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: string definition differences

what are  the differences between the

char str[]="hello"

and

char str[5]="hello"

?
and anther question is

int global_num;
          int count (int a)
          {static int local_num;
           }


in the above code,when we first execut it,what are the value of the global_num and the local_num?

Offline

#2 2007-11-01 05:31 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: string definition differences

A "hello" string is 6 chars big, including the terminating zero.
But any somewhat sane compiler will allocate 6 bytes for str,
even if you ask it to be 5 chars big (or 8 bytes even, because
of alignment). But still, the second case is wrong.

Global and static variables are always initialized to zero.

Offline

  • Index
  • » C
  • » string definition differences

Board footer

Powered by FluxBB