Why value of second variable is irrelevant after adding space or new line ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yogesh0551
    New Member
    • Oct 2014
    • 5

    Why value of second variable is irrelevant after adding space or new line ?

    Hello everyone! I'm newbie in programming learning C language.I'm little confused right now.I tried to Google about it but can not find out the satisfactory result so i thought to sort out by asking the question in this website.<br/>Have a look at this short program -
    Code:
    #include<stdio.h>
    int main()
    {
    int num1,num2;
    printf("enter the value of num1 and num2:");
    scanf("%d %d",&num1,&num2);
    printf("num1 = %d and num = %d",num1,num2);
    return 0;
    }
    When i enter value For example- 215-15 without space or new line than it gives out num1 = 215 and num2 = -15 but when i enter space or new line between 215- and 15 then it gives output "num1 = 215 and num2 = -175436266(or any unexpected number).
    I know that when scanf() reads any character which is not in the catagory of conversion specification it put back that character and end processing other inputs.But in the first case -(minus sign) seems to be irrelevent input according to the conversion specification but it shows correct output but in the later case it not showing correct output.Why?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    So you are processing 215- 15.

    Characters are obtained that are int. These would be 215. So num1 is 215.

    The process repeats. The - is obtained and the sign bit of num2 is set. Oops. A non-int character is obtained. num2 at this point has a sign bit set and a garbage value.

    It's always a good idea to check the return value of scanf. In this case, scanf should return 2 indicating 2 variables were successfully processed.

    A return of 0 indicates an error occurred.

    A return of EOF indicates the input buffer ran out before the process of the variable was complete.

    If you scanf one variable at a time you can pinpoint which variable errored out.

    Comment

    • Yogesh0551
      New Member
      • Oct 2014
      • 5

      #3
      why they are successfully assigned in first case and not in the second

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        In the first case it was 215-15.

        So 215 was scanned but the - was not. That's because integers don't end in non-digits. That left -15 which was scanned into num2.

        The essential point is that scanf stops scanning when a character is obtained that is not for the type being scanned.

        If you display the return from the scanf of 215- 15 you will see 1. That means one variable was successfully scanned. It should have been 2. So you an infer that the second variable had some kind of problem. In fact only the - was scanned then came the space (or newline or any non-int character) and the scan was stopped. - is not a valid int. The 15 was never scanned.

        BTW: That 15 is still in the input buffer just waiting for the next scanf. Then suddenly 15 pops out of the buffer instead of the number that was just entered.

        Input buffer parsing can get complex. I would not mess with it until you have A COUPLE OF YEARS experience in C. Until then just enter what is expected.

        Comment

        Working...