Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the best way to get input from user in C?

Tags:

c

user-input

Many people said that scanf shouldn't be used in "more serious program", same as with getline.

I started to be lost: if every input function I got across people said that I shouldn't use any of them, then what should I use? Is there is a more "standard" way to get input that I'm not aware of?

like image 634
user1115057 Avatar asked Feb 14 '12 14:02

user1115057


People also ask

How we can take input from user in C?

In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards.

Which is the safest way to take string input in C?

Use fgets() besides gets()

How do you receive an input from a user?

The simplest way to obtain user input is by using the input() function. This function prompts the user for an input from the keyboard. Once the user has give the input and pressed Enter, the program flow continues. The input() function will always output a string.


2 Answers

Generally, fgets() is considered a good option. It reads whole lines into a buffer, and from there you can do what you need. If you want behavior like scanf(), you can pass the strings you read along to sscanf().

The main advantage of this, is that if the string fails to convert, it's easy to recover, whereas with scanf() you're left with input on stdin which you need to drain. Plus, you won't wind up in the pitfall of mixing line-oriented input with scanf(), which causes headaches when things like \n get left on stdin commonly leading new coders to believe the input calls had been ignored altogether.

Something like this might be to your liking:

char line[256]; int i; if (fgets(line, sizeof(line), stdin)) {     if (1 == sscanf(line, "%d", &i)) {         /* i can be safely used */     } } 

Above you should note that fgets() returns NULL on EOF or error, which is why I wrapped it in an if. The sscanf() call returns the number of fields that were successfully converted.

Keep in mind that fgets() may not read a whole line if the line is larger than your buffer, which in a "serious" program is certainly something you should consider.

like image 85
FatalError Avatar answered Sep 18 '22 23:09

FatalError


For simple input where you can set a fixed limit on the input length, I would recommend reading the data from the terminal with fgets().

This is because fgets() lets you specify the buffer size (as opposed to gets(), which for this very reason should pretty much never be used to read input from humans):

char line[256];

if(fgets(line, sizeof line, stdin) != NULL)
{
  /* Now inspect and further parse the string in line. */
}

Remember that it will retain e.g. the linefeed character(s), which might be surprising.

UPDATE: As pointed out in a comment, there's a better alternative if you're okay with getting responsibility for tracking the memory: getline(). This is probably the best general-purpose solution for POSIX code, since it doesn't have any static limit on the length of lines to be read.

like image 20
unwind Avatar answered Sep 19 '22 23:09

unwind