Thread: noob question about long int use

  1. #1
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94

    noob question about long int use

    I am just starting to learn C. I am learning about loops and thought it would be a good idea to look at some sample source code to see as many examples as I could find on the subject. I found some samples here: C / ANSI-C examples (example source code) Organized by topic

    This is the example I don't understand

    This code counts backward and sums as it goes.
    Code:
    #include <stdio.h>
    void main() {
      
      long sum = 0L;  
      
      int count = 10;  /* The number of integers to be summed */
      
      int i = 0;      /* The loop counter                    */
    
    
      /* Sum integers from count to 1 */
      for (i = count ; i >= 1 ; sum += i-- );
    
      printf("\nTotal of the first %d numbers is %ld\n", count, sum);
    }
    They use
    Code:
    long sum = 0L;
    This seems odd seeing that the total possible sum cant be more then 60. From the reading I have done so far it seems to me long int doubles the storage requirements of the int from 4 to 8 bytes. Maybe I am missing something here but the number 55 should not required the extra bytes. Is this the best way to handle this or would
    Code:
     int sum = 0;
    do just as well. I was wondering if anyone could explain why they used the long int for this purpose.
    Last edited by jimtuv; 05-04-2010 at 02:58 PM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            printf("sizeof char = %i\n", sizeof(char));
            printf("sizeof short = %i\n", sizeof(short));
            printf("sizeof int = %i\n", sizeof(int));
            printf("sizeof long = %i\n", sizeof(long));
            printf("sizeof long int = %i\n", sizeof(long int));
            printf("sizeof long long = %i\n", sizeof(long long));
            printf("sizeof long long int = %i\n", sizeof(long long int));
    //      printf("sizeof long long long = %i\n", sizeof(long long long)); //compiler error.
            return 0;
    }
    Code:
    sizeof char = 1
    sizeof short = 2
    sizeof int = 4
    sizeof long = 4
    sizeof long int = 4
    sizeof long long = 8
    sizeof long long int = 8

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jimtuv View Post
    I was wondering if anyone could explain why they used the long int for this purpose.
    Possibly because they wanted to allow for larger initial values of "count" than 10.

    You might also wish to ponder whether an int is required to be 4 bytes. Can it be less?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User sbaginov's Avatar
    Join Date
    May 2010
    Location
    Italy
    Posts
    19
    Quote Originally Posted by jimtuv View Post
    They use
    Code:
    long sum = 0L;
    This seems odd seeing that the total possible sum cant be more then 60. From the reading I have done so far it seems to me long int doubles the storage requirements of the int from 4 to 8 bytes. Maybe I am missing something here but the number 55 should not required the extra bytes..
    When you code, you always have to think about the future use of it. Now you can read just numbers less than or equal to 100, because you type them and you are lazy!, but tomorrow, in real life, your code will have to read something like 32000, 31045, 29087 and then! an overflow might occurr after a few steps: though the values you read might even be stored by a short, their manipulation might easily produce results that exceed the short capacity: this is why one might choose long or even double...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by sbaginov View Post
    When you code, you always have to think about the future use of it. Now you can read just numbers less than or equal to 100, because you type them and you are lazy!, but tomorrow, in real life, your code will have to read something like 32000, 31045, 29087 and then! an overflow might occurr after a few steps: though the values you read might even be stored by a short, their manipulation might easily produce results that exceed the short capacity: this is why one might choose long or even double...
    Then you can just change the type easily...

    Quote Originally Posted by Kennedy View Post
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            printf("sizeof char = %i\n", sizeof(char));
            printf("sizeof short = %i\n", sizeof(short));
            printf("sizeof int = %i\n", sizeof(int));
            printf("sizeof long = %i\n", sizeof(long));
            printf("sizeof long int = %i\n", sizeof(long int));
            printf("sizeof long long = %i\n", sizeof(long long));
            printf("sizeof long long int = %i\n", sizeof(long long int));
    //      printf("sizeof long long long = %i\n", sizeof(long long long)); //compiler error.
            return 0;
    }
    Code:
    sizeof char = 1
    sizeof short = 2
    sizeof int = 4
    sizeof long = 4
    sizeof long int = 4
    sizeof long long = 8
    sizeof long long int = 8
    Sizes aren't guaranteed by the standard. This is only the output of a specific platform and compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Elysia View Post
    Sizes aren't guaranteed by the standard. This is only the output of a specific platform and compiler.
    I kinda expected the OP to use the code to see what values that machine/compiler use. MOST computers/compilers these days will have the above lengths -- no, it is not a fully qualified standard, but it does seem to be the de facto.

  7. #7
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    Thank you so much for your help. I see that choosing weather to use long is a good idea if you are not sure of the future use of the code or there are concerns that the result may in some way exceed the capacity of the variable. It seems to me though that adding in some error checking to make sure that any input would not exceed the variables size would also be a good idea.

    As for the size of int I would think logically the smallest size would be short int 2 bytes. Though it would stand to reason you could store an unsigned integer between 1 and 255 in one byte. But I wouldn't be surprised to find I am wrong. I am just starting out in all of this.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jimtuv View Post
    This seems odd seeing that the total possible sum cant be more then 60.
    Now that's just crazy talk. If the actual, real world value of count was always fixed at 10, then you already know the answer -- it's 55 -- and this code shouldn't even exist.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    Quote Originally Posted by brewbuck View Post
    Now that's just crazy talk. If the actual, real world value of count was always fixed at 10, then you already know the answer -- it's 55 -- and this code shouldn't even exist.
    Thats nice one call me lazy and another crazy for asking about some else's code because it seemed odd to me, and I have all of 5 days experience in C. Nice place you all have here. really encouraging. don't worry I will figure it out with out help.

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by jimtuv View Post
    Thats nice one call me lazy and another crazy for asking about some else's code because it seemed odd to me, and I have all of 5 days experience in C. Nice place you all have here. really encouraging. don't worry I will figure it out with out help.
    It's actually a decent place to get answers to questions. You have to realize everyone here is doing this out of their own time completely pro-bono. So don't get so upset all of the sudden. Come back for more questions.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jimtuv View Post
    Thank you so much for your help. I see that choosing weather to use long is a good idea if you are not sure of the future use of the code or there are concerns that the result may in some way exceed the capacity of the variable. It seems to me though that adding in some error checking to make sure that any input would not exceed the variables size would also be a good idea.
    For the future is probably a moot point. If you need bigger variables in the future, you can just change the size. You are not at a stage where it's absolutely critical to do it right the first time around.
    Also, checking input is always a good idea, but it can be tricky, especially when it comes to stuff like overflow. Although if you get the raw string input and use strtol, you can actually verify if there was overflow and underflow.

    As for the size of int I would think logically the smallest size would be short int 2 bytes. Though it would stand to reason you could store an unsigned integer between 1 and 255 in one byte. But I wouldn't be surprised to find I am wrong. I am just starting out in all of this.
    I would be careful with the words there. An int is not a short int. A short int can be shortened to short, but not int. Also, an int cannot be 1 byte. The standard guarantees that short int shall be greater than char, which is always 1 one.
    char < short int <= int <= long int <= long long int, I believe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Quote Originally Posted by Kennedy View Post
    I kinda expected the OP to use the code to see what values that machine/compiler use. MOST computers/compilers these days will have the above lengths -- no, it is not a fully qualified standard, but it does seem to be the de facto.
    Most compilers for microcontrollers (8 bit and 16 bit) use 16-bit ints and 32-bit longs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 06:02 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM