[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Comparing to null

how do i say

x != null in C?

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

thanks
0
Mike Miller
Asked:
Mike Miller
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

C is quite versatile here:


if (x != NULL)

is the same as saying

if (x)


Kent
0
 
Mike MillerSoftware EngineerAuthor Commented:
what if x = 0?
0
 
rishiskCommented:
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.

 
Sys_ProgCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
brettmjohnsonCommented:
> 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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now