Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't we write int x = printf("text"); since the printf() statement returns an integer value every time?

Tags:

c

printf

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

Edit:

If a function is returning certain value, why doesn't C make it necessary to store the value at the time of the function call?

like image 564
n00b Avatar asked Jul 24 '15 07:07

n00b


5 Answers

It can be, only if it is necessary.

If we don't care don't want the return value to be used. there is no use storing the same.

like image 180
Sourav Ghosh Avatar answered Oct 19 '22 20:10

Sourav Ghosh


If you have an idea how you are going to use x then of course you may 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 application of the return value of printf.

However sometimes it can be used for example to print tables with aligned columns.

For example

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" );
}

Its output is

Hello               World

Here is another example where the return value of printf finds a useful application.

Let's assume that you need to output a sequence of numbers separated with a comma like for example

1, 2, 3, 4, 5, 6, 7, 8, 9

How to output such a sequence using only a single loop without placing print statements outside the loop?

Here is a program that shows how it can be done based on using the return value of the function printf. :) 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; 
 } 

As for your program

int foo(int x) {
  return x;
}
int main() {
  foo(10);
}

then calling function foo does not have any effect. It does nothing and in fact can be removed. And not all compilers issue a message for your program. It seems that the compiler you are using has a compiler option that forse the compiler to consider warnings like errors. So your compiler wants that you would pay the attention to that the call of the function does not have any effect. It is possible that you made a logical error.

On the other hand calling function printf has a visible effect.

like image 27
Vlad from Moscow Avatar answered Oct 19 '22 19:10

Vlad from Moscow


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

The C programming language was historically written to be a language simple to parse and compile on a very limited (for today standards) PDP-11 computer.

Early C compilers (well, some of them) accepted functions declared without a return type AND without a return, like the following:

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

This is a function that those early C compilers would interpret as

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

So the answer to your question can be 'because of legacy code'.

There are however situations when the return value should or must be checked.

Rule 16.10 from MISRA C guidelines states that: "If a function returns error information, then that error information shall be tested".

So if you need to be sure the printf() has printed something, if you're using the return value as an 'error information', following MISRA guidelines, you must check that value.

like image 1
Marco Sanfilippo Avatar answered Oct 19 '22 18:10

Marco Sanfilippo


You can store the return value if you need it, for example to be sure that you are outputting the desired number of characters (it sounds very unlikely that one has this needs, especially considering that in production you usually have more comprehensive logging modules to handle your outputs).

You can also explicitly say that you don't care about that return value to the compiler with a (void) as in

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

Note that this has no implications other than suppressing warnings of some tools (e.g., lint)

like image 1
Vincenzo Pii Avatar answered Oct 19 '22 19:10

Vincenzo Pii


If a function is returning certain value, why doesn't C make it necessary to store the value at the time of the function call?

This is probably worse from the normal C programming style point of view: You have created a local variable with a value, but you don't use it:

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

would give a warning that you're not using x for anything. You would have to do this to solve both open ends:

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

or similar. Luckily, exit doesn't return anything.

like image 1
Mirar Avatar answered Oct 19 '22 18:10

Mirar