c++ – Why can I ignore return value of certain functions in C?

Question:

I am starting with C and I have the following doubt: Why if the definition of the scanf function indicates that it returns an integer value:

int scanf(const char *format, ...)

can it be used without taking this return value into account? The same happens with printf .

Example of use:

...
printf("Ingrese entero: ");
scanf("%d", &val );
...

Thanks a lot.

Answer:

Because the return value does not interest you.

You are not always going to use the return value of a function. For example, the integer that scanf returns is a value that tells you the number of characters read, or a negative number if nothing was read.

If you are not interested in knowing that, you can directly not receive the return value. In that case, the value is discarded and is nowhere to be found.

However, in some cases you will be interested in the return value, for example to know if a value was actually entered. In that case, you are going to want to know what that function returned, and surely you are going to want to check that a value of a certain length was actually entered.

Scroll to Top