C - Limit the string length
You must specify the length to print or terminate the string. Otherwise, you will invoke undefined behavior. Try this, in which the latter method is implemented.
#include <stdio.h>
#define MAXCHAR 8
int main(void)
{
char password[MAXCHAR + 1]; /* allocate one more element for terminating null-character */
int i;
char c;
printf("Insert password: MAX 8 CHARS!\n\n");
for(i = 0; i <= MAXCHAR; i++){
c = getchar();
if(i == MAXCHAR){
break;
}
else{
password[i] = c;
}
}
password[MAXCHAR] = '\0'; /* terminate the string */
printf("%s\n", password);
}
Some people say that the if(i == MAXCHAR){ break; }
part doesn't look good, so here is another code example:
#include <stdio.h>
#define MAXCHAR 8
int main(void)
{
char password[MAXCHAR + 1]; /* allocate one more element for terminating null-character */
int i;
printf("Insert password: MAX 8 CHARS!\n\n");
/* read exactly 8 characters. To improve, breaking on seeing newline or EOF may be good */
for(i = 0; i < MAXCHAR; i++){
password[i] = getchar();
}
password[MAXCHAR] = '\0'; /* terminate the string */
getchar(); /* to match number of call of getchar() to the original: maybe for consuming newline character after 8-digit password */
printf("%s\n", password);
}
Related videos on Youtube
Comments
-
Claudio Pisa over 1 year
(Sorry for my bad english !)
I wrote a program that asks you to type a password no longer than a certain number, eight characters in this case. The characters that pass the limit will be cut out from the array:
#include <stdio.h> #define MAXCHAR 8 main() { char password[MAXCHAR]; int i; char c; printf("Insert password: MAX 8 CHARS!\n\n"); for(i = 0; i <= MAXCHAR; i++){ c = getchar(); if(i == MAXCHAR){ break; } else{ password[i] = c; } } printf("%s\n", password); }
So the program works BUT there is a "strange" problem. If the limit IS EIGHT and I type a password longer than eight characters (Example: P455w0rds98) the output will be like this:
P455w0rd☺
So it puts a smiley at the end and I don't know why. It happens only if a the limit is established at eight.
-
Claudio Pisa over 6 years@BruceDavidWilner Why would you say such a thing ? "...computer programming may not be for you." Damn you destroyed me, i was just a rookie when a posted this. What you said is not encouraging at all.
-
-
Sourav Ghosh over 7 years
for(i = 0; i <= MAXCHAR; i++)
..are you sure? is not it, like 9 elements? you can get rid of the wholebreak;
thingy.... -
Eugene Sh. over 7 years@SouravGhosh there is a hack inside the loop to work around it :) Update: Yeah, you have seen it. A classic example of putting a patch without understanding the root cause.
-
MikeCAT over 7 years@SouravGhosh 9 characters will be read but only 8 characters + null-character will be stored. It may be weird specification, but I don't think it is invalid.
-
Eugene Sh. over 7 years@MikeCAT And it will mislead the user, thinking his password is 9 characters.
-
MikeCAT over 7 years@EugeneSh. It may be 8 characters + newline, but password with 7 characters or less isn't allowed here... What does "MAX" mean?
-
Eugene Sh. over 7 years@MikeCAT Based on your code it's 8+newline. Anyway, this whole
if-break
thing is a nonsense. -
tofro over 7 years
fgets
ofMAXCHAR
characters actually readsMAXCHAR - 1
characters and uses the last one for the string end. It will also stop reading after theMAXCHAR-1
th character has been seen, regardless of whether it was a newline or not. So withfgets
you might run into the problem of accepting maximum 6 chars+newline or 7 chars (with a newline still remaining in the stream), which makes it even harder to hit the exact maximum character count. -
Sourav Ghosh over 7 years@tofro and why do you think that? 1)
MAXCHAR
needs to be adjusted, it's not a fixed value. 2) You have to check and remove the trailing newline, if any, then check for the string length, what's the issue there? 3)with a newline still remaining in the stream
that is not supposed to be of much concern here, is not it? -
tofro over 7 years@SouravGosh The main thing is:
fgets
will not wait for confirmation of the password by the user once he has supplied the maximum amount of characters - Which is at least irritating, but can lead to all sorts of errors. Works, but I would never use that in production code. -
Claudio Pisa over 7 yearsI got two question now: 1) Why "if(i == MAXCHAR){ break; }" doesn't look good ? 2)Ok but why that goddamn smile appears only if the limit is eight ? o.O And why does it appear always ?
-
MikeCAT over 7 years@ClaudioPisa 1) Thinking again, I don't think the code is so bad because using it you can merge two calls of
getchar()
into one. 2) By chance if you invoke undefined behavior. -
MikeCAT over 7 years@EugeneSh. Why do you think this whole
if-break
thing is a nonsense.?