Why don't we write int x = printf ("text"); so how does the printf () statement return an integer value every time?

Since the printf function returns an integer value each time it is used (the number of characters written on the screen), is it necessary to store this value in the int variable every time printf is called?

Edit:

If a function returns a specific value, why does C not require saving the value during a function call?

+6
source share
6 answers

If you think that your C program will be translated into assembly language (for example, x86 assembly), reading the return value of the function is just reading the value stored in the "eax" register of your CPU.

The C programming language has historically been written as a language that is easy to parse and compile on the very limited (today) PDP-11 computer.

Early C compilers (well, some of them) accept functions declared without return type AND without return, for example:

 myFunc(int *x) { *x = *x + *x; } 

This is a function that early C compilers interpret as

 int myFunc(int *x) { *x = *x + *x; return 0; } 

So the answer to your question might be "due to outdated code."

However, there are situations where it is necessary or should check the return value.

Rule 16.10, contained in the MISRA C guidelines, states: โ€œIf a function returns error information, then this error information must be tested.โ€

So, if you need to make sure printf () prints something, if you use the return value as "error information", following the recommendations of MISRA, you should check this value.

+1
source

This can only be if necessary.

If we it does not matter , no need to use the return value. no need to store the same.

+3
source

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.

+2
source

You can save the return value if you need it, for example, to make sure that you output the required number of characters (this sounds very unlikely to be needed, especially considering that during the production process you usually have more complete registration of the modules to process your outputs )

You can also directly say that you do not need to return the value to the compiler using (void) , as in

 (void) printf("hello world\n"); 

Please note that this has no effect other than suppressing warnings of some tools (e.g. lint )

+1
source

If a function returns a specific value, why does C not require saving the value during a function call?

This is probably worse from the usual C programming style programming point. You created a local variable with a value, but you are not using it:

  int x; x = printf(...); 

will give a warning that you are not using x. You would have to do this to solve both open ends:

  if (printf(...)<EXPECTED_OUTPUT_BYTES) exit(1); 

or similar. Fortunately, the output does not return anything.

+1
source

As others have said, you can ignore the result of any function call.

However, if you use gcc, there is a warn_unused_result function attribute that you can add that calls gcc to warn you if the result of the function crash is not used. Combine this with -Werror , and then you will check what results are used.

Here is an example from the gcc info page:

 int fn () __attribute__ ((warn_unused_result)); int foo () { if (fn () < 0) return -1; fn (); return 0; } 

This will warn (or about an error with -Werror ) about using fn (); .

0
source

All Articles