Home > Software engineering >  why does scanf() input variable not output as the value it contains? instead it prints the memory lo
why does scanf() input variable not output as the value it contains? instead it prints the memory lo

Time:06-11

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int valueEntered ,x;
   
    scanf("enter the value:%d",&valueEntered);

    x = valueEntered;

    printf("the value entered is:%d\n", x);

    return 0;
}

CodePudding user response:

Split this call of scanf

scanf("enter the value:%d",&valueEntered);

into calls of printf and scanf

printf( "enter the value " );
scanf("%d",&valueEntered);

Otherwise the call of scanf will try to find the string "enter the value:" in the input stream.

In general you should check whether a call of scanf was successful.

CodePudding user response:

The scanf operation will fail unless the user himself enters "enter the value:" followed by a number. However, you probably do not want the user to have to enter that string, but you instead want your program to print that string. Therefore, instead of

scanf("enter the value:%d",&valueEntered);

you should write:

printf( "Enter a value: " );
scanf( "%d", &valueEntered );

However, before using the result of a scanf operation, you should always check the return value of scanf, in order to verify that it was successful. Therefore, it would be better to write the following:

printf( "Enter a value: " );

if ( scanf( "%d", &valueEntered ) != 1 )
{
    printf( "Conversion failure!\n" );
    exit( EXIT_FAILURE );
}

//if we reach this line, then scanf was successful, and we
//can now use the value of the variable "valueEntered"

In your question, you wrote:

instead it prints the memory location

It is not printing the memory location. Instead, it is printing garbage, i.e. it is printing whatever value valueEntered had before the scanf function call, because scanf did not assign a value to that variable.

If you change your program to the following, so that it checks the return value of scanf, then it will become obvious that scanf is failing:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int valueEntered ,x;
   
    if ( scanf("enter the value:%d",&valueEntered) != 1 )
    {
        printf( "Conversion failure!\n" );
        exit( EXIT_FAILURE );
    }

    x = valueEntered;

    printf("the value entered is:%d\n", x);

    return 0;
}

This program has the following behavior:

65
Conversion failure!

However, if the user himself enters "enter the value" following by a number, then scanf succeeds:

enter the value:65
the value entered is:65

If you instead use the following program, then everything will work as it should:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int valueEntered, x;
   
    printf( "Enter a value: " );

    if ( scanf( "%d", &valueEntered ) != 1 )
    {
        printf( "Conversion failure!\n" );
        exit( EXIT_FAILURE );
    }

    x = valueEntered;

    printf( "The value entered is: %d\n", x );

    return 0;
}

This program has the following behavior:

Enter a value: abc
Conversion failure!
Enter a value: 65
The value entered is: 65

As you can see, it is no longer necessary for the user himself to type "enter the value:" for the input to be accepted.

  • Related