What is the output of the foll: code and pls give the reason
main()
{
int c=50;
for (;c;)
c-- ;
printf("%d\n",c );
}
The output of the program is undefined because you've called a variadic
function that doesn't have a prototype in scope. To fix this, #include
<stdio.hat the top of your program.
Once you've done that, the output is easily deduced provided that you know
how a for-loop works (and particularly at what point it stops). If you
don't know this, you need to look it up in your C book.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
What is the output of the foll: code and pls give the reason
Did you try to compile and see? What do you think the output (if any)
will be of both compilation and execution (if compilation succeeded)?
What possible problem do you see during compilation? What do you think
is needed to rectify this? After recompiling what is the output? Why?
Do you feel that you can slightly improve the code in some way?
main()
{
int c=50;
for (;c;)
c-- ;
printf("%d\n",c );
}
Whatever your compiler is make sure that you give it the appropriate
options to enable *all* warnings, and enforce *strict* conformance to
either C89/C90 or C99, whichever is supported or preferred by you. If
you are using the gcc compiler use one of the following two
commandlines as appropriate:
In article <8ff5fd20-e1b3-4226-9412-bafa9a761f58@c6 5g2000hsa.googl egroups.com>,
Swapna <SwapnaCnair@gm ail.comwrote:
>What is the output of the foll: code and pls give the reason
>main()
>{
int c=50;
for (;c;)
c-- ;
printf("%d\n",c );
>}
Your program uses the implicit int on main, which is permitted in C89
but not allowed in C99.
Your program fails to return a value from main, which is well defined
in C99 but has undefined results in C89.
Thus if you can get the program to compile at all, you are either
using C89 and invoking undefined behaviour or else you are using
a hybrid compiler about which we can make no predictions.
(Note: this posting is not intended to superceed any of the
fine answers that have gone before; you need to pay attention to
all of them -too-.)
--
"There is nothing so bad but it can masquerade as moral."
-- Walter Lippmann
What is the output of the foll: code and pls give the reason
main()
{
int c=50;
for (;c;)
c-- ;
printf("%d\n",c );
}
I suggest three steps.
1. make the thing run. I am reluctant to call it a program.
2 observe what happens.
3. try to explain what happens. Note that the end result of a for
statement can also be produced by a while statement, for is considered more
convenient, thus we have it
You will have better results if you post in the English language. I can't
imagine anyone in any nation thinking foll or foll: was a word. In this
case it probably wouldn't have helped, but keep it in mind for the future.
This is not a chat room and most keyboards do not depend on typing with your
thumbs.
On Jul 9, 9:20 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
Your program fails to return a value from main, which is well defined
in C99 but has undefined results in C89.
The return value is undefined. How is it going to affect the output of
the program. There are environments which don't capture the return
value. Of course it is a good practise and standard compliant but how
is it going to affect the output?
>Your program fails to return a value from main, which is well defined
>in C99 but has undefined results in C89.
>
The return value is undefined.
I think the Standard says the an "unspecifie d" value is returned to the
host environment.
How is it going to affect the output of
the program. There are environments which don't capture the return
value. Of course it is a good practise and standard compliant but how
is it going to affect the output?
Walter doesn't say (at least from what you have quoted above) that the
output of the program will be affected. Merely that under a C90
implementation, an unspecified termination status value is returned,
which is not portable. Whether that even causes undefined behaviour is,
IMO, not clear at all.
Of course code outside the program (like shell scripts, shell itself, or
the program that has invoked the offending code) may be broken by
random return status values, but I don't see how that can be taken to
mean undefined behaviour *within* the program, as whatever happens,
happens after the program has finished executing. But some interpreters
of the Standard regard the "implementation " as the entire system
(compiler, OS, other programs, hardware etc.). Under this view, it
could be asserted that returning an unspecified value to
the "environmen t" constitutes undefined behaviour, but I think it's
merely an academic objection at best.
>>Your program fails to return a value from main, which is well defined
>>in C99 but has undefined results in C89.
>>
>The return value is undefined.
>
I think the Standard says the an "unspecifie d" value is returned to the
host environment.
No, C90 uses the word "undefined" (though not in the phrase "undefined
behavior"):
If the main function executes a return that specifies no
value, the termination status returned to the host environment is
undefined.
I think that reaching the closing brace is equivalent to executing a
return with no value.
Whether that even causes undefined behaviour is,
IMO, not clear at all.
Agreed.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Comment