How to scanf if there could be a char or int in the same input position












-1















For example, if I have an input that looks like this : 10 3 128, but also have a second input that looks like this: 16 2 F



How can i scan these values?
I've tried reading it as a string and then trying to convert it to an int or leaving it as a char if needed.



fscanf(in, "%d %d %c or %d, from, to, numorhex);


My program is basically a numeral system converter, and i'd like to know how to scan hex characters as well as ints.










share|improve this question


















  • 1





    You want to read base 16 numbers? Did you read the documentation?

    – Shawn
    Nov 28 '18 at 16:20
















-1















For example, if I have an input that looks like this : 10 3 128, but also have a second input that looks like this: 16 2 F



How can i scan these values?
I've tried reading it as a string and then trying to convert it to an int or leaving it as a char if needed.



fscanf(in, "%d %d %c or %d, from, to, numorhex);


My program is basically a numeral system converter, and i'd like to know how to scan hex characters as well as ints.










share|improve this question


















  • 1





    You want to read base 16 numbers? Did you read the documentation?

    – Shawn
    Nov 28 '18 at 16:20














-1












-1








-1








For example, if I have an input that looks like this : 10 3 128, but also have a second input that looks like this: 16 2 F



How can i scan these values?
I've tried reading it as a string and then trying to convert it to an int or leaving it as a char if needed.



fscanf(in, "%d %d %c or %d, from, to, numorhex);


My program is basically a numeral system converter, and i'd like to know how to scan hex characters as well as ints.










share|improve this question














For example, if I have an input that looks like this : 10 3 128, but also have a second input that looks like this: 16 2 F



How can i scan these values?
I've tried reading it as a string and then trying to convert it to an int or leaving it as a char if needed.



fscanf(in, "%d %d %c or %d, from, to, numorhex);


My program is basically a numeral system converter, and i'd like to know how to scan hex characters as well as ints.







c char int scanf






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 16:15









Stan MarshStan Marsh

216




216








  • 1





    You want to read base 16 numbers? Did you read the documentation?

    – Shawn
    Nov 28 '18 at 16:20














  • 1





    You want to read base 16 numbers? Did you read the documentation?

    – Shawn
    Nov 28 '18 at 16:20








1




1





You want to read base 16 numbers? Did you read the documentation?

– Shawn
Nov 28 '18 at 16:20





You want to read base 16 numbers? Did you read the documentation?

– Shawn
Nov 28 '18 at 16:20












2 Answers
2






active

oldest

votes


















0














Firstly, you can store everything you scanned in int and also as char.
When you want to print it after scanning. You can do it from both types.






share|improve this answer































    0














    You can do something like that :



    int main(void)
    {
    char *line = {"10 3 128", "16 2 F", NULL};
    int nb1;
    int nb2;
    int nb3;
    char letter;
    char end;


    for (size_t i = 0; line[i]; ++i) {

    if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
    printf("number number number : %d %d %dn", nb1, nb2, nb3);
    } else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
    printf("number number letter : %d %d %cn", nb1, nb2, letter);
    } else {
    printf("format line not supported : %sn", line[i]);
    }
    }

    return (0);
    }


    Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53523783%2fhow-to-scanf-if-there-could-be-a-char-or-int-in-the-same-input-position%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Firstly, you can store everything you scanned in int and also as char.
      When you want to print it after scanning. You can do it from both types.






      share|improve this answer




























        0














        Firstly, you can store everything you scanned in int and also as char.
        When you want to print it after scanning. You can do it from both types.






        share|improve this answer


























          0












          0








          0







          Firstly, you can store everything you scanned in int and also as char.
          When you want to print it after scanning. You can do it from both types.






          share|improve this answer













          Firstly, you can store everything you scanned in int and also as char.
          When you want to print it after scanning. You can do it from both types.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 '18 at 16:27









          mato0mato0

          33




          33

























              0














              You can do something like that :



              int main(void)
              {
              char *line = {"10 3 128", "16 2 F", NULL};
              int nb1;
              int nb2;
              int nb3;
              char letter;
              char end;


              for (size_t i = 0; line[i]; ++i) {

              if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
              printf("number number number : %d %d %dn", nb1, nb2, nb3);
              } else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
              printf("number number letter : %d %d %cn", nb1, nb2, letter);
              } else {
              printf("format line not supported : %sn", line[i]);
              }
              }

              return (0);
              }


              Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.






              share|improve this answer




























                0














                You can do something like that :



                int main(void)
                {
                char *line = {"10 3 128", "16 2 F", NULL};
                int nb1;
                int nb2;
                int nb3;
                char letter;
                char end;


                for (size_t i = 0; line[i]; ++i) {

                if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
                printf("number number number : %d %d %dn", nb1, nb2, nb3);
                } else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
                printf("number number letter : %d %d %cn", nb1, nb2, letter);
                } else {
                printf("format line not supported : %sn", line[i]);
                }
                }

                return (0);
                }


                Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.






                share|improve this answer


























                  0












                  0








                  0







                  You can do something like that :



                  int main(void)
                  {
                  char *line = {"10 3 128", "16 2 F", NULL};
                  int nb1;
                  int nb2;
                  int nb3;
                  char letter;
                  char end;


                  for (size_t i = 0; line[i]; ++i) {

                  if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
                  printf("number number number : %d %d %dn", nb1, nb2, nb3);
                  } else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
                  printf("number number letter : %d %d %cn", nb1, nb2, letter);
                  } else {
                  printf("format line not supported : %sn", line[i]);
                  }
                  }

                  return (0);
                  }


                  Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.






                  share|improve this answer













                  You can do something like that :



                  int main(void)
                  {
                  char *line = {"10 3 128", "16 2 F", NULL};
                  int nb1;
                  int nb2;
                  int nb3;
                  char letter;
                  char end;


                  for (size_t i = 0; line[i]; ++i) {

                  if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
                  printf("number number number : %d %d %dn", nb1, nb2, nb3);
                  } else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
                  printf("number number letter : %d %d %cn", nb1, nb2, letter);
                  } else {
                  printf("format line not supported : %sn", line[i]);
                  }
                  }

                  return (0);
                  }


                  Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 28 '18 at 16:43









                  Tom'sTom's

                  2,042420




                  2,042420






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53523783%2fhow-to-scanf-if-there-could-be-a-char-or-int-in-the-same-input-position%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Lallio

                      Futebolista

                      Jornalista