user3595689
user3595689

Reputation: 33

trouble with Eclipse compiler

I used Eclipse to compile c code, but all of a sudden all my codes got trouble, which were all correct previously.

for example if I want use scanf input a argument, before the scanf a printf statement I will use for guiding the user. like printf("type the size\n"); but after compiling in Console I need type the size first, then the printf("type the size\n") command just pop up, which should be the other way round.

#include <stdio.h>
#include <stdlib.h>

void try(int a);

int main(void)
{
    int a;

    printf("type the size\n");
    try(a);

    return 0;
}

void try(int a)
{
    scanf("%d", &a);
    printf("%d\n", a);
}

the result: 2 type the size size is chosen 2 I need type a number first, here like I need type 2 first and then the "type the size" just pop up.

here is what I want :

type the size 2 size is chosen 2

Upvotes: 2

Views: 90

Answers (2)

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7128

try this one, for example,

#include <stdio.h>
#include <stdlib.h>
void try(int *a);
int main() {
 int a;
 printf("type the size\n");
 fflush(stdout);
 try(&a);
 return 0;
}
void try(int *a){
 scanf("%d", a);
 printf("%d\n", *a);
 fflush(stdout);
 return;
}

Also, you need to get the value set in atry back in main, so, you need to pass a a pointer as shown

Upvotes: 0

Spikatrix
Spikatrix

Reputation: 20244

Its a bug in eclipse and this has been reported by most of the people using eclipse and MinGW.

To overcome this problem, you could use fflush(stdout) after every call to printf or use the following in the start of main :

setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);

This will cause stdout and stderr to flush immediately whenever it is written to.

Upvotes: 3

Related Questions