Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!