C while loops ♾️

Поделиться
HTML-код
  • Опубликовано: 19 янв 2025

Комментарии • 21

  • @BroCodez
    @BroCodez  3 года назад +24

    #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;
    }

  • @oscare123
    @oscare123 2 года назад +79

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

    • @minhtuanle9268
      @minhtuanle9268 2 года назад +6

      very detailed and clear ,tkiu bro

    • @Aymanne-io6vb
      @Aymanne-io6vb 9 месяцев назад +2

      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.

    • @ivancasenas4336
      @ivancasenas4336 4 месяца назад

      Thank you!!

    • @ayazsenol654
      @ayazsenol654 2 месяца назад

      You are a life saver!

    • @yanisch9509
      @yanisch9509 2 месяца назад

      Thank you for helping us understand better

  • @koolboie
    @koolboie Год назад +17

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

  • @2kreptilesgaming572
    @2kreptilesgaming572 2 года назад +7

    explain this i did not get this line
    name[strlen(name) - 1] = '\0'

    • @LooseNutt420
      @LooseNutt420 3 месяца назад +1

      its easy /,,\≤≤≥≥/⁰∅ⁿ¿1?⁰1¹

    • @LooseNutt420
      @LooseNutt420 3 месяца назад

      nothin under sin of one pirmits ten to acumilate or msnifest aquiring progression

  • @marshaldteach5456
    @marshaldteach5456 10 дней назад

    great work , thankyou

  • @OnlyPlaysGg
    @OnlyPlaysGg Год назад +1

    Hi just started learning C thanks for the vedio!

  • @khalilayari9113
    @khalilayari9113 3 года назад +7

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

    • @SpicaSuzuki
      @SpicaSuzuki 3 года назад +7

      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

    • @_Anna_Nass_
      @_Anna_Nass_ 3 года назад +3

      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.

    • @prumchhangsreng979
      @prumchhangsreng979 3 года назад +10

      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.

    • @phongtranquoc7557
      @phongtranquoc7557 2 года назад +1

      @@2kreptilesgaming572 the output does change can u help me?

    • @2kreptilesgaming572
      @2kreptilesgaming572 2 года назад

      @@phongtranquoc7557 yes i was confused about that when i was learning about that.now i got it

  • @Ava-x9z3n
    @Ava-x9z3n 5 месяцев назад +1

    This is super helpfull!