printf long changes variable when it's digits is more than 9 chars












1















I have this simple code in C, whenever I enter an id number that longer than 9 digits, it prints a different number. What's the wrong?



void main() {
long id1;
double l1, w1;
printf("enter studint 1 id,lenght and weight: n");
scanf("%ld %lf %lf", &id1, &l1, &w1);

printf("STUDINTS INFORMATION:");
printf("nn%-10.0ldt %-4.3lft %-4.3lfn", id1, l1, w1);
}









share|improve this question

























  • start by checking the return value of scanf().

    – Sourav Ghosh
    Nov 25 '18 at 4:58






  • 1





    "I enter an id number that longer than 9 digits" --> What was the number entered? What was printed?

    – chux
    Nov 25 '18 at 5:00






  • 3





    A long range may only be [-2,147,483,647 ... 2,147,483,647]. A number outside that range is asking for trouble. Use a wider integer type.

    – chux
    Nov 25 '18 at 5:03













  • %-10.0ld for id1 seems incorrect: if id1 == 0 the output will be just 10 spaces.

    – chqrlie
    Nov 25 '18 at 9:10
















1















I have this simple code in C, whenever I enter an id number that longer than 9 digits, it prints a different number. What's the wrong?



void main() {
long id1;
double l1, w1;
printf("enter studint 1 id,lenght and weight: n");
scanf("%ld %lf %lf", &id1, &l1, &w1);

printf("STUDINTS INFORMATION:");
printf("nn%-10.0ldt %-4.3lft %-4.3lfn", id1, l1, w1);
}









share|improve this question

























  • start by checking the return value of scanf().

    – Sourav Ghosh
    Nov 25 '18 at 4:58






  • 1





    "I enter an id number that longer than 9 digits" --> What was the number entered? What was printed?

    – chux
    Nov 25 '18 at 5:00






  • 3





    A long range may only be [-2,147,483,647 ... 2,147,483,647]. A number outside that range is asking for trouble. Use a wider integer type.

    – chux
    Nov 25 '18 at 5:03













  • %-10.0ld for id1 seems incorrect: if id1 == 0 the output will be just 10 spaces.

    – chqrlie
    Nov 25 '18 at 9:10














1












1








1








I have this simple code in C, whenever I enter an id number that longer than 9 digits, it prints a different number. What's the wrong?



void main() {
long id1;
double l1, w1;
printf("enter studint 1 id,lenght and weight: n");
scanf("%ld %lf %lf", &id1, &l1, &w1);

printf("STUDINTS INFORMATION:");
printf("nn%-10.0ldt %-4.3lft %-4.3lfn", id1, l1, w1);
}









share|improve this question
















I have this simple code in C, whenever I enter an id number that longer than 9 digits, it prints a different number. What's the wrong?



void main() {
long id1;
double l1, w1;
printf("enter studint 1 id,lenght and weight: n");
scanf("%ld %lf %lf", &id1, &l1, &w1);

printf("STUDINTS INFORMATION:");
printf("nn%-10.0ldt %-4.3lft %-4.3lfn", id1, l1, w1);
}






c printf scanf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 9:04









chqrlie

60.2k747102




60.2k747102










asked Nov 25 '18 at 4:57









Majd SadiMajd Sadi

155




155













  • start by checking the return value of scanf().

    – Sourav Ghosh
    Nov 25 '18 at 4:58






  • 1





    "I enter an id number that longer than 9 digits" --> What was the number entered? What was printed?

    – chux
    Nov 25 '18 at 5:00






  • 3





    A long range may only be [-2,147,483,647 ... 2,147,483,647]. A number outside that range is asking for trouble. Use a wider integer type.

    – chux
    Nov 25 '18 at 5:03













  • %-10.0ld for id1 seems incorrect: if id1 == 0 the output will be just 10 spaces.

    – chqrlie
    Nov 25 '18 at 9:10



















  • start by checking the return value of scanf().

    – Sourav Ghosh
    Nov 25 '18 at 4:58






  • 1





    "I enter an id number that longer than 9 digits" --> What was the number entered? What was printed?

    – chux
    Nov 25 '18 at 5:00






  • 3





    A long range may only be [-2,147,483,647 ... 2,147,483,647]. A number outside that range is asking for trouble. Use a wider integer type.

    – chux
    Nov 25 '18 at 5:03













  • %-10.0ld for id1 seems incorrect: if id1 == 0 the output will be just 10 spaces.

    – chqrlie
    Nov 25 '18 at 9:10

















