473,378 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

initializing int to a number bigger than INT_MAX

hi all,
Out of sheer curosity, I decided to initialize an integer with a number
bigger than INT_MAX, however I still am not able to justify its output.
Here is the program :

#include<stdio.h>

int main(void)
{
int t=0xFFFFFFFFFFFFFFFFDABC;
printf("t is : %x\n",t);
getchar();
return 0;
}
I ran this program using Dev-C++ in Windows on x86( gcc.exe
"C:\Untitled.c" -o "C:\Untitled.exe" -Wall -pg -g3
-I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lgmon -pg -g3)

The output was
t is : ffffdabc

What is happening in here?
And even if I write
int t=0xFFFCFDFAFFFFFFFFDABC;
it is giving the same output. Infact even if i increase any hex digit
at beginning(like int t=0xFFAAAAFFFFFFFFFFFFFFDABC;) it is giving the
same output.

I guess its some kind of truncation going on .. but then why is it
ignoring MSBs ? I am also guessing this behaviour is compiler dependent
- am i correct?

Nov 14 '05 #1
7 6095

vi***@gmail.com wrote:
hi all,
Out of sheer curosity, I decided to initialize an integer with a number bigger than INT_MAX, however I still am not able to justify its output. Here is the program :

#include<stdio.h>

int main(void)
{
int t=0xFFFFFFFFFFFFFFFFDABC;
printf("t is : %x\n",t);
getchar();
return 0;
}
I ran this program using Dev-C++ in Windows on x86( gcc.exe
"C:\Untitled.c" -o "C:\Untitled.exe" -Wall -pg -g3
-I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lgmon -pg -g3)

The output was
t is : ffffdabc

What is happening in here?
And even if I write
int t=0xFFFCFDFAFFFFFFFFDABC;
it is giving the same output. Infact even if i increase any hex digit
at beginning(like int t=0xFFAAAAFFFFFFFFFFFFFFDABC;) it is giving the
same output.

I guess its some kind of truncation going on .. but then why is it
ignoring MSBs ? I am also guessing this behaviour is compiler dependent - am i correct?


Nov 14 '05 #2

vi***@gmail.com wrote:
hi all,
Out of sheer curosity, I decided to initialize an integer with a number bigger than INT_MAX, however I still am not able to justify its output. Here is the program :

#include<stdio.h>

int main(void)
{
int t=0xFFFFFFFFFFFFFFFFDABC;
printf("t is : %x\n",t);
getchar();
return 0;
}
I ran this program using Dev-C++ in Windows on x86( gcc.exe
"C:\Untitled.c" -o "C:\Untitled.exe" -Wall -pg -g3
-I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lgmon -pg -g3)

The output was
t is : ffffdabc

What is happening in here?
And even if I write
int t=0xFFFCFDFAFFFFFFFFDABC;
it is giving the same output. Infact even if i increase any hex digit
at beginning(like int t=0xFFAAAAFFFFFFFFFFFFFFDABC;) it is giving the
same output.

I guess its some kind of truncation going on .. but then why is it
ignoring MSBs ? I am also guessing this behaviour is compiler dependent - am i correct?


Hi,
First of all,I think it is[like you said] compiler/platform -
dependent,so,it's
a little OT.Second,why 'getchar()'?And third,this is,in my humble
opinion,clearly undefined.

Nov 14 '05 #3
On Tue, 12 Apr 2005 09:29:35 -0700, vi***@gmail.com said to the parser:
hi all,
Out of sheer curosity, I decided to initialize an integer with a number
bigger than INT_MAX, however I still am not able to justify its output.
Here is the program :

int t=0xFFFFFFFFFFFFFFFFDABC;
First of all, this is in fact *smaller* than INT_MAX. What you've
written is a negative number, as you're using a signed int. All the
extra leading Fs, above and beyond the 32-bit limit of your architecture,
in binary terms, are leading ones, which can "safely" be "dropped" and
still yield the same 2s-complement number in a "lower int-sized" machine,
assuming the meaningful bits of the number are small enough to fit.

Hmmm, I didn't explain that very well, but...

I'm surprised you would know of the existence of INT_MAX, yet
don't seem to follow what it means.

INT...
MAX...

It's the maximum size int you can declare.

I suspect that on your hardware, INT_MAX is 0x7FFF FFFF, or 2,147,483,647.

INT_MIN will be declared as -INT_MAX-1 or -2,147,483,648.

So by trying to enter more than 32 bits into your declared int, it's
getting truncated to the lowest 32 bits, 0xFFFFDABC, or -9540.

Note that 0xFFFF FFFF is the "largest" hexadecimal value that can be held
by an int on your architecture. Note also that for a signed int, this is
actually -1.
I guess its some kind of truncation going on .. but then why is it
ignoring MSBs ? I am also guessing this behaviour is compiler dependent
- am i correct?


In so far as INT_MAX and INT_MIN will change from compiler to compiler and
architecture to architecture, yes.
Michael
Nov 14 '05 #4
"vi***@gmail.com" <bh**************@gmail.com> writes:
hi all,
Out of sheer curosity, I decided to initialize an integer with a number
bigger than INT_MAX, however I still am not able to justify its output.
Here is the program :

#include<stdio.h>

int main(void)
{
int t=0xFFFFFFFFFFFFFFFFDABC;
printf("t is : %x\n",t);
getchar();
return 0;
} [snip] The output was
t is : ffffdabc

[snip]

Strictly speaking, the "%x" specifier expects an unsigned int. You
can almost certainly get away with passing an int, but I still prefer
to use an explicit cast (one of the few cases where casts are useful):

printf("t is : %x\n", (unsigned)t);

The literal 0xFFFFFFFFFFFFFFFFDABC requires 80 bits, which is probably
(but not certainly) larger than any integer type.

An unsuffixed hexadecimal integer constant's type is the first type
from this list:

int
unsigned int
long int
unsigned long int
long long int (C99 only)
unsigned long long int (C99 only)

in which the value can be represented. If it can't be represented in
any of those types, it may have an extended integer type (if the
implementation has extended integer types; I don't know of any that
do). If there is no type that can represent the value, the standard
doesn't say what its type is; I believe this means it invokes
undefined behavior by omission (i.e., the behavior is undefined not
because the standard says so, but because it fails to define the
behavior).

I'd be happier if the standard said something explicit about integer
constants that can't be represented in any type.

On the other hand, suppose you use a literal that fits in some integer
type, but not in int. For example, assuming 32-bit int and long, and
64-bit long long:

int t = 0xFFFFFFFFFFFFDABC; /* 64 bits */

