Let's summarize your first attempt:
#include <stdio.h>
int main()
{
char name[2]; /* can hold 2-characters */
printf ("Enter Your Name\n");
scanf ("%s", name); /* no check of return, no field-width */
printf ("%s", name); /* UB if more than 1 char entered */
}
If more than 1 character is entered by the user, you invoke undefined behavior on your call to scanf()
overwriting your array bounds and on printf()
(depending on the implementation) as name
is not a nul-terminated string. See: Undefined, unspecified and implementation-defined behavior and What is indeterminate behavior in C++ ? How is it different from undefined behavior? and Undefined behavior
The rule is Don't Skimp on Buffer Size!, the rule of thumb is take your longest expected input and at minimum double that. For names, there are a few that can reach 32-characters, so start with a size for your name
array of 64
.
Now let's look at a minimal way to avoid problems (not a great approach, but better):
#include <stdio.h>
#define MAXNAME 64 /* if you need a constant, #define one (or more) */
int main()
{
char name[MAXNAME]; /* can hold 63-character name */
printf ("Enter Your Name\n");
if (scanf ("%63s", name) != 1) { /* validate return, use field-width */
puts ("(user canceld input)");
return 0;
}
printf ("%s\n", name); /* guaranteed no UB */
}
Above a constant is defined for the maximum name length MAXNAME
and it is used to initialize name
. If you later need to change the length, there is one simple and convenient place to do it.
You cannot use any input function correctly unless you Check The Return. Here you need to protect against the user generating a manual EOF
by pressing Ctrl + d (Ctrl + z on windows).
You cannot use scanf()
to read into an array correctly unless you provide a field-width modifier to prevent writing beyond your array bounds if the user enters more characters than your array can hold. Without a field-width modifier, scanf()
into a character array is no safer than gets()
, Why gets() is so dangerous it should never be used!. So you limit the number of characters scanf()
will read with "%63s"
(you must use an integer literal, a variable or macro will not do)
Also note, you can only read up to the first whitespace with "%s"
A better approach is to read the input with fgets()
and then use strcspn()
to trim the '\n'
at the end of the buffer filled by fgets()
. You will then be able to read names with whitespace, like "Mickey M. Mouse" and you will consume the entire line of input (up to 63 characters, including the '\n'
character generated by the user pressing Enter). With scanf()
and "%s"
the '\n'
is left in stdin
unread and if your next attempt is with a line-oriented input function like fgets()
or POSIX getline()
, the input will not read anything except the '\n'
left in stdin
.
#include <stdio.h>
#include <string.h>
#define MAXNAME 64 /* if you need a constant, #define one (or more) */
int main()
{
char name[MAXNAME]; /* can hold 63-character name */
fputs ("Enter Your Name: ", stdout);
if (!fgets (name, MAXNAME, stdin)) { /* reads up to 63 chars */
puts ("(user canceld input)");
return 0;
}
name[strcspn (name, "\n")] = 0; /* overwrite \n with \0 */
printf ("%s\n", name); /* guaranteed no UB */
}
Look things over and let me know if you have further questions.