Re: Why do i get runtime error ?
- From: Bruce Cook <bruce-usenet@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 08 Feb 2009 00:44:12 GMT
Jens Thoms Toerring wrote:
karthikbalaguru <karthikbalaguru79@xxxxxxxxx> wrote:
On Feb 6, 8:37 pm, j...@xxxxxxxxxxx (Jens Thoms Toerring) wrote:
karthikbalaguru <karthikbalagur...@xxxxxxxxx> wrote:
Is it not possible to use %f and print #define's ?
I guess you forgot to mention that you have
#include <stdio.h>
here, didn't you?
#define MAX_VAL 100
int main(void)
{
printf("%f", MAX_VAL);
return 0;
}
I get runtime error for the above code .
Any specific reason ?
Yes, it's because you're not honest with printf(). You tell it
via '%f' that it can expect a double value but what you give it
insteadis an integer. Now printf() tries to interpret the
data it receives as a double but the data may simply be nor
a valid double and that may result in a runtime error.
Thinking over this, i got a strange query.
But, Why doesn't handle these execptions and convey something like
' %d should be used '
or
'#define's need %d while passed to printf'
etc...
Actually, some compilers warn you for e.g.
prinf( "%f", 100 );
if you raise the warning level high enough. E.g. gcc, when started
with '-W', tells you about that line
warning: format '%f' expects type 'double', but argument 2 has type 'int'
But it can only do that if it gets a format string that is known
at compile time. On the other hand, there are situations where
format strings are only assembled at run time and then the com-
piler can't do anything about it. To catch also those cases
checks to be done at run time would have to be inserted before
each call of printf() (and similar functions). I guess there's
nothing in the C standard that would forbid this but, probably
since it might incur a rather high overhead, I haven't seen it
done (which doesn't mean it doesn't exist!). Traditionally, in
C very few run time checks are done automatically since all of
them tend to reduce execution speed, so the programmer has to
be on the look-out to avoid this and other errors.
One of the base philosophys behind the compiler it that it will just build
code that does the job, you the programmer are responsible for , for
example, making sure you have the correct arguments. For the most part
there is no runtime checking such as other languages do (bounds etc).
What's actually happening with that printf(), is that you are simply calling
a library function with a set of arguments. The first argument of printf()
is a string pointer that the function uses to control the formatting of it's
output, specifying string literals, and the format/data type of the
remaining arguments.
printf() is not special, although a compiler could be educated to know about
printf() and friends and do some additional checking. In other languages
I/O operations are handled by language constructs, so therefore compile and
run time checking can be applied. in C, it's just another library function.
As Jens points out, parameters may be passed to printf() in a variety of way
that would make their data type obvious at runtime. This would mean a great
deal of overhead doing runtime checking. C as a language generally doesn't
hold your hand, it lets you make the most egregious mistakes and happily
assumes that you know what you're doing. While later compilers will warn
you about a raft of common mistakes, some things like the content of strings
C is really not interested in.
Bruce
.
- References:
- Why do i get runtime error ?
- From: karthikbalaguru
- Re: Why do i get runtime error ?
- From: Jens Thoms Toerring
- Re: Why do i get runtime error ?
- From: karthikbalaguru
- Re: Why do i get runtime error ?
- From: Jens Thoms Toerring
- Why do i get runtime error ?
- Prev by Date: Re: passing char arrays by reference
- Next by Date: Re: Need clarity on structure alignment
- Previous by thread: Re: Why do i get runtime error ?
- Next by thread: Re: Why do i get runtime error ?
- Index(es):
Relevant Pages
|