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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -