Solved

Comparing to null

Posted on 2003-11-07
8
184 Views
Last Modified: 2010-04-15
how do i say

x != null in C?

and what is the equivilant of the correct statement in C#?

thanks
0
Comment
Question by:mwmiller78
8 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 9702823

C is quite versatile here:


if (x != NULL)

is the same as saying

if (x)


Kent
0
 

Author Comment

by:mwmiller78
ID: 9702852
what if x = 0?
0
 

Expert Comment

by:rishisk
ID: 9703022
If your question is
"How will I check if x's value is zero?"
then adding an else construct to what Kdo had written would handle it.

If your question is
"What if i just say   if ( x = 0) ?"
Then 0 will be assigned to the variable x and the expression would be evaluated as the value of x, which will become zero in this case and the statements falling under if will not get executed.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9703177
Here's is the explanation

As Kent said

if ( x != NULL )  {
  // some code
}

is same as saying

if ( x ) {
  // some code
}


Now it depends on your requirement. If u want that only if x has some non-zero value should some code be executed, then
the above 'if' would handle both the conditions [for NULL as well for 0] i.e. the above 'if' would execute the //some code part only if x has some value other than 0 [non-zero] and it is not NULL

If u want to handle a separate case for 0, then u would have to use the comparison operator ==
U can say

if ( x == 0 ){
// some code
}

this would check if x is 0, if yes, then it would execute some code part

U can obviously combine the above two conditions

HTH
Amit
0
 
LVL 45

Accepted Solution

by:
Kdo earned 75 total points
ID: 9703702

For all intents and purposes, NULL is zero.  (There may be size differences or casting differences but NULL is zero.)

So adding one more "is the same as"

if ( x != NULL )  {
  // some code
}

is the same as

if (x != 0) {
  // some code
}

is same as

if ( x ) {
  // some code
}



You needn't worry about this, but on some compilers the last example is the most efficient.  The compiler will simply test whether the value of x is zero.

The other examples all produce LOGICAL expressions.  The test in "if (x == NULL)" is a logical test.  A logical test always produced a 1 or 0 answer so the compiler will first evaluate "x == NULL" as a logical expression and convert it to 1 or 0.  The if () will then test whether the value is zero.  This process requires an extra step and a couple more instructions.

Of course, if your compile options include code optimization, then the compiler may reduce all of the tests above to a single instruction.


Kent
 
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9707641
> what if x = 0?

NULL is usually defined as (void *)0
0

Featured Post

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Retrun object in plist format 5 54
memory leak detection 9 69
An API detour question 7 79
smtp c source code 7 42
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now