start by checking the return value of scanf().

– Sourav Ghosh
Nov 25 '18 at 4:58





start by checking the return value of scanf().

– Sourav Ghosh
Nov 25 '18 at 4:58




1




1





"I enter an id number that longer than 9 digits" --> What was the number entered? What was printed?

– chux
Nov 25 '18 at 5:00





"I enter an id number that longer than 9 digits" --> What was the number entered? What was printed?

– chux
Nov 25 '18 at 5:00




3




3





A long range may only be [-2,147,483,647 ... 2,147,483,647]. A number outside that range is asking for trouble. Use a wider integer type.

– chux
Nov 25 '18 at 5:03







A long range may only be [-2,147,483,647 ... 2,147,483,647]. A number outside that range is asking for trouble. Use a wider integer type.

– chux
Nov 25 '18 at 5:03















%-10.0ld for id1 seems incorrect: if id1 == 0 the output will be just 10 spaces.

– chqrlie
Nov 25 '18 at 9:10





%-10.0ld for id1 seems incorrect: if id1 == 0 the output will be just 10 spaces.

– chqrlie
Nov 25 '18 at 9:10












3 Answers
3






active

oldest

votes


















1














You use type long for id1 which may have range of just -2147483647 to 2147483647 depending on the target system architecture. This type cannot be used to store larger numbers on your system.



Incidentally, the format %-10.0ld for id1 seems incorrect as the output for the value 0 will be just 10 spaces. The precision field does not specify the number of decimals for integers, but the minimum number of digits.



Note also that main without arguments should have a prototype of int main(void), and you should test the return value of scanf() to avoid undefined behavior on invalid input.



To solve your problem, here are possible solutions:




  • You could use type double but I would not advise so.


  • You could try type long long int, but this type may not be fully supported on your system.


  • I suggest you use a character string for your purpose.



Here is a modified version:



#include <stdio.h>

int main(void) {
char id1[20];
double l1, w1;

printf("enter student 1 id, height and weight:n");
if (scanf("%19s%lf%lf", id1, &l1, &w1) != 3) {
printf("invalid inputn");
return 1;
}
printf("STUDENT INFORMATION:n");
printf("n%-10st %-4.3ft %-4.3fn", id1, l1, w1);
return 0;
}





