You are here:

C++/return value



hello sir,

scanf() returns the no of fields successfully assigned values.and if we are assigining to sone integer type variable ,it will return that value in that variable. what happend if we are not assigning it to some integer variable. it will return value to which variable?

thank you

If I understand what you are asking, you wish to know what happens to function return values (such as the one returned by the C/C++ library function scanf) if it is not assigned or otherwise used as in this example:

   int v(0);
   scanf("%d", &v );

In these cases the return value is lost. Both C and C++ allow function return values to be ignored in this way. Note that not storing the return value does not imply that there is a memory leak, although this may not be true in all cases as it depends on the value returned. For example if the return value is a pointer to some object then the documentation of the function should indicate if the caller is responsible for releasing this resource (e.g. freeing the memory or deleting the object) when done with the object, if so then the caller must store the returned value to prevent a memory leak.

In the case of functions like scanf ignoring the returned value will prevent your code from being able to detect and handle errors that occurred during execution of the function.

In other cases maybe the information returned is not always so useful  - for example many of the C/C++ library C-string functions such as strcpy return a pointer that is in fact a copy of one of the (pointer) parameters:

char * source_c_string("C string to copy");
char destination_c_string[256];
char * copied_c_ctring(strcpy(destination_c_string, source_c_string));

The pointer value of copied_c_string will be equal to the address of the destination_c_string parameter (remember that the name of an array variable is equivalent to the address of its zeroth element). In many situations this is not useful. Where it would be of use is if you were immediately doing something with the returned value - such as passing it to another function, strlen for example:

size_t dest_len(strlen(strcpy(destination_c_string, source_c_string)));

Otherwise it is not that useful and so the returned pointer is most usefully ignored:

strcpy(destination_c_string, source_c_string);

The alternative is that the code becomes littered with (local) variables just to collect returned values that are then not used.

Hope this helps.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.