C while loops ♾️

preview_player
Показать описание
C while loops tutorial example explained

#C #while #loop

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("\nWhat's your name?: ");
fgets(name, 25, stdin);
name[strlen(name) - 1] = '\0';

while(strlen(name) == 0)
{
printf("\nYou did not enter your name");
printf("\nWhat's your name?: ");
fgets(name, 25, stdin);
name[strlen(name) - 1] = '\0';
}

printf("Hello %s", name);

return 0;
}
Рекомендации по теме
Комментарии
Автор

#include <stdio.h>
#include <string.h>

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("\nWhat's your name?: ");
fgets(name, 25, stdin);
name[strlen(name) - 1] = '\0';

while(strlen(name) == 0)
{
printf("\nYou did not enter your name");
printf("\nWhat's your name?: ");
fgets(name, 25, stdin);
name[strlen(name) - 1] = '\0';
}

printf("Hello %s", name);

return 0;
}

BroCodez
Автор

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';

oscare
Автор

Bro i dont get it ..why the hell are u so underrated !

koolboie
Автор

explain this i did not get this line

name[strlen(name) - 1] = '\0'

kreptilesgaming
Автор

Hi just started learning C thanks for the vedio!

OnlyPlaysGg
Автор

can u me explain this more : name[strlen(name) - 1] = '\0';
and why we used?
and what's different between scanf and fgets ?

khalilayari