By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,118 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,118 IT Pros & Developers. It's quick & easy.

Output of nested scanf statements in a printf

P: 1
iam an amatuer who is trying to get into the C programming but I have many doubts please help me. If scan is function than what value does scanf returns and similarly about printf what is the output of this statement
main()
int i;
printf(""%d",scanf("%d",scanf("%d",&i));
}
Jun 21 '07 #1
Share this Question
Share on Google+
4 Replies


DeMan
100+
P: 1,806
Hi neotan,

Welcome to TSDN. I am sure you will find a wealth of answers from the experts in the various forums.
I will move this question to the c(++) forum for you. In future, you can access the forums directly by selecting from the different tabs on the blue bar near the top of your screen.

I hope the c/c++ experts can be of assistance!
Jun 21 '07 #2

Expert 10K+
P: 11,448
iam an amatuer who is trying to get into the C programming but I have many doubts please help me. If scan is function than what value does scanf returns and similarly about printf what is the output of this statement
main()
int i;
printf(""%d",scanf("%d",scanf("%d",&i));
}
Bookmark this link. It's a nice reference for your C and/or C++ development.
scanf returns the number of items that were succesfully scanned and printf returns
the number of characters that were printed.

Your code example most likely produces a core dump because its second, third
etc. (if applicable) arguments are supposed to be addresses where the scan
results should be stored.

kind regards,

Jos
Jun 21 '07 #3

archonmagnus
100+
P: 113
In order to test the return values, try this code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main (void)
  4. {
  5.     int i;
  6.     printf("Enter an integer value: ");
  7.  
  8.     int j = scanf("%d", &i);
  9.     int k = printf("Items Scanned: %d", j);   // Outputs "1"
  10.  
  11.     printf("\nItems Printed: %d\n", k);     // Outputs "16" (Total character outputs from last printf)
  12.  
  13.     return 0;
  14. }
  15.  
Jun 21 '07 #4

sicarie
Expert Mod 2.5K+
P: 4,677
neotan-

Please also have a look at our Posting Guidelines, including those that request you use good thread titles, and code tags.

I've changed this thread title, please let me know if you don't think it adequately describes your issue.

Thanks, and welcome!
Jun 21 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.