#include #include int main() { // while loop = repeats a section of code possibly unlimited times. // WHILE some condition remains true // a while loop might not execute at all char name[25]; printf(" What's your name?: "); fgets(name, 25, stdin); name[strlen(name) - 1] = '\0'; while(strlen(name) == 0) { printf(" You did not enter your name"); printf(" What's your name?: "); fgets(name, 25, stdin); name[strlen(name) - 1] = '\0'; } printf("Hello %s", name); return 0; }
Many people asking about line 14. strlen(name) counts the number of characters in name. If "Bro" is input into fgets, then fgets will set name to 'B' 'r' 'o' 'newline'. That's 4 characters. name is currently 4 characters long. So strlen(name) is equivalent to the number 4. It counts the string length. 4 characters. strlen(name) equals the number 4. If you wrote strlen(name ) + strlen(name), you'd get "8". This is like algebra. x + x = y. if x = 4, then you get 4 + 4 = 8. If you wrote strlen(name) - 1, you'd get "3". Ok so our goal is to delete that last character (newline) from the string. We need some way to say "delete the last character". However that's not procedural enough for a computer to understand. So the way we do it is by saying "count how many characters there are in name (let's say 4), ok now replace the 4th character with a nothing". But there's a problem with this. When counting, computers start at the number 0. So 'B' is character 0, 'r' is character 1, 'o' is character 2, 'newline' is character 3. So we actually want to delete character 3 not character 4. There are 4 characters, but a computer counts "0, 1, 2, 3". Because of the zero, its 1 less than what you'd get if you counted starting at 1. So we need to say "make these changes to name: count like a human how many characters are in name (1, 2, 3, 4), then get that number and minus 1 (4 - 1 = 3), and delete (replace with nothingness) the character in position 3 counting like a computer 0, 1, 2, 3). name[strlen(name) - 1] = '\0';
Also the reason you need this is because clicking the "enter" button creates a new line in the terminal. To prevent this we use the code explained above. To see this in action run this code: char name[20]; printf("What is your name?"); fgets(name, 20, stdin); name[strlen(name)- 1] = '\0' ; printf("Hello %s. How are you doing?", name); now put the comment thingy (//) on ' fgets(name, 20, stdin);' and see what happens to the full stop and what comes after it. Yeah, that's why we put the null terminator. It indicates the end of the string, thus new line ( ) is replaced with (\0) which indicates the end of the string and prevents functions from reading beyond the end of the string.
well i don't understand why we used -1 but i understand something and want to share. scanf only scans input wihtout spaces like this = "SpicaSuzuki" not "Spica Suzuki" İf you enter Spica suzuki it only scans spica. and fgets can scan Spica Suzuki. "\0" means end of the line. it is something like (.) if you write char name[25] it's actually 24. Something like = char name[6] S P I C A \0. spica has 5 letters but we NEED to write 6. 6th letter is \0 1 means true 0 means false maybe" - 1" means "0" which says there is no input and this code says if there is no input then it is \0. but yea idk what -1 means
I’m kind of a noob myself, but I’ll try to explain. The brackets mean they are accessing a specific character in that array by index. Since indexing starts at 0 the index that is the same as the length for any string would be outside of the range by one. Fgets reads input until it sees a newline character (which is already at the end of each char array at it’s address in memory, but that’s another topic) and stops. However it also reads the newline character and appends it onto the end of the string. so if you enter “bro” it stores “bro ”. Then to chop that “ ” off you have to chop off the last element in the index of the char array so access that with [strlen(name)-1]. Hope that kinda made sense.
ik this has already been answered twice, but I want to explain it again to provide clarity for myself as well. Because fgets automatically add a " " to the end of user input. then we do [strlen(name)-1] to index the last element of the string, we then reset the last element of string from " " to "\0" manually. Now the user input will no longer end with a " ", but rather, it ends with nothing. example: 1. If you type: "My name is Sreng." fgets will automatically turn it into "My name is Sreng ". 2. then in our code we type: name[strlen(name)-1] = '\0'; This will turn "My name is Sreng. " to "My name is Sreng." 3. No longer print a line.
#include
#include
int main()
{
// while loop = repeats a section of code possibly unlimited times.
// WHILE some condition remains true
// a while loop might not execute at all
char name[25];
printf("
What's your name?: ");
fgets(name, 25, stdin);
name[strlen(name) - 1] = '\0';
while(strlen(name) == 0)
{
printf("
You did not enter your name");
printf("
What's your name?: ");
fgets(name, 25, stdin);
name[strlen(name) - 1] = '\0';
}
printf("Hello %s", name);
return 0;
}
Many people asking about line 14.
strlen(name) counts the number of characters in name.
If "Bro" is input into fgets, then fgets will set name to 'B' 'r' 'o' 'newline'. That's 4 characters. name is currently 4 characters long.
So strlen(name) is equivalent to the number 4. It counts the string length. 4 characters. strlen(name) equals the number 4.
If you wrote strlen(name ) + strlen(name), you'd get "8". This is like algebra. x + x = y. if x = 4, then you get 4 + 4 = 8.
If you wrote strlen(name) - 1, you'd get "3".
Ok so our goal is to delete that last character (newline) from the string. We need some way to say "delete the last character". However that's not procedural enough for a computer to understand. So the way we do it is by saying "count how many characters there are in name (let's say 4), ok now replace the 4th character with a nothing".
But there's a problem with this. When counting, computers start at the number 0. So 'B' is character 0, 'r' is character 1, 'o' is character 2, 'newline' is character 3. So we actually want to delete character 3 not character 4. There are 4 characters, but a computer counts "0, 1, 2, 3". Because of the zero, its 1 less than what you'd get if you counted starting at 1.
So we need to say "make these changes to name: count like a human how many characters are in name (1, 2, 3, 4), then get that number and minus 1 (4 - 1 = 3), and delete (replace with nothingness) the character in position 3 counting like a computer 0, 1, 2, 3).
name[strlen(name) - 1] = '\0';
very detailed and clear ,tkiu bro
Also the reason you need this is because clicking the "enter" button creates a new line in the terminal. To prevent this we use the code explained above. To see this in action run this code:
char name[20];
printf("What is your name?");
fgets(name, 20, stdin);
name[strlen(name)- 1] = '\0' ;
printf("Hello %s. How are you doing?", name);
now put the comment thingy (//) on ' fgets(name, 20, stdin);' and see what happens to the full stop and what comes after it. Yeah, that's why we put the null terminator. It indicates the end of the string, thus new line (
) is replaced with (\0) which indicates the end of the string and prevents functions from reading beyond the end of the string.
Thank you!!
You are a life saver!
Thank you for helping us understand better
Bro i dont get it ..why the hell are u so underrated !
explain this i did not get this line
name[strlen(name) - 1] = '\0'
its easy /,,\≤≤≥≥/⁰∅ⁿ¿1?⁰1¹
nothin under sin of one pirmits ten to acumilate or msnifest aquiring progression
great work , thankyou
Hi just started learning C thanks for the vedio!
can u me explain this more : name[strlen(name) - 1] = '\0';
and why we used?
and what's different between scanf and fgets ?
well i don't understand why we used -1 but i understand something and want to share.
scanf only scans input wihtout spaces like this = "SpicaSuzuki" not "Spica Suzuki" İf you enter Spica suzuki it only scans spica. and fgets can scan Spica Suzuki.
"\0" means end of the line. it is something like (.) if you write char name[25] it's actually 24. Something like = char name[6] S P I C A \0. spica has 5 letters but we NEED to write 6. 6th letter is \0
1 means true 0 means false maybe" - 1" means "0" which says there is no input and this code says if there is no input then it is \0. but yea idk what -1 means
I’m kind of a noob myself, but I’ll try to explain. The brackets mean they are accessing a specific character in that array by index. Since indexing starts at 0 the index that is the same as the length for any string would be outside of the range by one.
Fgets reads input until it sees a newline character (which is already at the end of each char array at it’s address in memory, but that’s another topic) and stops. However it also reads the newline character and appends it onto the end of the string. so if you enter “bro” it stores “bro
”. Then to chop that “
” off you have to chop off the last element in the index of the char array so access that with [strlen(name)-1].
Hope that kinda made sense.
ik this has already been answered twice, but I want to explain it again to provide clarity for myself as well.
Because fgets automatically add a "
" to the end of user input.
then we do [strlen(name)-1] to index the last element of the string, we then reset the last element of string from "
" to "\0" manually. Now the user input will no longer end with a "
", but rather, it ends with nothing.
example:
1. If you type:
"My name is Sreng."
fgets will automatically turn it into "My name is Sreng
".
2. then in our code we type:
name[strlen(name)-1] = '\0';
This will turn "My name is Sreng.
" to "My name is Sreng."
3. No longer print a line.
@@2kreptilesgaming572 the output does change can u help me?
@@phongtranquoc7557 yes i was confused about that when i was learning about that.now i got it
This is super helpfull!