Significance of 0;

P

Pruthvi

What is the signifecance of using 0; or 1; or 100; in C code?
I think it is a NULL statement, won't do anything.
Pls let me no any better reasons for using this in the code.
 
J

Joona I Palaste

Pruthvi said:
What is the signifecance of using 0; or 1; or 100; in C code?
I think it is a NULL statement, won't do anything.
Pls let me no any better reasons for using this in the code.

It calculates the value of 0 or 1 or 100 and then discards it, without
doing anything else. It is effectively a null statement.
 
M

Martin Dickopp

Pruthvi said:
What is the signifecance of using 0; or 1; or 100; in C code?
I think it is a NULL statement, won't do anything.

Technically, it is not a null statement. A null statement is defined by
the C standard (section 6.8.3#3) as just a single semicolon.

`0;' or `1;' or `100;' is an expression statement. When it is evaluated,
the side effects are performed (which are none in this case), and the
result is discarded. So the effect is the same as that of a null
statement.
Pls let me no any better reasons for using this in the code.

Occasionally, macros might expand to such a statement under some
condition. I see no reason for using it explicitly, though. Not
everything that is allowed is also useful. :)

Martin
 
R

Robert Stankowic

Martin Dickopp said:
Pruthvi said:
What is the signifecance of using 0; or 1; or 100; in C code?
I think it is a NULL statement, won't do anything.
[....]

Pls let me no any better reasons for using this in the code.

Occasionally, macros might expand to such a statement under some
condition. I see no reason for using it explicitly, though. Not
everything that is allowed is also useful. :)

I use it sometimes for debugging:

if(<some condition which occurs very seldom>)
{
1; /*can set a breakpoint here*/
}

Robert
 
F

Fred L. Kleinschmidt

Robert said:
Martin Dickopp said:
Pruthvi said:
What is the signifecance of using 0; or 1; or 100; in C code?
I think it is a NULL statement, won't do anything.
[....]

Pls let me no any better reasons for using this in the code.

Occasionally, macros might expand to such a statement under some
condition. I see no reason for using it explicitly, though. Not
everything that is allowed is also useful. :)

I use it sometimes for debugging:

if(<some condition which occurs very seldom>)
{
1; /*can set a breakpoint here*/
}

Robert

Sometimes, such a statement is used to comply with coding standards. For
example, consider a coding standard that requires an "else" statement
for every "if", in order to explicitly demonstrate to future maintainers
that the "else" condition was considered and that there was nothing to
be done, rather than having been inadvertently forgotten about or
deleted:
#define NOOP 0 /* Can also just be nothing */
if ( a ) {
...
}
else {
NOOP;
}
or
while ( ... ) {NOOP} /* Body-less loop - shows I didn't
inadvertently delete something */
 
L

LibraryUser

Fred L. Kleinschmidt said:
.... snip ...

Sometimes, such a statement is used to comply with coding
standards. For example, consider a coding standard that requires
an "else" statement for every "if", in order to explicitly
demonstrate to future maintainers that the "else" condition was
considered and that there was nothing to be done, rather than
having been inadvertently forgotten about or deleted:
#define NOOP 0 /* Can also just be nothing */
if ( a ) {
...
}
else {
NOOP;
}

Generally used only at the tail of a chain of else-ifs. I
usually simply make it a comment, as in:

if (a) {
...
}
else if (b) {
...
}
/* else nothingneeded; */

No zeroes or extraneous code needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top