-5

I am unsure why my code is skipping the printf after scanf. Only after I input my gets(array) does my printf print the statement.

void stack_(void) {
  int amount;
  printf("PLease enter amount\t");
  scanf("%i ", &amount);
  char array[amount];
  printf("user amount is %i\n", amount);
  gets(array);
  puts(array);
}
mediocrevegetable1
  • 4,086
  • 1
  • 11
  • 33
LePiff
  • 27
  • 3
  • Please 1. format your code properly 2. copy-paste as text the interactive session from your terminal/console/whatever showing what is your input and how it is interleaved with the output. – Antti Haapala -- Слава Україні Mar 06 '21 at 10:15
  • 3
    [never use gets](https://stackoverflow.com/q/1694036/6699433) – klutt Mar 06 '21 at 10:18
  • you have to use gets if you want to add blank space to your arrays – LePiff Mar 06 '21 at 10:27
  • 3
    @LePiff That proves that you did not read the link about why you should never use gets – klutt Mar 06 '21 at 10:34
  • But you could add a `fflush(stdout);` after the `printf` – klutt Mar 06 '21 at 10:43
  • who said that `gets()` is used to add blank space to arrays? – phuclv Mar 06 '21 at 10:47
  • `gets` is catching the newline left in the input stream from the `scanf` call for `amount`; you'll need some way to remove it before the `gets` call (usually something like `while (getchar() != '\n') /* empty loop */ ;`). But like klutt says, never use `gets` - use `fgets` instead. – John Bode Mar 08 '21 at 14:57
  • Would you say to use gets only when using Malloc due to the fact that i won't need to predefine my array size? since using fgets, I will need to specify the amount i wish to input.... – LePiff Mar 13 '21 at 21:21
  • @LePiff: ***NEVER*** use `gets`. It was removed from the standard library in the 2011 revision of the standard. It *will* (not might, but *will*) introduce a point of failure in your code. Yes, you need to specify the maximum number of characters to read with `fgets`, meaning you need to know how large your input buffer is. C doesn't automatically extend storage for you as you write to it (which is part of *why* `gets` was so dangerous), you have to manage all that yourself. – John Bode May 03 '21 at 18:23

1 Answers1

0

%d , not %i . And you have a waste space in scanf