Why passing string to scanf() compiles fine in C? -
i wrote simple program mistake use scanf() instead of printf() displaying message on console. expecting compile time error, compiles fine without warnings & crashes @ runtime. know scanf() used taking input keyboard. shouldn't error in following program?
#include <stdio.h> int main() { scanf("hello world"); // oops, had printf() return 0; }
is invokes undefined behavior(ub)? there mention in c standard? why isn't checked @ compile time whether proper & valid arguments passed scanf() function or not?
the code behaving correctly. indeed, scanf
declared
int scanf(const char *format, ...);
luckily, format
not contain %
there no correspondence in ...
, invoke ub.
further, format
string literal allows compiler go through ensuring passed right type of parameters in regards format specifiers, part of sanity checks enabled higher warning levels. (-wformat family)
Comments
Post a Comment