Thread: help me out ,not sure why this is happenning?

  1. #1
    Registered User
    Join Date
    Mar 2023
    Posts
    1

    help me out ,not sure why this is happenning?

    help me out ,not sure why this is happenning?-ftocdoubt-jpg
    so here when i have \n in scanf its asking for two inputs instead of one , i wanted to ask why this was the case .SHouldnt \n just casue to move to the next line ,why is it asking for another input and then giving the result for just the first one?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,112
    First, please post your code in Code Blocks here in this forum rather than a fuzzy image.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      float f, c;
      scanf("%f", &f);
      c=(f-32)/1.8;
      printf("The temp in Celsius is %f\n", c);
    
      return 0;
    }
    Remove the '\n' from the scanf().

    Use a normal temp in Fahrenheit, such as 68 degrees F!

    Output:
    Code:
    $ ./foo 
    68
    The temp in Celsius is 20.000000

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    140

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Any whitespace (space, tab, newline, etc) in a scanf format string causes it to "eat" all whitespace at that point up until the next non-whitespace character. Therefore it is generally a mistake to put any kind of whitespace at the end of a scanf format string.

    It's also important to know that most scanf format specifiers also skip whitespace before scanning the input value. The only specifiers that do not skip whitespace are %c, %[], and %n (although %n doesn't actually scan a value but instead yields the number of characters read up to that point).
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Tags for this Thread