c - Why printf("test"); does not give any error? -
if int x=printf("test");
executes safely, without error in c because printf
returns int
value (the length of data has printed.) if not storing integer value:
printf("text");
why don't error this?
many functions in c return something. whether programmer decides value them - , ignoring return code leads bugs... in case of printf()
, return value seldom useful. provided allow following code:
int width; width = printf("%d", value); // find out how wide while (width++<15) printf(" "); width = printf("%s", name); while (width++<30) printf(" ");
i'm not saying that's code (there other ways too!), describes why function return value isn't used often.
if programmer decide ignore return value, there isn't compiler error - value merely forgotten. it's bit buying something, getting receipt, , dropping on floor - ignore returned value.
the latest compilers can instructed flag code returned values ignored. these compilers can taught functions' returns significant , aren't. printf()
in second category.
Comments
Post a Comment