Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Comparing to null

Posted on 2003-11-07
8
Medium Priority
?
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 46

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 46

Accepted Solution

by:
Kent Olsen earned 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

609 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