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

  1. #1
    Registered User
    Join Date
    Mar 2023

    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
    New York, NY
    First, please post your code in Code Blocks here in this forum rather than a fuzzy image.

    #include <stdio.h>
    int main(void)
      float f, c;
      scanf("%f", &f);
      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!

    $ ./foo 
    The temp in Celsius is 20.000000

  3. #3
    Registered User
    Join Date
    Apr 2021

  4. #4
    Registered User
    Join Date
    Dec 2017
    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