CPP local variables must be initialized?

Local variables in

CPP must be initialized first, but the following code outputs 0. I don"t know why?

-sharpinclude<iostream>
using namespace std;

int main(){
    int a;
    cout << a << endl;
    return 0;
}
CPP
Jan.19,2022

does not have to be initialized, but before initialization, it is the original value in memory. Output 0 just because the space of that a happens to be 0, and then run


uninitialized standard is not defined, each compiler processing strategy is different, even different versions of the same compiler strategy may be different. Some compilers vary from level of optimization to level of optimization. You can just look at it as a random value, but it just happens to be zero.


The

CPP local variable must be initialized first, otherwise its value on memory is unknown. If you have previously used this piece of memory and have not eliminated the value on this piece of memory, the last value will be read at this time.
in addition, different compilers have different treatments for uninitialized local variables, such as

above
-sharpinclude<iostream>
using namespace std;

int main(){
    int a;
    cout << a << endl;
    return 0;
}

outputs 0 before vs2017 and an error after vs2017-C4700 uses the uninitialized local variable'a'. Even if some compilers assign uninitialized local variables to 0, we still have to maintain the good habit of initializing all local variables to avoid some unexpected errors.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a8e3-2c24f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a8e3-2c24f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?