holzschu / holzschu/a-shell

scanf in C code not working as expected

Open
#1,046 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Perl
Stars
3.9k
Forks
213
Avg merge
1m
Merged PRs (30d)
1

Description

After compiling the [following code](https://onecompiler.com/c/44wzuu388) with `clang` and running it:

```
#include

int main(void)
{
int miles, yards;
double kilometers;

printf("Please input the number of miles: ");
scanf("%d", &miles);
printf("...and the number of yards: ");
scanf("%d", &yards);

kilometers = (miles + yards/1760.0) * 1.609344;
printf("\nThat makes %lf kilometers.\n\n", kilometers);

return 0;
}
```

the order of operations is mysteriously mixed up. It asks for two inputs first and then prints all of the `printf` commands at the end -- something like
```
Please input the number of miles: ...and the number of yards:
That makes 35.425685 kilometers.
```

Is this a known issue -- can others duplicate this issue -- or is there something wrong with my particular instance of the app?

The code worked fine in the browser in [here](https://onecompiler.com/c/44wzuu388) (OneCompiler)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the linked C example in a-Shell with clang and compare its prompt behavior with the OneCompiler result. Inspect how the terminal handles printf output before scanf input; done means the prompts appear before input, or the terminal-specific buffering behavior is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.