+ 1

I don’t understand, y output is 1

# include <stdio.h> int main(){ int x, s; x = scanf(“%d”, &x); s = x * x; printf(“%d”, s); return 0; } The output is 1, but I write 2 into scanf WHY?!

12th Jun 2022, 6:59 PM
Матвей Барканов
Матвей Барканов - avatar
9 Answers
+ 3
Probably because scanf returns 1. But either way, just scan it, you don't set x equal to the scanf function. https://code.sololearn.com/cjRc22Ri14AE/?ref=app
12th Jun 2022, 7:05 PM
Slick
Slick - avatar
+ 1
Look at the fix. I took away "x =" on line 5
12th Jun 2022, 7:08 PM
Slick
Slick - avatar
+ 1
Матвей Барканов "Ok, but how the meaning of scanf put into x?" scanf("%d", &x 👈HERE)
12th Jun 2022, 10:07 PM
Bob_Li
Bob_Li - avatar
0
Ok, but how the meaning of scanf put into x?
12th Jun 2022, 7:07 PM
Матвей Барканов
Матвей Барканов - avatar
0
Oh, great! It’s working! Thank you! But y scanf returned 1?
12th Jun 2022, 7:10 PM
Матвей Барканов
Матвей Барканов - avatar
0
I don't know, please use google. Plus, thats not the correct way to use scanf. The way i posted is. So use it right and you'll have no issues
12th Jun 2022, 7:11 PM
Slick
Slick - avatar
0
Because he used assignment operator on scanf , so c compiler gets it as there is only one value entered so x=1 therefore after multiplication it stays as 1 so prints as 1 !!
13th Jun 2022, 2:49 AM
Sandesh Verma
Sandesh Verma - avatar
0
scanf returns how many number of values it read. int n = scanf("%d %d", &x, &y) ; printf returns how many character it displayes on the output.. int y = printf("n=%d", n); //n=2 printf("%d", y) ; // 3 Hope it helps.. https://code.sololearn.com/cjh3dwj9kA34/?ref=app
13th Jun 2022, 9:53 AM
Jayakrishna 🇮🇳
- 2
Hii
13th Jun 2022, 3:33 PM
Sumit Verma
Sumit Verma - avatar