-1
void getInputWith_gets()
{
    char firstName[5];
    char lastName[5];
    printf("Enter your first name: ");
    gets(firstName);
    printf("Enter your last name: ");
    gets(lastName);
    printf("Hello, %s, %s\n", firstName, lastName);
}

int main(int argc, char **argv)
{
    getInputWith_gets();
    //getInputWith_fgets();
    system("pause");
    return 0;
}

I am using MS Visual Studio 2017, I know the restriction of using the "gets()" function that I have a maximum of 5 chars to be entered but if I entered exactly 5 characters, the console prints correctly(and doesn't print "press any key to continue... due to "system("pause") statment") but the program get stuck at the debugger screen and after the last "printf" statement I get a a red error symbol with a pop-up saying: "Run-Time Check Failure #2 - Stack around the variable 'lastName' was corrupted." does this means that the "gets()" function will read 5 exclusive characters only?

  • 5
    Don't use `gets`. Never ***ever*** use `gets`. It's a dangerous function, and because of that have been obsoleted since the C99 standard, and removed completely from the latest C11 standard. – Some programmer dude Jun 26 '18 at 11:41
  • As for your problem, you seem to have forgotten that `char` strings in C are really called ***null-terminated** byte strings*. That *null-terminated* part is important, and of course needs space in your arrays. – Some programmer dude Jun 26 '18 at 11:42
  • a `char[5]` only has room for a string consisting of max. 4 characters (because it needs room for the `'\0'` terminator) – Sander De Dycker Jun 26 '18 at 11:42
  • I just came from Java background so am just new to C and all I wanted is an explanation for the exception as exceptions in Java are more descriptive – DesertXGhost Jun 26 '18 at 11:55
  • Then I suggest you get a few books to read from the beginning. And learn that C doesn't *have* exceptions. Unfortunately the underlying operating system and hardware might have what it calls "exceptions" but it's not the same as Java (or C# or C++) exceptions. – Some programmer dude Jun 26 '18 at 12:00
  • The comments are pretty clear. You try to stuff 6 bytes into a 5 bytes array. A 5 character string takes up 6 bytes (5 for the characters and one for the NUL terminator). In C there is no checking of array bounds at all and there are no exceptions. Google "c undefined behaviour". – Jabberwocky Jun 26 '18 at 12:04
  • this is what I get when insert more than 5 inclusive characters. This is the exception that I am talking about, and I know that C/C++ has no exception like index out of bound. https://ibb.co/nmwNzo – DesertXGhost Jun 26 '18 at 12:20
  • 1
    @DesertXGhost "Stack around the variable 'lastName' was corrupted". Right. The array is of size 5, but `gets` wrote 5 characters *plus* the trailing null character. So `gets` wrote beyond where it was supposed to, and that explains the exception. So the moral of the story is (1) allocate string arrays bigger than they need to be and (2) don't use `gets`. – Steve Summit Jun 26 '18 at 12:28
  • 2
    In situations like these, it almost never makes sense to say "I want to read a string of exactly 5 characters, so I'll make an array of exactly the right size." Strings are pretty inherently variable-length. So if you want to read someone's first name, say "The name will probably be 5 to 10 characters, but some are longer, and I need to remember to leave room for the trailing null character also, so to be on the safe side I'll make it `char firstName[25]`. Computers these days have lots and lots of memory. It's been a *long* time since we had to worry about keeping things small. – Steve Summit Jun 26 '18 at 12:33
  • What @SteveSummit says: don't bean-count. Unless you are in a RAM-restricted embedded environment, just [128] and be done with. "don't use gets" - for sure! – Martin James Jun 26 '18 at 13:03
  • Why not use `scanf` since first/last names (usually) don't contain any blank space characters. – kingW3 Jun 26 '18 at 13:15
  • "exceptions in Java are more descriptive" --> Java is like walking on a tight rope with a net. C is like walking on a tight rope [without a net](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2017/08/1503460312keeping-programming-skills-sharp-1024x475.jpg). It assumes code is well written and does not need checks. – chux - Reinstate Monica Jun 26 '18 at 13:52
  • @kingW3 It's true that most beginning programmers learn how to do input using `scanf`. At some point, though, you have to un-learn `scanf`, and learn some more robust techniques for doing input, because `scanf` is basically useless for real programs. So I'll never discourage someone from starting off on the right foot and doing line-based input from the get-go. (Although not using `gets`, of course.) – Steve Summit Jun 26 '18 at 14:14

1 Answers1

0

You have multiple bugs here:

Also note that the function format void getInputWith_gets() is obsolete style, you should be writing void getInputWith_gets(void).

Overall, it seems you are learning C from a completely outdated source (over 20 years outdated).

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • `void getInputWith_gets()` as part of a function definition as OP did is not obsolete. As part of a declaration, it is obsolete C11 6.11.6. IAC, I like the explicit `(void)` in function definitions too. – chux - Reinstate Monica Jun 26 '18 at 13:56
  • @chux Have you seen anyone writing function declarations as `void getInputWith_gets(void);` but definitions as `void getInputWith_gets()`?. That would be a very strange coding style and code smell. – Lundin Jun 26 '18 at 14:07
  • Yes I have and it has an odor of higher maintenance - though not obsolete. – chux - Reinstate Monica Jun 26 '18 at 14:11