0

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.

{
    int max=0,x[5];
    for(int a=0;a<5;a++)
    {
        cout<<"Enter no "<<a+1<<" : ";
        cin>>x[a];
        if(max<x[a])
    {
        max=x[a];
    }

    }
    cout<<endl<<max;
}

output:

Enter no 1 : 1
Enter no 2 : 5
Enter no 3 : 8
Enter no 4 : 7
Enter no 5 : 5

8

and when type max=0 means at the declaration assign 0 to max

it gives me the same result that's mean null value is equal to 0. if yes then what is difference between null value and 0

Lee
  • 142,018
  • 20
  • 234
  • 287
  • *"it gives me the same result"* *what* gives you the same result? – eerorika Nov 17 '18 at 17:24
  • Your "programmer" is mixing concepts in a misleading manner. There is no such thing in C++ as a "null value" that arises due to not initialising a variable. If an `int` is uninitialised then evaluating its value gives undefined behaviour - which (loosely) means the C++ standard doesn't define what happens. Any consequence you can imagine of evaluating it is notionally possible (junk value, zero, reformatting a hard drive, different value if code is built with a different compiler, value varying with phase of moon, etc etc). – Peter Nov 17 '18 at 20:23

3 Answers3

0

It actually depends on what programming language are you writing your code in. For instance, if you access in read an uninitialized variable in c/c++ you cannot know which value it will give you. Memory is not zero-ed when the variable is allocated, so whatever bits were there, they will be read as a value in the type of that variable.

Game Maker's own language, GML, allows you to decide at compile time if uninitialized variables have to be set to 0 by default or not; but any decent programmer would set that option off and not rely on it; i'm not aware of other languages that automatically initialize variable values.

I'd say relying on such a feature is bad practice.


About the difference between null and 0, it actually depends on the base type of your variable. An integer can only store integers, so null will still be readable as an integer.

Barnack
  • 921
  • 1
  • 8
  • 20
0

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • You can even get nasal demons when you have undefined behavior. http://www.catb.org/jargon/html/N/nasal-demons.html – rsjaffe Nov 17 '18 at 17:45
0

There's no 'null' value in c++. Local primitives are uninitialized (you'll get garbage that just happens to be in memory at this time), while static and global variables are zero-initialized. So by saying int max; You'll either get random garbage or zero (depending on where you declared it).

vrtex
  • 165
  • 9
  • that zero would happen to be just another random garbage possiblity – Barnack Nov 17 '18 at 17:37
  • @barnack - "that zero" is referring default initialisation being zero-initialisation in some contexts e.g. an `int` of static storage duration is initialised to zero, by default. `int max` outside a function initialises to zero. That is not "another random garbage possibility". – Peter Nov 17 '18 at 20:08