Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not understanding the return value from scanf

Tags:

c

I have the following C program, and I don't understand the return value from scanf.

#include <stdio.h>

int main()
{
    int p,n1,n2;

    p=scanf("%d %d",&n1, &n2);

    printf("%d", p);
    return 0;
}

p is assigned the return value from scanf.
When I provide values for n1 and n2, the value of p is always 2. I don't understand why the value is always 2 regardless of what numbers I provide for n1 and n2.

like image 328
opu 웃 Avatar asked Nov 23 '25 11:11

opu 웃


1 Answers

scanf returns the number of values read, which in your case is 2.

like image 136
DFord Avatar answered Nov 24 '25 23:11

DFord



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!