Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Very much Thanks in advance.
I am not a very good programmer in C. I have a doubt, it may be theoretical and conceptual problem.
Following code always gives wrong output, output is always 1?

XML
#include <stdio.h>

void main()
{
  int a = 100; // even tried initialize with 200 still output will be 1
  printf("%d", scanf("%d", &a));
}


User-input: 10
Output: 1

User-input: 99
Output: 1

Note: I am using DevC++ environment.
Posted
Comments
ggupta2009 16-Feb-12 0:18am    
Is it due to standard-Input-buffer is not flushed (for scanf()) and before that printf() is called. But even then output should be initialized value of a? But as I have observed always output is 1?

yes you can, but you must have to know what printf function will print.
//copied from cplusplus.com
On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned.


to learn more about scanf follow the link
 
Share this answer
 
I can Understand ur problem have done a little mistake.

U think that scanf return value which it read from user but that is wrong.

scanef return number of items successfully read. in your case it is one(1) that is 'a', The in put given by user is stored in a not return.

printf("%d", scanf("%d %d", &a,&b));

it will print 2 and so on.

for more referance read this: scanf Referance[^]
 
Share this answer
 
Comments
ggupta2009 17-Feb-12 1:04am    
Thank you very much...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900