If you have an idea how you are going to use x, then of course you can write
int x = printf( "text" );
Otherwise, the return value of the function is simply discarded because it is not used.
And in most cases, programmers do not find a useful printf return value application.
However, sometimes it can be used, for example, to print tables with aligned columns.
for instance
int n = printf( "%s", SomeString ); if ( n < COLUMN_WIDTH ) printf( "%*c", COLUMN_WIDTH - n, ' ' );
Consider this simple program.
#include <stdio.h> int main( void ) { const int COLUMN_WIDTH = 20; int n = printf( "%s", "Hello" ); if ( n < COLUMN_WIDTH ) printf( "%*c", COLUMN_WIDTH - n, ' ' ); printf( "%s", "World" ); }
His conclusion
Hello World
Here is another example where printf returns a useful application.
Suppose you need to output a sequence of numbers separated by a comma, for example,
1, 2, 3, 4, 5, 6, 7, 8, 9
How to output such a sequence using only one cycle without placing print statements outside the cycle?
Here is a program that shows how this can be done based on the return value of the printf function. :) Try it.
#include <stdio.h> #include <stdlib.h> #include <time.h> int main( void ) { size_t n = 0; printf( "Enter the number of elements in the array (greater than 0): " ); scanf( "%zu", &n ); if ( n == 0 ) exit( 1 ); int a[n]; srand( ( unsigned int )time( NULL ) ); size_t i; for ( i = 0; i < n; i++ ) a[ i ] = rand() % n; i = 0; do { printf( "%d", a[ i ] ); } while ( ++i < n && printf( ", ") > 0 ); return 0; }
Regarding your program
int foo(int x) { return x; } int main() { foo(10); }
then the calling function foo has no effect. It does nothing and can actually be deleted. And not all compilers give a message for your program. It seems that the compiler you are using has a compiler option that forces the compiler to consider warnings such as errors. Therefore, your compiler wants you to pay attention to the fact that calling a function has no effect. Perhaps you made a logical mistake.
The printf calling function, on the other hand, has a visible effect.