Solved

Comparing to null

Posted on 2003-11-07
8
186 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:Mike Miller
8 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9702823

C is quite versatile here:


if (x != NULL)

is the same as saying

if (x)


Kent
0
 

Author Comment

by:Mike Miller
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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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:
Kent Olsen 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

830 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