Which is more efficient
if(!var_name)
or
if(var_name == NULL)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Both will compile to the same code. Your choice of which to use should depend on which is most readable.
This version:
should only be used when
var_nameis a pointer, otherwise you will confuse anyone who reads your code in the future. Some compilers might complain if you use this on a non-pointer.This one:
should be used in cases when you are logically treating
var_nameas a boolean (true/false) value. This can include whenvar_nameis a pointer, sinceNULLis the value for “false” or undefined pointers.If
var_nameis an integer, then I would choose a third option:as I find it expresses intent more clearly.