share|improve this answer































    0














    Usually, IDs are better stored as strings. Not only is it more flexible if the ID someday contains letters, but it avoids problems like this. IDs are not arithmetic, i.e. you cannot typically get anything out of adding or subtracting them, so you may as well just store them as strings. Then you can easily support any length, regardless of the width of various integer types on your system.






    share|improve this answer































      0














      That's because range of long in C is from -2,147,483,648 to 2,147,483,647.
      So, when you try to save a number longer than 9 digits it gets out of range.



      For more details on the range of variables in C: https://www.tutorialspoint.com/cprogramming/c_data_types.htm






      share|improve this answer
























      • This tutorial is incorrect. The range of type long is at least -2147483647 to 2147483647 but it may be much larger, as is the case on 64-bit linux systems. Your correctly diagnosed the OP's problem, but the link is misleading.

        – chqrlie
        Nov 25 '18 at 9:10











      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%2f53464771%2fprintf-long-changes-variable-when-its-digits-is-more-than-9-chars%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You use type long for id1 which may have range of just -2147483647 to 2147483647 depending on the target system architecture. This type cannot be used to store larger numbers on your system.



      Incidentally, the format %-10.0ld for id1 seems incorrect as the output for the value 0 will be just 10 spaces. The precision field does not specify the number of decimals for integers, but the minimum number of digits.



      Note also that main without arguments should have a prototype of int main(void), and you should test the return value of scanf() to avoid undefined behavior on invalid input.



      To solve your problem, here are possible solutions:




      • You could use type double but I would not advise so.


      • You could try type long long int, but this type may not be fully supported on your system.


      • I suggest you use a character string for your purpose.



      Here is a modified version:



      #include <stdio.h>

      int main(void) {
      char id1[20];
      double l1, w1;

      printf("enter student 1 id, height and weight:n");
      if (scanf("%19s%lf%lf", id1, &l1, &w1) != 3) {
      printf("invalid inputn");
      return 1;
      }
      printf("STUDENT INFORMATION:n");
      printf("n%-10st %-4.3ft %-4.3fn", id1, l1, w1);
      return 0;
      }





      share|improve this answer




























        1














        You use type long for id1 which may have range of just -2147483647 to 2147483647 depending on the target system architecture. This type cannot be used to store larger numbers on your system.



        Incidentally, the format %-10.0ld for id1 seems incorrect as the output for the value 0 will be just 10 spaces. The precision field does not specify the number of decimals for integers, but the minimum number of digits.



        Note also that main without arguments should have a prototype of int main(void), and you should test the return value of scanf() to avoid undefined behavior on invalid input.



        To solve your problem, here are possible solutions:




        • You could use type double but I would not advise so.


        • You could try type long long int, but this type may not be fully supported on your system.


        • I suggest you use a character string for your purpose.



        Here is a modified version:



        #include <stdio.h>

        int main(void) {
        char id1[20];
        double l1, w1;

        printf("enter student 1 id, height and weight:n");
        if (scanf("%19s%lf%lf", id1, &l1, &w1) != 3) {
        printf("invalid inputn");
        return 1;
        }
        printf("STUDENT INFORMATION:n");
        printf("n%-10st %-4.3ft %-4.3fn", id1, l1, w1);
        return 0;
        }





        share|improve this answer


























          1












          1








          1







          You use type long for id1 which may have range of just -2147483647 to 2147483647 depending on the target system architecture. This type cannot be used to store larger numbers on your system.



          Incidentally, the format %-10.0ld for id1 seems incorrect as the output for the value 0 will be just 10 spaces. The precision field does not specify the number of decimals for integers, but the minimum number of digits.



          Note also that main without arguments should have a prototype of int main(void), and you should test the return value of scanf() to avoid undefined behavior on invalid input.



          To solve your problem, here are possible solutions:




          • You could use type double but I would not advise so.


          • You could try type long long int, but this type may not be fully supported on your system.


          • I suggest you use a character string for your purpose.



          Here is a modified version:



          #include <stdio.h>

          int main(void) {
          char id1[20];
          double l1, w1;

          printf("enter student 1 id, height and weight:n");
          if (scanf("%19s%lf%lf", id1, &l1, &w1) != 3) {
          printf("invalid inputn");
          return 1;
          }
          printf("STUDENT INFORMATION:n");
          printf("n%-10st %-4.3ft %-4.3fn", id1, l1, w1);
          return 0;
          }





          share|improve this answer













          You use type long for id1 which may have range of just -2147483647 to 2147483647 depending on the target system architecture. This type cannot be used to store larger numbers on your system.



          Incidentally, the format %-10.0ld for id1 seems incorrect as the output for the value 0 will be just 10 spaces. The precision field does not specify the number of decimals for integers, but the minimum number of digits.



          Note also that main without arguments should have a prototype of int main(void), and you should test the return value of scanf() to avoid undefined behavior on invalid input.



          To solve your problem, here are possible solutions:




          • You could use type double but I would not advise so.


          • You could try type long long int, but this type may not be fully supported on your system.


          • I suggest you use a character string for your purpose.



          Here is a modified version:



          #include <stdio.h>

          int main(void) {
          char id1[20];
          double l1, w1;

          printf("enter student 1 id, height and weight:n");
          if (scanf("%19s%lf%lf", id1, &l1, &w1) != 3) {
          printf("invalid inputn");
          return 1;
          }
          printf("STUDENT INFORMATION:n");
          printf("n%-10st %-4.3ft %-4.3fn", id1, l1, w1);
          return 0;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 9:19









          chqrliechqrlie

          60.2k747102




          60.2k747102

























              0














              Usually, IDs are better stored as strings. Not only is it more flexible if the ID someday contains letters, but it avoids problems like this. IDs are not arithmetic, i.e. you cannot typically get anything out of adding or subtracting them, so you may as well just store them as strings. Then you can easily support any length, regardless of the width of various integer types on your system.






              share|improve this answer




























                0














                Usually, IDs are better stored as strings. Not only is it more flexible if the ID someday contains letters, but it avoids problems like this. IDs are not arithmetic, i.e. you cannot typically get anything out of adding or subtracting them, so you may as well just store them as strings. Then you can easily support any length, regardless of the width of various integer types on your system.






                share|improve this answer


























                  0












                  0








                  0







                  Usually, IDs are better stored as strings. Not only is it more flexible if the ID someday contains letters, but it avoids problems like this. IDs are not arithmetic, i.e. you cannot typically get anything out of adding or subtracting them, so you may as well just store them as strings. Then you can easily support any length, regardless of the width of various integer types on your system.






                  share|improve this answer













                  Usually, IDs are better stored as strings. Not only is it more flexible if the ID someday contains letters, but it avoids problems like this. IDs are not arithmetic, i.e. you cannot typically get anything out of adding or subtracting them, so you may as well just store them as strings. Then you can easily support any length, regardless of the width of various integer types on your system.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 5:13









                  John ZwinckJohn Zwinck

                  153k17177294




                  153k17177294























                      0














                      That's because range of long in C is from -2,147,483,648 to 2,147,483,647.
                      So, when you try to save a number longer than 9 digits it gets out of range.



                      For more details on the range of variables in C: https://www.tutorialspoint.com/cprogramming/c_data_types.htm






                      share|improve this answer
























                      • This tutorial is incorrect. The range of type long is at least -2147483647 to 2147483647 but it may be much larger, as is the case on 64-bit linux systems. Your correctly diagnosed the OP's problem, but the link is misleading.

                        – chqrlie
                        Nov 25 '18 at 9:10
















                      0














                      That's because range of long in C is from -2,147,483,648 to 2,147,483,647.
                      So, when you try to save a number longer than 9 digits it gets out of range.



                      For more details on the range of variables in C: https://www.tutorialspoint.com/cprogramming/c_data_types.htm






                      share|improve this answer
























                      • This tutorial is incorrect. The range of type long is at least -2147483647 to 2147483647 but it may be much larger, as is the case on 64-bit linux systems. Your correctly diagnosed the OP's problem, but the link is misleading.

                        – chqrlie
                        Nov 25 '18 at 9:10














                      0












                      0








                      0







                      That's because range of long in C is from -2,147,483,648 to 2,147,483,647.
                      So, when you try to save a number longer than 9 digits it gets out of range.



                      For more details on the range of variables in C: https://www.tutorialspoint.com/cprogramming/c_data_types.htm






                      share|improve this answer













                      That's because range of long in C is from -2,147,483,648 to 2,147,483,647.
                      So, when you try to save a number longer than 9 digits it gets out of range.



                      For more details on the range of variables in C: https://www.tutorialspoint.com/cprogramming/c_data_types.htm







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 25 '18 at 5:19









                      Mangaldeep PannuMangaldeep Pannu

                      8217




                      8217













                      • This tutorial is incorrect. The range of type long is at least -2147483647 to 2147483647 but it may be much larger, as is the case on 64-bit linux systems. Your correctly diagnosed the OP's problem, but the link is misleading.

                        – chqrlie
                        Nov 25 '18 at 9:10



















                      • This tutorial is incorrect. The range of type long is at least -2147483647 to 2147483647 but it may be much larger, as is the case on 64-bit linux systems. Your correctly diagnosed the OP's problem, but the link is misleading.

                        – chqrlie
                        Nov 25 '18 at 9:10

















                      This tutorial is incorrect. The range of type long is at least -2147483647 to 2147483647 but it may be much larger, as is the case on 64-bit linux systems. Your correctly diagnosed the OP's problem, but the link is misleading.

                      – chqrlie
                      Nov 25 '18 at 9:10





                      This tutorial is incorrect. The range of type long is at least -2147483647 to 2147483647 but it may be much larger, as is the case on 64-bit linux systems. Your correctly diagnosed the OP's problem, but the link is misleading.

                      – chqrlie
                      Nov 25 '18 at 9:10


















                      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%2f53464771%2fprintf-long-changes-variable-when-its-digits-is-more-than-9-chars%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

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python