If you default initialize a fundamental-type object (such as int
) in automatic storage, the value will be indeterminate. The behaviour of reading an indeterminate value is undefined.
Undefined behaviour means that the standard doensn't constrain the behaviour of the program in any way. As far as the C++ standard is concerned, possible behaviours include, none which are guaranteed:
- Output that you expect.
- Output that you don't expect.
- Same output as some program which doesn't have UB.
- Different output than some program which doesn't have UB.
- Any output.
- No output whatsoever.
- Side-effects that you expect.
- Side-effects that you don't expect.
- Any Side-effects.
- No Side-effects whatsoever.
- Possible side-effects include:
- Corruption of data.
- Security vulnerabilities.
- Anything within the capability of the process, hopefully limited by the OS.
- Inconsistent behaviour on other systems.
- Inconsistent behaviour even on same system if you re-compile using another compiler.
- Inconsistent behaviour even if you re-compile using same compiler.
- Inconsistent behaviour even without recompilation during another execution:
- Possibly only when you're on vacation.
- Possibly only when you're demonstrating your program to your employer or important client.
- Consistent behaviour in all of the above cases.
A programmer told me that if we don't assign a value to any variable e.g like max then it consider as null value.
There is no such thing as null value integer in C++. There is such thing as a null pointer, as well as null character, neither of which are directly related to each other, and neither have anything to do with an uninitialized variable.
Your programmer is using confusing terminology, but if by null value, they meant indeterminate value, then they are correct. And in that case your question becomes what is difference between indeterminate value and 0, and the answer is that reading an indeterminate value is undefined behaviour (see above).
when type max=0 means at the declaration assign 0 to max
To be pedantic, you have int max=0
which is not an assignment, but initialization.