Long Max question.

So I'm working on my first program for class, and I'm still very new to all of this. My program is supposed to "assign the largest and
smallest short, int, and long, both signed and unsigned, from standard
headers/libraries to variables of the correct data type." While I am giving it my best shot, I've noticed that when I run what I have so far, it gives the value of LONG_MAX as 9223372036854775807, as opposed to 2147483647, which is what it is defined as via this website. Is this something very simple that I am not aware of? Any help would be appreciated. Thank you.
9223372036854775807 is a perfectly legitimate LONG_MAX. The ranges of the integral data types are not fixed.
The value of LONG_MAX will vary depending upon the environment (hardware and compiler). The value you quote looks like a 64-bit value while the smaller figure relates to a 32-bit environment.

Here's a quote from this site:
The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems. But for other systems, the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size. The same applies to the floating point types float, double and long double, where each one must provide at least as much precision as the preceding one.

http://www.cplusplus.com/doc/tutorial/variables/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<cmath>
#include<climits>

#define NUM_BITS_BYTE 8


int main() {

//const int NUM_BITS_BYTE=8;

//Prints the maximum signed long that can be represented in C and C++
std::cout << "The long max in decimal is: " <<std::dec << LONG_MAX << std::endl;

std::cout << "The long max in hex is: " <<std::hex << LONG_MAX << std::endl;

std::cout << "And the long max in oct is: " <<std::oct << LONG_MAX << std::endl;

std::cout << "The unsign long max in dec is: " <<std::dec << ULONG_MAX << std::endl;

std::cout << "The unsign long max in hex is: " <<std::hex << ULONG_MAX << std::endl;

std::cout << "The unsign long max in oct is: " <<std::oct << ULONG_MAX << std::endl;

std::cout << "The unsigned short max in dec is: " <<std::dec << USHRT_MAX << std::endl;

std::cout << "The unsigned short max in hex is: " <<std::hex << USHRT_MAX << std::endl;

std::cout << "The unsigned short max in oct is: " <<std::oct << USHRT_MAX << std::endl;


return 0;
}


Does this make any sense at all?
Well, it isn't a solution to the problem: "assign the largest and
smallest short, int, and long, both signed and unsigned, from standard
headers/libraries to variables of the correct data type.
"

(Your program doesn't seem to define any variables at all).

One suggestion, decimal and hex notation are sensible choices.
I wouldn't bother with octal unless there is some specific need to do so.
Last edited on
Okay, that's good to know. How do I go about defining a variable? And I included octal because it is part of the assignment, even though it doesn't really seem necessary.

Err wait I'll just use the previous link for the variable.
Last edited on

unsigned long int unsignlong=ULONG_MAX;
signed long int signlong=LONG_MAX;

std::cout <<"The unsigned long is: " <<std::dec << unsignlong << std::endl;


Does this make any more sense?
I think so yes. Looks like that's the sort of thing which was asked for.
Topic archived. No new replies allowed.