What does the C compiler do with different types of declarations?

I understand it:

int i = 3;  // declaration with definition

It tells the compiler:

  • reserve memory space for storing an integer value.
  • Associate a name with a memory location.
  • Save the value 3in this place.

But what does this declaration say to the compiler:

int i;  // declaration
+5
source share
6 answers

The announcement tells the compiler to reserve a place for the variable iand map the name ito this space (your points 1 and 2).

If iis a global variable, it is initialized 0.

, i undefined (, , - ), .

+4

: (.. ) .

int i; : i, int, , int. , , . , i, , . , .. , . , .

, int i i. : . . , . i , , 0. :

int i;
/* ... more code ...*/
int i;

i 0 (, i ). :

int i;
int i = 3;

, i 3 .

, 0. 0 , , , ( ) .

+3

, , i. undefined.

+1

, :

  • ( )
  • .
0
, , ( ) :
int i;

i int, ( i ). , , "":

, undefined. , . . , : undefined.

, int i; , , : "i - int, . , ". , C .

0

:

1) ( "", ). ; 2) ( ); 3) (, malloced).

"int i;" , . , ( "" .

Objects created in external memory are initialized to zero if they are not explicitly initialized (for example, "int i = 3";

You can create an external object in a function using the "static" keyword.

int a; // external memory with "global" scope. Initialized to 0 implicitly.
static int b; // external memory with file (module) scope. Initialized to 0 implicitly.
int c = 3; // external memory initialized to 3.

f()
{
  int d; // created on the stack. Goes away when the block exits. Filled with random trash because there is no initialization.
  int e = 4; // stack object initialized to 3.
  static int f; // "f" is external but not global. Like all externals, it implicitly initialized to zero.
  static int g = 3; // An external like f but initialized to 3.

}
0
source

All Articles