fgets is executed before any other code is run

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cclark18
    New Member
    • Jan 2010
    • 2

    fgets is executed before any other code is run

    Hi everybody

    I am a C newbie. A simplified version of my problem is below. The issue is that when the code is run, fgets is executed before any of the other code is run. I'm sure its some minor detail I am missing. Any help you can give in the matter would be appreciated.

    Thanks in advance.

    /* *************** *************** *************** ************* */
    #include <stdio.h>

    char line[100];
    int height;
    int width;
    float area;

    void go() {
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d %d", &width, &height);
    }

    int main() {

    printf("Enter width height? ");
    go();
    area = (width * height)/2.0;
    printf("The area is %f\n", area);
    return(0);
    }
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    A few questions for you to make it clearer...

    What exactly do you mean fgets() gets executed before any of the code is ran?
    Do you say you do not see anything printed by the printf()?
    What would you rather was executed first?

    Kind Regards,

    Alex.

    Comment

    • murugesandins
      New Member
      • Jun 2006
      • 4

      #3
      The program does the following sequence of flow:
      1. start the program from main function
      2. call the function printf("Enter width height? ") inside main and display the string "Enter width height? " in output
      3. Then call the function go()
      4. Inside the function go(), call the function fgets to get the input from the input stream(stdin) and store that value in the variable line.
      5. call the function sscanf which will take the input from the variable line, and those values are given to the variables width and height.
      6. Return to the main function
      7. calculate the area using (width * height)/2.0
      8. print the value of the variable of area using printf.
      Example output from linux platform:
      a.out
      Enter width height? 12 24
      The area is 144.000000
      Example output in vc++ on windows XP:
      bytes.exe
      Enter width height? 12 24
      The area is 144.000000

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        If you mean that fgets is executed before you see the output to printf that is because stdout is a buffered stream. The library is only actually required to output it when it sees a newline "\n" in the output stream or if the stream is flushed with a call to

        fflush(stdout);

        Since you have no '\n' in the text you are printing the library is not required to output it immediately and apparently in your case it doesn't so you need to make the fflush call to get the text ouput.


        As a side note never EVER call fflush on stdin

        fflush(stdin);

        it is undefined behaviour.

        Comment

        • murugesandins
          New Member
          • Jun 2006
          • 4

          #5


          characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment. Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            It is not clear if you are trying to support what I have said to contradict what I have said.

            If it is the later case and you are claiming what I have said is wrong then I suggest you read the replies to the thread you yourself have quoted rather than just quoting from the references given in the first post providing the background for OPs actual question. If you do you will find that what I have said is correct, the fflush call is at a minimum desireable if not actually required.

            Comment

            • cclark18
              New Member
              • Jan 2010
              • 2

              #7
              @myusernotyours - Looking back, I guess the question was a little vague, sorry. The console awaited my input before showing the first printf() statement as Banfa said.

              @murugesandins - Yes, that is what the program was supposed to do.

              @Banfa - That was the issue. Thank you sir, may your cup'eth overflow.

              Comment

              Working...