If value of a scanf()
argument after scanf()
returns is undefined if the conversion was unsuccessful, but the most likely behaviour is that it remains unchanged. In this case b
is uninitialised so could have any value regardless of the behaviour of scanf()
.
Consider:
#include <stdio.h>
int main(void)
{
int a = -1, b = -1;
scanf("%d %d",&a,&b);
printf("
The value of a is : %d",a);
printf("
The value of b is : %d",b);
return 0 ;
}
I would expect in this case both a
and b
to have either the entered value or -1 if the conversion fails - even though that is not actually required.
scanf()
returns the number of arguments converted and assigned. Consider:
#include <stdio.h>
int main(void)
{
int a = 0, b = 0;
int convert = scanf("%d %d",&a,&b) ;
if( convert > 0 )
{
printf("
The value of a is : %d",a);
if( convert > 1 )
{
printf("
The value of b is : %d",b);
}
else
{
printf("
The value of b is undefined" );
}
}
else
{
printf("
The value of a is undefined" );
}
return 0 ;
}
For your test input 54test hello
it outputs:
45test hello
The value of a is : 45
The value of b is undefined
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…