Use of 100;

K

karthikbalaguru

Hi,

I came across a simple program that is written as below.
I wonder, what is the use of the statement '100;' (line number 4) ?

#include<stdio.h>
int main(void)
{
100;
printf("%d\n",50);
}

The compiler does not warn or throw any error.
It gives the correct output of 50.
I use Visual C++ 2008 Express Edition.

Where are such statements useful and does the
standard support it ? What value do those statements
add to in a program ?

Thx in advans,
Karthik Balaguru
 
K

Keith Thompson

karthikbalaguru said:
I came across a simple program that is written as below.
I wonder, what is the use of the statement '100;' (line number 4) ?

#include<stdio.h>
int main(void)
{
100;
printf("%d\n",50);
}

That particular statement is of no use.
The compiler does not warn or throw any error.
It gives the correct output of 50.
I use Visual C++ 2008 Express Edition.

Where are such statements useful and does the
standard support it ? What value do those statements
add to in a program ?

An expression statement consists of an expression (any arbitrary
expression) followed by a semicolon. The expression is evaluated and
the result is discarded. Normally such expressions are evaluated
for their side effects. In this case, it has no side effects,
but the language doesn't prevent you from doing useless things.
(Some compilers might warn you about it.)

Note that the following line is also an expression statement.
The expression is a call to the printf function, which returns
an int result (the number of characters printed). Since it's an
expression statement, that result is silently discarded. In that
case, it's the side effect (printing "50\n") that's important.
 
S

Seebs

Hi,

I came across a simple program that is written as below.
I wonder, what is the use of the statement '100;' (line number 4) ?

It doesn't have any.
The compiler does not warn or throw any error.

Well, there isn't an error, but many compilers would give a warning for this.
Where are such statements useful and does the
standard support it ? What value do those statements
add to in a program ?

It's more a question of "why bother to prohibit them?"

-s
 
K

karthikbalaguru

That particular statement is of no use.



An expression statement consists of an expression (any arbitrary
expression) followed by a semicolon.  The expression is evaluated and
the result is discarded.  Normally such expressions are evaluated
for their side effects.  In this case, it has no side effects,
but the language doesn't prevent you from doing useless things.
(Some compilers might warn you about it.)

Note that the following line is also an expression statement.
The expression is a call to the printf function, which returns
an int result (the number of characters printed).  Since it's an
expression statement, that result is silently discarded.  In that
case, it's the side effect (printing "50\n") that's important.

Interesting !
Thx for the very clear info.

Karthik Balaguru
 

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

Similar Threads

Strange printfs !! 11
Strange !! 10
It works without stdio.h !! 22
Why do i get runtime error ? 19
"%d"+1 and printf !! 23
typedef is a storage class specifier ? 3
Array of structs function pointer 10
Switch for floating !! 70

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top