How To Clear The Input Buffer | C Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 16 янв 2023
  • How to clear the standard input buffer in C, as well as why we might need to clear/flush the input buffer. It might seem like fgets(), scanf() and similar function calls are being "skipped" if the input buffer is not cleared, this video explains this issue and how to solve it. Source code: github.com/portfoliocourses/c... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

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

  • @darshjobanputra2009
    @darshjobanputra2009 Год назад +27

    It feels that you're soon going to be the only person known to cover A-Z, of C programming on RUclips... All the other channels miss one topic or the other!! KEEP MAKING SUCH MORE VIDEOS!! 🔥🔥

    • @PortfolioCourses
      @PortfolioCourses  Год назад +5

      Thank you for the enthusiastic feedback Darsh, I really appreciate it! :-) There are many, many more C videos I wish to make. Though in the long term, there are many other topics I wish to cover too.

  • @Raied114
    @Raied114 Год назад +10

    Your videos are getting me through my uni C course. You're a legend! Keep up the videos!

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

      Thank you for sharing this positive feedback Raed. And I'm so glad to hear the videos are helping you with your uni C course... helping students is the biggest reason why I love making these videos! :-)

  • @proggenius2024
    @proggenius2024 2 месяца назад +1

    thank you so much. I am always confused whenever I see fflush and now I understand even more.

  • @arthur_p_dent4282
    @arthur_p_dent4282 Год назад +4

    Just had to solve this one the other day. You are a treasure trove of useful c tips and tricks.

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

    You're a legend. My go to for C programming on youtube.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Thank you for the kind feedback Jacob, I'm really glad these videos are your go to for C programming on RUclips! :-D

  • @fifaham
    @fifaham Год назад +2

    Very informative and to the point.

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

    Thanks for referring me to this video. Much appreciated

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

    you helped me so much and kept my brain braining, thank you

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

    Thank you, That was my question 😊

  • @hasdrubal9799
    @hasdrubal9799 Год назад

    Tanks for another advance topic 😉

  • @mohammadahmedragab837
    @mohammadahmedragab837 Год назад

    your videos are super clear easy and amazing, please make a video about cybersecurity 💚💙💙💙

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

      Thank you for the positive feedback! Hopefully one day I can cover cybersecurity, though it's a very big topic. :-)

  • @nub8312
    @nub8312 Год назад +3

    I am studying computer science in Italy as an Italian and it makes me very sad when I come across situations like this. We are taught about the "input buffer", but our teachers simply tell us to use "fflush" without providing any explanation on how we can create our own version of this function. This lack of guidance and support is disheartening

    • @PortfolioCourses
      @PortfolioCourses  Год назад +3

      Sorry to hear that. :-( Often times in programming we're left learning things ourselves at the end of the day, so it's good you're seeking out other sources to learn from.

    • @nub8312
      @nub8312 Год назад +2

      yea, you are right :)

  • @marcelijankowski9593
    @marcelijankowski9593 7 месяцев назад

    Thanks man!

  • @safeyamohamed3854
    @safeyamohamed3854 Год назад

    thank you so much

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

    Thank you. I always use while( getchar() != '
    ' ); to clear input buffer, but never include c != EOF. It is good to know.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You're welcome Eden, I'm glad you learned something new! :-)

  • @naboulsikhalid7763
    @naboulsikhalid7763 Год назад

    another great video that I added to my portfolio of solutions. if there Bigger than Thank you. Tell me what is it, and I will be glade to to send it back to you . Thanks a lot for helping us

  • @user-hj1bl8yb3f
    @user-hj1bl8yb3f Год назад

    Really nice, now i had tottaly understanded how its working
    Find your video while searching of alternative fflush(), Thanks!

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You're welcome, I'm glad the video was able to help you out! :-)

  • @trevorjamesconsideracion1982
    @trevorjamesconsideracion1982 Год назад

    I've just been thinking about this xd

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Haha that's a funny coincidence! :-) I had a few people running into this problem in the comments of other videos so I decided to make this video.

  • @glimpse8144
    @glimpse8144 Год назад

    You can also use fseek() to clear input buffer.
    fseek(stdin, 0, SEEK_END);

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Thanks so much for sharing this! I sort of prefer this method that you've shared, personally, because it's simpler. But just so people reading are aware, technically it's not 100% portable across C compilers. i.e. it's possible this statement will not work across all C compilers. See here: stackoverflow.com/a/16673023. That said, this method, as well as using fflush(stdin) are very widely supported in practice. :-)

    • @CarelessWinner
      @CarelessWinner 8 месяцев назад

      Thank you. This worked for me in Visual Studio.
      "fflush(stdin);" seems to do nothing.
      "while ((c = getchar()) != '
      ') {}" creates and infinite loop requesting input.

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

    Good

  • @yoruichi5802
    @yoruichi5802 Год назад

    Nice

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

    Of course you can build a function that you can use repeatedly to flush the stdin.

  • @Rai_Te
    @Rai_Te 5 месяцев назад

    Your program is an example for several things.
    #1 The C stdlib is not designed well ... this is a well known, and often discussed fact.
    #2 mixing fgets() with scanf() is usually a bad idea.
    better practise is to use fgets() to read a line from stdin, then
    parse this line using sscanf()
    #3 Do not forget to check returncodes from functions.

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

    why not just:
    while(getchar() != '
    ');

  • @the_nuckelavee
    @the_nuckelavee 11 месяцев назад

    thank you.
    when i was 18y.o. i had left the institute after they had given us Turbo Pascal 5.0 instead of C.
    now i am 43y.o. and i was doing K&R exersises and my code was behaving badly after i’d chained several inputs.
    p.s. oh by the way -about C -while local wikipedians rant that the language is obsolete nowadays, i wanna to share some specifics (weirdness) of physical being’s implementation -i do not feel pain (anandamide is not destroyed)
    m.ruclips.net/video/4nGFNayjYrM/видео.html
    thank you