fgets is unsafe! So what’s the alternative?

Поділитися
Вставка
  • Опубліковано 2 лис 2024

КОМЕНТАРІ • 21

  • @mrkewi1
    @mrkewi1 21 день тому +1

    wow! never before came across a C tutorial series like this one. please make more videos in the series.

    • @LearnWithHuw
      @LearnWithHuw  21 день тому

      Thank you. I currently have *two* series on UA-cam that are mainly about C and low-level programming. I am preparing some more at the moment too. Here are the two series online at the moment:
      ua-cam.com/play/PLZHx5heVfgEt8Ynevr3OX9UBKLPXdSMGM.html
      ua-cam.com/play/PLZHx5heVfgEvL826uk3DeeUy1QiP7nrIs.html
      Best wishes
      Huw

  • @johnc3403
    @johnc3403 Рік тому +1

    The Little Book of C Programming is on my Christmas list. A perfect companion to last years Christmas present, K&R 2ed.

    • @LearnWithHuw
      @LearnWithHuw  Рік тому +2

      I am in illustrious company! Thank you!

  • @BitwiseMobile
    @BitwiseMobile 2 роки тому +1

    I think the C++ standard library now includes buffer overflow safe alternatives to all our buffer overflow friends. I wrote C++ for 10 years, and then while I was working for a Microsoft subsidiary we beta tested C#. I'll never go back to the hell that is virtual destructors, new, delete, and everything associated with a non-garbage collected language. C/C++ is great when you need to get down to the metal (and I would argue on other platforms, but dotnet has been ported to pretty much everywhere now, so that argument no longer holds), but it comes with it a plethora of issues. With great power comes great responsibility (for freeing your memory, and ensuring your buffers are of the proper size).

  • @ironfist7789
    @ironfist7789 Рік тому +1

    Problem is, while windows is
    for new line, mac was
    for carriage return and some used
    for a newline to satisfy both (from what I recall doing this in the past). In programming muds in the 90s, a lot of codebases used
    in their strings.

  • @radosmirkovic8371
    @radosmirkovic8371 3 місяці тому

    This is great. I finally understand why c is unsafe language. C should be taught in this way, not in a way like JavaScript is taught. Btw I am not rust fanboy.

    • @LearnWithHuw
      @LearnWithHuw  3 місяці тому

      Ha! Yes, C is a very unsafe language. Very powerful for certain applications. But it won't do much to protect you from making potentially catastrophic mistakes.
      Thanks for the comment.
      Huw

  • @SATTWIK.1
    @SATTWIK.1 5 місяців тому

    we just need to get rid off the new line
    example:
    char name[25];
    printf("Enter your name:
    ");
    fgets(name,25,stdin);
    name[strlen(name)-1]='\0';
    the last line removes the new line character. we can use this

  • @EmmanuelGoldstein-h7d
    @EmmanuelGoldstein-h7d 3 місяці тому

    sizeof(str) / sizeof(*str)

  • @abdelhaylawhy631
    @abdelhaylawhy631 Рік тому

    First, I wanna thank you for this HERO function, and thanks for this amazing course. but why don't you add it in c language as Standard library functions, that will be more helpful.

    • @LearnWithHuw
      @LearnWithHuw  Рік тому +1

      I think the C standard library is very cautious about change. But anyway, as you can see, it's not really to hard for us to write functions that work the way we'd like them to. Many thanks for your comment.

  • @abdelhaylawhy631
    @abdelhaylawhy631 Рік тому

    I have a question: for reading numbers like int, floats, long, ... is scanf() safe? or should we think about another ways? and thanks 👍

    • @LearnWithHuw
      @LearnWithHuw  Рік тому +2

      Personally I would never use scanf for anything. It is inherently unsafe. You might want to browse through the videos in my playlist which go into the problems in more detail: ua-cam.com/play/PLZHx5heVfgEvL826uk3DeeUy1QiP7nrIs.html

    • @abdelhaylawhy631
      @abdelhaylawhy631 Рік тому

      @@LearnWithHuw I have watched all videos in the playlist, but in the last video you have explained the safer way to read "strings" with your "readln" function, but what about number types like: int, float, double... I hope you explain that in your next video.

    • @LearnWithHuw
      @LearnWithHuw  Рік тому +2

      @@abdelhaylawhy631 You would need to "convert" those to the appropriate data types in your own code. Remember, you can only ever read strings from the system prompt (series of characters) and it is then up to you how to interpret those strings if they represent numbers. scanf does conversion automatically but is unsafe so you would do better to do the conversions in your own code.

    • @abdelhaylawhy631
      @abdelhaylawhy631 Рік тому

      hmm, that's good thanks. For the conversion I heard about functions like: atoi, strtol,... so because you make me never trust any other function, I would like to ask you if these functions are enough and safe? And more than that, how can I use them with your "readln" hero function?!!!

  • @BryanChance
    @BryanChance Рік тому +1

    Yep, why don’t people just write a custom input function? Read one character at a time and limit it. c’s input routines are probably what gave C a bad name. LOL

  • @Sufian95
    @Sufian95 9 місяців тому +1

    Will a fflush(stdin) workout?

    • @GaryChike
      @GaryChike 9 місяців тому

      It works here .. but 'fflush(stdin)' invokes undefined behavior., fflush(stdout) does not.