Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:22:47+00:00 2026-06-15T04:22:47+00:00

In a recent homework assignment I’ve been told to use long variable to store

  • 0

In a recent homework assignment I’ve been told to use long variable to store a result, since it may be a big number.

I decided to check will it really matter for me, on my system (intel core i5/64-bit windows 7/gnu gcc compiler) and found out that the following code:

printf("sizeof(char) => %d\n", sizeof(char));
printf("sizeof(short) => %d\n", sizeof(short));
printf("sizeof(short int) => %d\n", sizeof(short int));
printf("sizeof(int) => %d\n", sizeof(int));
printf("sizeof(long) => %d\n", sizeof(long));
printf("sizeof(long int) => %d\n", sizeof(long int));
printf("sizeof(long long) => %d\n", sizeof(long long));
printf("sizeof(long long int) => %d\n", sizeof(long long int));

produces the following output:

sizeof(char) => 1
sizeof(short) => 2
sizeof(short int) => 2
sizeof(int) => 4
sizeof(long) => 4
sizeof(long int) => 4
sizeof(long long) => 8
sizeof(long long int) => 8

In other words, on my system, int and long are the same, and whatever will be too big for int to hold, will be too big for long to hold as well.

The homework assignment itself is not the issue here. I wonder how, on a system where int < long, should I assign an int to long?

I’m aware to the fact that there are numerous closely related questions on this subject, but I feel that the answers within these do not provide me with the complete understanding of what will or may happen in the process.

Basically I’m trying to figure out the following:

  1. Should I cast long to int before the assignment, or since long
    is not a different data type, but merely a modifier,
    it will be
    considered unharmful to assign directly?
  2. What happens on systems where long > int? Will the result be
    undefined (or unpredictable) or it will cause the extra parts of the
    variable to be omitted?
  3. How does the casting from long to int works in C?
  4. How does the assignment from long to int works in C when I don’t
    use casting?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:22:48+00:00Added an answer on June 15, 2026 at 4:22 am

    The language guarantees that int is at least 16 bits, long is at least 32 bits, and long can represent at least all the values that int can represent.

    If you assign a long value to an int object, it will be implicitly converted. There’s no need for an explicit cast; it would merely specify the same conversion that’s going to happen anyway.

    On your system, where int and long happen to have the same size and range, the conversion is trivial; it simply copies the value.

    On a system where long is wider than int, if the value won’t fit in an int, then the result of the conversion is implementation-defined. (Or, starting in C99, it can raise an implementation-defined signal, but I don’t know of any compilers that actually do that.) What typically happens is that the high-order bits are discarded, but you shouldn’t depend on that. (The rules are different for unsigned types; the result of converting a signed or unsigned integer to an unsigned type is well defined.)

    If you need to safely assign a long value to an int object, you can check that it will fit before doing the assignment:

    #include <limits.h> /* for INT_MIN, INT_MAX */
    
    /* ... */
    
    int i;
    long li = /* whatever */
    
    if (li >= INT_MIN && li <= INT_MAX) {
        i = li;
    }
    else {
        /* do something else? */
    }
    

    The details of “something else” are going to depend on what you want to do.

    One correction: int and long are always distinct types, even if they happen to have the same size and representation. Arithmetic types are freely convertible, so this often doesn’t make any difference, but for example int* and long* are distinct and incompatible types; you can’t assign a long* to an int*, or vice versa, without an explicit (and potentially dangerous) cast.

    And if you find yourself needing to convert a long value to int, the first thing you should do is reconsider your code’s design. Sometimes such conversions are necessary, but more often they’re a sign that the int to which you’re assigning should have been defined as a long in the first place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working through a recent Computer Science homework involving recursion and big-O notation.
A recent homework assignment I have received asks us to take expressions which could
In recent versions of python, one can use something like with open('abc.txt') as f:
Since recent update Xcode 4.3 now seems to default to LLDB debugger. I just
I have been searching for a pure Java SSH library to use for a
Since recent runtimes in iOS, we are able to define properties that will generate
A recent project made use of very pixel-large (~5e3px2) but still byte-small (~100kb —
I have been working on operations with strings in a recent 100 level CompSci
My recent app is like a forum, i use listview to show each thread.
I want to use recent C# 5.0 async methods, however some specific way. Consider

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.