Thread: Scanf in C language

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    13

    Post Scanf in C language

    Suppose that we call scanf as follows:
    scanf(“%d%f%d”, &i, &x, &j);
    If the user enters
    10.3 5 6
    what will be the values of i, x and j after the call? (Assume that i and j are int variables and x
    is a float variable).

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    13

    Scanf in C language

    Suppose that we call scanf as follows:
    scanf(“%f%d%f”, &x, &i, &y);
    If the user enters
    12.3 45.6 789
    what will be the values of x, i and y

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So perhaps you should create a small C program and try each one and see the results.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    Well i tried but i am not sure if my results are correct..... I have been told these questions are tricky so please someone can help me out to see hwta the final and correct results will be.THANKS

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Well i tried but i am not sure if my results are correct
    So what ARE your results?

    > ..... I have been told these questions are tricky
    They're not tricky at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    100.3000005

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by vicky2
    scanf(“%d%f%d”, &i, &x, &j);
    If the user enters
    10.3 5 6
    i = 10
    x and j have indeterminate values and cannot be used
    Also, the return value from that scanf() call was 1 to let you know only 1 assignment was made.


    Quote Originally Posted by vicky2 View Post
    scanf(“%f%d%f”, &x, &i, &y);
    If the user enters
    12.3 45.6 789
    x = 12.3
    i = 45
    y is indeterminate and cannot be used.
    Also, the return value from that scanf() call was 2 to let you know only 2 assignments were made.

    ~~~~~~~~

    Remember to always test the return value of scanf()
    Code:
    if (scanf("%f%d%d", &x, &i, &j) != 3) /* error */;

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by vicky2 View Post
    100.3000005
    It might be worth printing out some newlines or other white-space between each number.

    printf("%d\n%f\n%d\n", i, x, j);
    rather than
    printf("%d%f%d\n", i, x, j);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Oops ... my bad at post #7

    first scanf gets 10, 0.3, and 5 (and keeps " 6" in the buffer)
    second scanf gets 12.3, 45, and 0.6 (and keeps " 789" in the buffer)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is C++ or C language is a high Level language?
    By uthmankhale in forum C++ Programming
    Replies: 5
    Last Post: 08-25-2011, 06:00 PM
  2. What's the Difference Between a Programming Language and a Scripting Language?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-15-2005, 04:46 PM
  3. C Language And A Scripting Language
    By DarkSpy in forum C Programming
    Replies: 9
    Last Post: 06-26-2003, 08:05 AM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  5. Computer Language VS Spoken Language
    By Isometric in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-04-2002, 03:47 PM