Results 1 to 3 of 3

Thread: printf executes before should

  1. #1
    Join Date
    Jan 2012
    Beans
    161

    Angry printf executes before should

    Code:
    #include <stdio.h>
    
    
    
    char stringA[256];
    char stringB[256];
    
    
    char *pointerA;
    char *pointerB;
    char selection;
    char selection2;
    
    
    int main(){
    
    
      pointerA = stringA;   
      pointerB = stringB;
    
    
      void getFirstString(char *pointer);
      void getSecondString(char *pointer);
      void appendString(char *A, char *B);
      
    
    
      getFirstString(pointerA);
      getSecondString(pointerB);
      appendString(pointerA,pointerB);
    
    
      return 0;
    
    
    
    
    }
    
    
    
    
    
    
    void getFirstString(char *pointer){
    
    
      void getValue(char *ptr);
      do{
        printf("What is the first string : ");
        getValue(pointer);
        printf("So your 1string is ");
        puts(pointer);
        printf(" Is this correct?  Y or N : ");
        scanf("%c",&selection);
        }while(selection != 'Y');
    
    
    }
    
    
    void getSecondString(char *pointer){
      void getValue(char *ptr);
      do{
        printf("What do you wish to append?");
        getValue(pointer);
        printf("So your 2string is ");
        puts(pointer);
        printf(" Is this correct? Y or N : ");
        scanf("%c",&selection2);
        }while(selection2 != 'Y');
    
    
    }
    
    
    
    
    void getValue(char *ptr){
    
    
      fgets(ptr,120,stdin);
    
    
    
    
    }

    For some reason in getSecondString, the printf statement 'So your 2string is' executes before the fgets statement, looking like:

    Code:
    What do you wish to append?So your 2string is
    Anyone know why?

  2. #2
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,274

    Re: printf executes before should

    Quote Originally Posted by wingnut2626 View Post
    For some reason in getSecondString, the printf statement 'So your 2string is' executes before the fgets statement
    That's actually not what is happening; add some strategic debugging statements to get a clearer view of what is going on, here

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: printf executes before should

    By default, when the output is to console, it is line-buffered (in other words output happens when there is a "\n"). So your statement is executed, but the corresponding output is delayed. You have to flush the stream to force output to happen, or you can set the buffering mode for the stream. See http://www.gnu.org/software/libc/man...ling-Buffering
    Last edited by ofnuts; December 21st, 2013 at 05:18 PM.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •