0

I am aware fgets() and gets() but I was told those two functions are deprecated so I would rather use something else. I used scanf but the issues I am running into is that scans stops reading after it hits a space so if I entered a first and last name then it would only return the first string. This is my original code for user input with scanf but I realized if a user has three strings like John Doe Smith then it would only read John Doe.

char firstName[100], lastName[100];
printf("Please enter your first and last name: \n");
scanf("%s%s", firstName, lastName);

This returns the right idea but only if user inputs two strings like John Doe.

since its the beginning of the semester, I want to keep the code simple and not get into any other functions that seem too much to use.

Barmar
  • 596,455
  • 48
  • 393
  • 495
user1829
  • 33
  • 5
  • 3
    Far from being deprecated `fgets()` is very useful. However `gets()` is no longer a valid C function. Your `scanf` won't work for a two-word first name or surname, such as "Betty Jo Davis" or "Pete Da Silva" because the `%s` format specifier will stop scanning at the first whitespace character. But it should work for two words such as the name "John Smith". – Weather Vane Jan 29 '20 at 23:52
  • When I use fgets() in my code and compile it, my terminal returns that it "isn't safe" to use. – user1829 Jan 29 '20 at 23:57
  • 4
    Is that Microsoft's idea of "safe"? No C code is safe, and there is little that MS does other than to confuse further. C is safe when used correctly. – Weather Vane Jan 29 '20 at 23:58
  • yes I'm using visual studios text editor and my terminal on my Mac – user1829 Jan 30 '20 at 00:00
  • 5
    MS will lead you up the garden path - away from standard C. They imagine they call the tune, but ignore the C standard themselves. – Weather Vane Jan 30 '20 at 00:00
  • 2
    Please note that user input is never simple, often the trickiest part of a program. – Weather Vane Jan 30 '20 at 00:07
  • MS is trying to get you to use the Annex K functions, which were hyped as being safer than the standard ones, but which are just as easy if not easier to misuse in ways that make them unsafe and which pretty much no implementors but MS support. Annex K may even be removed in a future edition of the standard. `gets` is actually deprecated (even removed from the language) but `fgets` is perfectly fine and is generally considered best-practice. – R.. GitHub STOP HELPING ICE Jan 30 '20 at 03:21
  • 1
    @WeatherVane "MS will lead you up the garden path - away from standard C" --> sounds like their marketing strategy of [Embrace, extend, and extinguish](https://en.wikipedia.org/wiki/Embrace,_extend,_and_extinguish) – chux - Reinstate Monica Jan 30 '20 at 03:30
  • @user1829 Do not use `scanf()` until you know why you should not use it. – chux - Reinstate Monica Jan 30 '20 at 03:32
  • 1
    It's a typo in the compiler warning, it should more accurately say "Microsoft Visual Studio is not safe to use". Instead you should be using a standard compliant C compiler. – Lundin Jan 30 '20 at 07:41
  • @Lundin I'm using Visual Studios as my text editor and compiling using gcc – user1829 Jan 30 '20 at 18:07
  • @user1829 Obviously you aren't, or you wouldn't get that warning. Only the broken Microsoft compiler gives such incorrect warnings. – Lundin Jan 31 '20 at 07:53

1 Answers1

2

fgets() is the preferred C standard way to read from standard input. It is safe as long as the buffer you pass in is valid memory that belongs to you, and the length you pass in is equal to or less than the size of the buffer.

More sophisticated user-input libraries are available, such as Gnu Readline, but if you're just learning the language, stick with standards like fgets().

Lee Daniel Crocker
  • 12,296
  • 1
  • 24
  • 47
  • Is fgets() the most standard function to use? I see it involves pointers and we haven't exactly went over that yet. I don't know if there are any other standard functions. I think fgets() is my best option right now. – user1829 Jan 30 '20 at 00:28
  • 2
    Eventually you'll get to things like `read()` and `fread()` as well, but they're not a simple to use as `fgets()` for starting out. The buffer argument doesn't have to be a pointer--it can be an array (e.g. `char buf[80]; fgets(buf, sizeof buf, stdin);`. – Lee Daniel Crocker Jan 30 '20 at 00:34