the constant is of type unsigned long long. The value is implicitly
converted to type int before being used to initialize t. An
overflowing conversion to a signed integer type doesn't invoke
undefined behavior; it merely yields an implementation-defined result
or raises an implementation-defined signal. (In C90, it yields an
undefined result; there's no mention of raising a signal.)

Having said all that, on every implementation I know of a conversion
from a larger to a smaller integer type simply discards the high-order
bits. You shouldn't depend on this behavior in portable code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5


Michael Coyne wrote:
On Tue, 12 Apr 2005 09:29:35 -0700, vi***@gmail.com said to the parser:

hi all,
Out of sheer curosity, I decided to initialize an integer with a number
bigger than INT_MAX, however I still am not able to justify its output.
Here is the program :

int t=0xFFFFFFFFFFFFFFFFDABC;

First of all, this is in fact *smaller* than INT_MAX. What you've
written is a negative number, as you're using a signed int. All the
extra leading Fs, above and beyond the 32-bit limit of your architecture,
in binary terms, are leading ones, which can "safely" be "dropped" and
still yield the same 2s-complement number in a "lower int-sized" machine,
assuming the meaningful bits of the number are small enough to fit.

Hmmm, I didn't explain that very well, but...


No, indeed: Much of what you wrote was incorrect.

To begin with, the hexadecimal constant is a large
*positive* number. In fact, all numeric[*] constants in C
are non-negative. The problems here are (1) that the big
hex constant may be too large for any integral data type
supported by the implementation, and (2) even if that's
not the case, it may be bigger than INT_MAX.

Case 1 may apply even though the compiler didn't
complain; I can't find anything in the Standard that seems
to require a diagnostic for out-of-range constants. If
case 1 applies, we really can't tell for sure what happened,
but it looks like the compiler may have mangled the value
(along the lines you describe, and that part of your answer
was correct -- for two's complement machines, anyhow).

If it's case 2, initialization is "as if" by conversion
of the very large value to `int'. Converting an out-of-range
value to `int' produces an implementation-defined result (or
raises an implementation-defined signal). On two's complement
machines it is likely that the implementation-defined result
will be "chopped" in the way you describe, but this is not
guaranteed by the Standard.
[*] The only negative constants I can think of are enum
values specifically declared as negative, and character or
wide-character constants involving "extra" characters not
in the basic execution set (and even then only if `char' or
`wchar_t' is signed). Constants that "look like" numbers
are non-negative.

--
Er*********@sun.com

Nov 14 '05 #6
Keith Thompson <ks***@mib.org> writes:
[...]
An unsuffixed hexadecimal integer constant's type is the first type
from this list:

int
unsigned int
long int
unsigned long int
long long int (C99 only)
unsigned long long int (C99 only)

in which the value can be represented. If it can't be represented in
any of those types, it may have an extended integer type (if the
implementation has extended integer types; I don't know of any that
do). If there is no type that can represent the value, the standard
doesn't say what its type is; I believe this means it invokes
undefined behavior by omission (i.e., the behavior is undefined not
because the standard says so, but because it fails to define the
behavior).

I'd be happier if the standard said something explicit about integer
constants that can't be represented in any type.

[...]

I posted a question about this in comp.std.c, and Wojtek Lerch pointed
me to a constraint in C99 6.4.4p2:

The value of a constant shall be in the range of representable
values for its type.

I think this could be worded better (there's no such thing as "its
type", since 6.4.4.1 doesn't define a type for an integer constant
that can't be represented), but it's clear enough that it's intended
to be a constraint violation.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
On 12 Apr 2005 09:29:35 -0700, "vi***@gmail.com"
<bh**************@gmail.com> wrote:
hi all,
Out of sheer curosity, I decided to initialize an integer with a number
bigger than INT_MAX, however I still am not able to justify its output. <snip example> What is happening in here? <snip>
I guess its some kind of truncation going on .. but then why is it
ignoring MSBs ? I am also guessing this behaviour is compiler dependent
- am i correct?


Your guesses are right. Converting to a signed integer type a value
(of a wider integer type) that doesn't fit produces an
implementation-defined result, or in C99 possibly raises an
implementation-defined signal. It is very common, though not required,
to just take the lower bits, because that efficiently gives the
correct (required) result for values that _do_ fit, and the same logic
works for unsigned types which are required to take the low bits of an
unsigned or 2s-complement representation. (Officially unsigned takes
the 'modulo' value, but for 2sC that equals the low bits. The very few
if any S&M or 1sC machines must do something more complicated.)

Implementation-defined does mean that the documentation associated
with the implementation must specify what it does.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
26
by: G Patel | last post by:
Hi, I'm wondering if anyone knows if the following function will function properly as a set-bit counter on non 2s complement machines (as K&R2 implies). | int bitcount(unsigned x) | { | ...
23
by: Davey | last post by:
How do I display an integer in binary format in C? e.g. 4 displayed as "100"
3
by: pocmatos | last post by:
Hi all, I'm doing parsing with flex and bison and I read numbers which are just a sequence of digits + and keep them in an int, however if the number is bigger than int, I should output error....
4
by: SkyBlue | last post by:
A straight forward Q from C newbie: What is the limit of INT variable type? I'm writing a code to calculate the sum of odd/even numbers between a given range, and I want to limit the number size...
21
by: Frederick Gotham | last post by:
I'm trying to devise a compile-time constant for X, where X is the greatest number which satisfies both the following criteria: (1) X <= DESIGNATED_MAX_VALUE (2) X % Y == 0 I'll try to...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
29
by: fdmfdmfdm | last post by:
let's say without checking including files, how do we to represent the biggest say int in a system? I ran across a book give this code: long int biggest = 0x7FFFFFFF; Does that mean the...
26
by: Yevgen Muntyan | last post by:
Hey, It was mentioned elsewhere that printf("%d", INT_MAX); is implementation-defined. Why? Is it because INT_MAX value is implementation-defined so output depends on implementation, or is it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.