ron653
ron653

Reputation: 5

C - scanf execute before print

I try to run this code:

    #include <stdio.h>

int main() {

   char str1[20], str2[30];

   printf("Enter name: ");
   scanf("%s", str1);
   getc(stdin);

   printf("Enter your website name: ");
   scanf("%s", str2);
   getc(stdin);

   printf("Entered Name: %s\n", str1);
   printf("Entered Website:%s", str2);

   return(0);
}

from http://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm

so I expected to get this in the console:

Enter name: admin
Enter your website name: admin.com

Entered Name: admin
Entered Website: admin.com

but actually I got this in my console:

admin
admin.com
Enter name: Enter your website name: Entered Name: admin
Entered Website:admin.com

so I would like to know why the scanf executed before the print. maybe its related to using eclipse as IDE?

Upvotes: 0

Views: 106

Answers (2)

coopski
coopski

Reputation: 1

This is a compiler issue - ive only ever happen in eclipse. This is because it is trying to replicate unix compilation and therefor has some weird quirks

after every time you want to see output, place fflush(stdout);

Upvotes: 0

ebby94
ebby94

Reputation: 3152

Try adding fflush(stdout); between your printf and scanf.

Upvotes: 1

Related Questions