Solved

Comparing to null

Posted on 2003-11-07
8
185 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

810 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