Issue in scanning the user input like: scanf("%[^\n]", name);

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Issue in scanning the user input like: scanf("%[^\n]", name);

      I know this is an unsecure way of using scanf. When I use this in any other web/local IDE, program works.

      I'm using Segger Embedded Studio for RISCV V 5.60a 64 bit non commercial license for non embedded learning purpose

      C Source Code

      1. #include <stdio.h>
      2. #include <math.h>
      3. int main() {
      4. char name[50];
      5. printf("Enter any string: \n");
      6. scanf("%[^\n]", name);
      7. int i;
      8. for (i = 0; i <= 50; i++) {
      9. if (name[i] == '\0')
      10. break;
      11. }
      12. printf("\nThe length of the string is: %d.", i);
      13. return 0;
      14. }
      Display All
    • Hello,

      Thank you for your inquiry.
      Maybe I missed it, but what exactly is not working?
      Which I/O interface are you using for communication with the target (see project option Library I/O)?

      Best regards,
      Nino
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Hello Nino,

      Thank you for your reply! In line 7, I use the pattern %[^\n] for getting input for char array till a new line char using scanf. I'm not using/connecting any specialized/embedded device/target or interface. Just running the IDE on my computer and inputting through console provided in run and debug window.
    • Hello,

      but you are simulating an embedded target device. Maybe I need to ask differently.
      What is the expected behaviour? What is not working?

      Best regards,
      Nino
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Hello,

      We gave your example a try.
      For character class matching to be supported you need to enable project option Printf/Scanf->Scanf Classes Supported->Yes.

      Can you confirm that scanf then works for you as well?

      Best regards,
      Nino
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.