Is the value i defined after: int i, j = 1;

Given:

int i, j = 1;

Is a value defined i? If so, what is it?

I suspect this is a duplicate, but hard to find - if anyone can find it, let me know.

+3
source share
8 answers
  • Global variables are initialized to default values ​​(0 for int)
  • Local variables are not initialized by default

For instance:

#include <iostream>

int a, b=1; // a=0, b=1

int main(void) {
 int p, q=1; // p=undef, q=1
 return 0;
}

for local variables:

#include <iostream>
int main(void) {
  {
    int x = 99; // change stack where a would be
  }
  int a, b=0;
  std::cout << a << std::endl;
  return 0;
}
+14
source

If this code is in the global scope, then it iwill be 0. Otherwise, it is inot defined.

+5
source

, i .

+2

, , , , .

+2

:

int i;
int j = 1;

, i , .

+1

i undefined. undefined, i, -.

. undefined int , undefined.

, , , , BIOS .

The moral of the story: if it is not initialized, do not use it.

+1
source

int i - This is a definition statement, but it is not assigned a value.

Extern int i is an announcement.

0
source

Variable i can contain from 0 to garbage. refer to the following link

http://www.intap.net/~drw/cpp/cpp03_02.htm for more information

0
source

Source: https://habr.com/ru/post/1780430/


All Articles