Re: Why do i get runtime error ?



Bart <bc@xxxxxxxxxx> writes:
On Feb 6, 3:15 pm, karthikbalaguru <karthikbalagur...@xxxxxxxxx>
wrote:
Is it not possible to use %f and print #define's ?

#define MAX_VAL 100
int main(void)
{
     printf("%f", MAX_VAL);
     return 0;

}

I get runtime error for the above code .
Any specific reason ?

The error has been explained but this can easily be picked up by your
compiler. Adjust the warning levels for it.

That depends on the compiler. gcc, for example, can diagnose
mismatches between a printf format string and arguments (if the format
is given as a string literal), but I don't know whether other
compilers can do so.

The bottom line is that it's entirely up to you, the programmer, to
make sure that your format string is correct for the argument types
you're feeding it. Some compilers might be able to help you diagnose
the problem, but you can't count on it. The only reliable solution is
to get it right in the first place.

--
Keith Thompson (The_Other_Keith) kst@xxxxxxx <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"
.



Relevant Pages

  • Re: ApostaC - Variadic functions RFCAS
    ... >> Okay, but if the format string isn't evaluated until runtime, then ... because the compiler cannot know at compile time ... ApostaC doesn't do pointers this way, but I just wanted to get the idea ... thinking about how you can implement 'printf' and 'fprintf' without ...
    (comp.programming)
  • Re: writing about 100 colomns of real data into text file
    ... > may also have a limit on the allowable size of maxrec. ... Apparently, my compiler ... >> but apparently things fail when I use that format string in the first write ... and the other nb_runs+4 columns are reals of a certain ...
    (comp.lang.fortran)
  • Re: Fortran error reading text file made by Matlab
    ... A given compiler _might_ (depending on compiler extension but not according to Standard F95 under list-directed format ("*" FORMAT string) but not with a fixed width field. ... A tab is only a single character embedded in the record; the I5,12F8.3 of OP's original Fortran READ posting needs a record consisting of precisely 5+96 characters plus the record terminator. ... The tab-delimited record written by Matlab will have only the specific characters written per the field specifications used which were too narrow and included a non-existent character in the allowable character set for the purpose (a TAB is allowed in writing external records or in internal CHARACTER variables, but not as part of a fixed format record). ...
    (comp.soft-sys.matlab)
  • Re: narrowing conversions with arrays
    ... Causes the runtime error. ... So the compiler doesn't raise an error because bases can contain an instance ... An unhandled exception of type 'System.ArrayTypeMismatchException' occurred ... Inheritedinheriteds; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: segmentation fault on calloc
    ... > Failure to #include something is only going to confuse the compiler. ... returns `int', while in fact it returns `void *'. ... which may well cause a runtime error. ...
    (comp.lang.c)