-3

I've seen various posts of how using scanf before fgets might cause problems. However, it makes no sense to me why adding it AFTER should cause the first statement to break.

This is what I have:

  unsigned char key;
  unsigned char message[100];

  printf(" Please input a message \n  ");
  fgets(message, MAX_BUF, stdin); 
  printf("Your message is:  %s \n", message);


  // Read user input for message
  printf(" Please input a numeric key\n  ");
  scanf("%d", &key);

  printf("Your message is:  %s \n", message);
  printf("strlen(message) = %d \n", strlen(message));

And this is a "trial run":

  Please input a message
   hello
   Your message is: hello

  Please input a numeric key
   123

  Your message is: 
  strlen(message) = 0

Like this, the message variable ends up empty no matter the input. However, if I comment out the line with scanf, everything works (except I can't assign key).

Help!

5
  • 4
    It shouldn't be possible for code that only runs later in your program to go back in time and change your earlier output. So please use the EDIT button on your question to provide a "Minimal, Complete, Verifiable Example". Without one, it's not possible to know exactly what your definitions are or what might be causing the issue you are seeing. Commented Oct 3, 2014 at 1:48
  • Vital to report the return values from fgets() and scanf() to help understand the issues. Showing your input helps. Commented Oct 3, 2014 at 1:48
  • Thanks for the comments, it's my first question in Stack Overflow so I apologize for the incompleteness of the post. I have edited it and hope it is good now. Commented Oct 4, 2014 at 17:59
  • @HostileFork undefined behaviour can cause time travel. (Although in this code no time travel is necessary, as the bogus output does not appear until after the UB)
    – M.M
    Commented Oct 4, 2014 at 20:34
  • @MattMcNabb Hmmm. I see a possibly unexplored area for movie scripts now. "The universe is a giant computer, and if you break the rules the universe compiler is free to reorder events however it wants. Michael J Fox and Christopher LLoyd return in Back to the Future IV: Undefined Behavior" (Perhaps the universe spec said 87 mph was the fastest a DeLorean should go?) Commented Oct 4, 2014 at 20:48

1 Answer 1

1

The %d format specifier to scanf() is a promise that the next argument is the address of a memory buffer that is large enough to hold an int (usually 4 or 8 bytes), but your key is just a 1-byte unsigned char, so scanf() will write 3 (or perhaps 7) bytes past the end, trampling on whatever occupies that memory -- in this case, very likely message. What exactly it writes there will depend on the endianness of your system, but for small inputs (<= 255) on a little-endian system (e.g. x86) its second byte (which will be written to the first byte of message) will be 0, which effectively null-terminates message at zero length.

3
  • %d is a promise that the next argument is actually a pointer to int . Doing anything else (even if the right size) causes undefined behaviour, although you may get away with it.
    – M.M
    Commented Oct 4, 2014 at 20:35
  • Thanks. I had switched to %d after %u was not working either. I just found out that the correct format specifier is %hhu. Commented Oct 5, 2014 at 18:37
  • @PabloPrado: Glad you solved the problem. If my answer was helpful, please upvote it and/or mark it as the accepted answer :) Commented Oct 5, 2014 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.