Why printf("test"); does not give any error?

Megha M

If int x=printf("test"); executes safely, without error in because printf returns an int value (the length of data it has printed.) But what about if we are not storing that integer value:

printf("text"); 

Why don't we get an error from this?

John Burger

Many functions in C return something. Whether the programmer decides to do anything with that value is up to them - and often ignoring the return code leads to bugs... But in the case of printf(), the return value is seldom useful. It is provided for to allow the following code:

int width;

width = printf("%d", value); // Find out how wide it was
while (width++<15) printf(" ");
width = printf("%s", name);
while (width++<30) printf(" ");

I'm not saying that's good code (there are other ways to do this too!), but it describes why a function could return a value that isn't used very often.

If the programmer does decide to ignore the return value, there isn't a compiler error - the value is merely forgotten. It's a bit like buying something, getting the receipt, and dropping it on the floor - ignore the returned value.

The latest compilers can be instructed to flag code where returned values are ignored. But even these compilers can be taught which functions' returns are significant and which aren't. printf() would be in the second category.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why printf("test"); does not give any error?

From Dev

Core Data update does not save and does not give any error, why?

From Dev

Why does setting (test in assembly) give type error?

From Dev

Why does this conversion give an error?

From Dev

Why does "printf" not produce any output?

From Dev

Why does "printf" not produce any output?

From Java

Why does printf("%f",0); give undefined behavior?

From Dev

Why does printf(i) give 0 as output in this program?

From Dev

Why does getInt inside RDD[Row].map give "error: value getInt is not a member of Any"?

From Dev

C++ Why does this program does not give any output?

From Dev

Why does interp function give me error?

From Dev

Why does this if statement give me an error

From Dev

Why does this SQL query give a syntax error?

From Dev

Why does not gcc give me warning or error?

From Dev

Why does "tasksel" give an "aptitude failed" error?

From Dev

Why does "small" give an error about "char"?

From Dev

Why does this dict give me a syntax error?

From Dev

Why does the compiler not give an error for this addition operation?

From Dev

Why does "tasksel" give an "aptitude failed" error?

From Dev

Why does this html code give me an error?

From Dev

Why does this SQL query give a syntax error?

From Dev

Why does calling a property on a string not give an error?

From Dev

Why does Agda give error "expected: ℕ, actual: ℕ"

From Dev

Why does *= not give any errors when implicitly casting a float to an int?

From Dev

Why TPL Dataflow block.LinkTo does not give any output?

From Dev

Why does mipmapping give me barely any performance increases?

From Dev

Access to out of array range does not give any error

From Java

Why does c = ++(a+b) give compilation error?

From Dev

Why does this complex rational give an overflow error in Julia?

Related Related

  1. 1

    Why printf("test"); does not give any error?

  2. 2

    Core Data update does not save and does not give any error, why?

  3. 3

    Why does setting (test in assembly) give type error?

  4. 4

    Why does this conversion give an error?

  5. 5

    Why does "printf" not produce any output?

  6. 6

    Why does "printf" not produce any output?

  7. 7

    Why does printf("%f",0); give undefined behavior?

  8. 8

    Why does printf(i) give 0 as output in this program?

  9. 9

    Why does getInt inside RDD[Row].map give "error: value getInt is not a member of Any"?

  10. 10

    C++ Why does this program does not give any output?

  11. 11

    Why does interp function give me error?

  12. 12

    Why does this if statement give me an error

  13. 13

    Why does this SQL query give a syntax error?

  14. 14

    Why does not gcc give me warning or error?

  15. 15

    Why does "tasksel" give an "aptitude failed" error?

  16. 16

    Why does "small" give an error about "char"?

  17. 17

    Why does this dict give me a syntax error?

  18. 18

    Why does the compiler not give an error for this addition operation?

  19. 19

    Why does "tasksel" give an "aptitude failed" error?

  20. 20

    Why does this html code give me an error?

  21. 21

    Why does this SQL query give a syntax error?

  22. 22

    Why does calling a property on a string not give an error?

  23. 23

    Why does Agda give error "expected: ℕ, actual: ℕ"

  24. 24

    Why does *= not give any errors when implicitly casting a float to an int?

  25. 25

    Why TPL Dataflow block.LinkTo does not give any output?

  26. 26

    Why does mipmapping give me barely any performance increases?

  27. 27

    Access to out of array range does not give any error

  28. 28

    Why does c = ++(a+b) give compilation error?

  29. 29

    Why does this complex rational give an overflow error in Julia?

HotTag

Archive