Why is printf () an unclean function?

As far as I know, unclean functions are those that do not always return the same value when called with the same parameters (I have to skip something, or maybe I'm mistaken, correct me if I find it).

So why printf()is it considered an unclean function?

+5
source share
6 answers

No, an "unclean" function is a function with side effects .

In other words, no matter how many times you call it, a pure function will affect nothing but its output.

For example, it is unclean , although it returns zero: foo

int x;
int foo() { x++; return 0; }
int bar() { return x; }

foo , bar().

printf , " " - , - ( ..).
, , .
printf , , , - ( - ). .

: ( , ), printf , getchar.:) , .

+15

, . , , , . , printf , , , I/O .

+7

, printf , I/O. I/O - I/O (, ).

+3

, , . , printf.

P.S. printf , , , , -, , .

+1

printf() , - .....

0

Many answers explain that printfI / O has a side effect, but printfmay have other side effects. For example, the specifier %nallows you printfto write to the specified address (and is the cause of some security vulnerabilities).

0
source

All Articles