Are NULL and zero that same thing in C++? | SoloLearn: Learn to code for FREE!

+26

Are NULL and zero that same thing in C++?

Is there a comparison operator like '===' in PHP?

4/27/2017 12:19:42 AM

Shaun

39 Answers

New Answer

+32

anything equal to 0 maybe initialised as 0. meaning, its only applicable to datatypes supporting numerics. NULL on the other hand may mean its empty. like a string, char or any other relative data types

+22

Nope, we don't have a ===.

+19

What is the difference between NULL and zero: https://www.sololearn.com/discuss/348465/?ref=app

+15

@kirkschafer Thank you great answer was struggling with null v. zero, no more

+11

Null basically have NO VALUE. & 0 is a numeric value itself. Null value is empty, undefined or not even initialized. When a variable is null then variable assign no any value, but if variable is 0 then it hold a integer in memory. ☺️

+8

Additional info to "no exact equivalence (===)". I'll just hang back to link pages with digestibly-short explanations + code demo. They demonstrate what the compiler can/can't determine about NULL (the macro result) under different compiler standards, as compared to the actual integer literal 0 and the "null_ptr" type. First, the NULL macro (and its values, depending on C++ version) http://en.cppreference.com/w/cpp/types/NULL Demonstrates literal, integer, nullptr_t, and (if you uncomment the NULL line) that the compiler can't figure out what NULL should mean: http://en.cppreference.com/w/cpp/types/nullptr_t Just for completion: http://en.cppreference.com/w/cpp/language/nullptr

+8

Null doesn't occupy the memory space, Zero occupy the 1bit space of memory.

+6

null means that there is no value found. and 0 means zero but zero is still a value which is zero.

+6

From the Google style guide at: https://google.github.io/styleguide/cppguide.html#0_and_nullptr/NULL "For pointers (address values), there is a choice between 0, NULL, and nullptr. For projects that allow C++11 features, use nullptr. For C++03 projects, we prefer NULL because it looks like a pointer. In fact, some C++ compilers provide special definitions of NULL which enable them to give useful warnings, particularly in situations where sizeof(NULL) is not equal to sizeof(0)." So, it depends on the language definition in use. Anyway, good hint by @Kirk Schafer!

+6

NULL means that it can have any value. At the digital world there is no such thing as zero or a decimal 0. There is a difference in a binary 0 and NULL it self. NULL can be empty or contain a value but it is not necessary.

+5

we have == comparison operator in c++

+4

In Programming, 0 and null have their on significance. 0 treated as number data type value and also treated as false using in a condition. where as null used as special meaning that define a variable hold no value. Most of time, null assigned to object that have no value

+4

Null is absence of a value, but any supporting numerics datatype can have the value 0 as a starting point.

+4

Take a look at this code https://code.sololearn.com/can5PEJjw9D8/?ref=app NULL is just a preprocessor constant which was defined like this : #define NULL 0 i wouldn't be surprised if NULL exists since the beginning of the c language. Then in theory 0 and NULL are exactly the same thing, you can compare and use NULL ptr as if it were an integer. But in practice lot of programmers use the NULL constant only for pointer, so a lot of compiler will think you will do the same and will send you warnings if you use NULL constant as an integer. Now c++ has developped an other, and really null pointer : the nullptr which can really be considered as a pointer, you can compare it to other value, but you can't print it or doing calculations with it (nullptr *5 is incorrect for example). So i suggest you to use nullptr instead of NULL (but keep using 0 for integers^^)

+3

NULL does equal 0 so that's a thing to watch out for with int* and user-defined == operators where 0 on one side might not actually mean a blank / non existing object (null pointer).

+3

Zero is a value of variable, NULL is a macros.

+3

I know it's confusing but null is used to describe the absence of a value i.e nothing. Although zero has no value...it is still an integer.I hope this gave you a better understanding.

+3

Null doesn't occupy the memory space, but Zero (0) occupy the 1bit space of memory.

+2

No. null has no value and 0 is a value of 0 whether int, string,float, or double

+2

Zero is a value but null is not ..