473,378 Members | 1,397 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.

difference between int and long

hello all

I am bit confused with int and long data type.

I read in some book that, int cal hold 2^16 values
where in long can hold 2^32. and both data types are 4 bytes long.

My doubt this, since both are 4 bytes , how can it hold different size?

Thank you
Jan 16 '07 #1
14 24538
Imran wrote:
hello all

I am bit confused with int and long data type.

I read in some book that, int cal hold 2^16 values
where in long can hold 2^32. and both data types are 4 bytes long.

My doubt this, since both are 4 bytes , how can it hold different size?

Thank you

If they are both 4 bytes then they can both hold 2^32. The difference
between int and long depends on the type of processor. On many
processors today they are exactly the same.

--
Scott McPhillips [VC++ MVP]

Jan 16 '07 #2

Imran napsal:
hello all

I am bit confused with int and long data type.

I read in some book that, int cal hold 2^16 values
where in long can hold 2^32. and both data types are 4 bytes long.

My doubt this, since both are 4 bytes , how can it hold different size?

Thank you
Standard guarantees only that int is at least 16-bit and long is at
least 32-bit and sizeof(int) <= sizeof(long).

So for example on typical 32-bit platform are int and long both 32-bit,
on 64-bit Linux can be int 32-bit and long 64-bit etc.

Jan 16 '07 #3
On Tue, 16 Jan 2007 10:30:40 -0500, Scott McPhillips [MVP] wrote:
Imran wrote:
>hello all

I am bit confused with int and long data type.

I read in some book that, int cal hold 2^16 values
where in long can hold 2^32. and both data types are 4 bytes long.

My doubt this, since both are 4 bytes , how can it hold different size?

Thank you


If they are both 4 bytes then they can both hold 2^32. The difference
between int and long depends on the type of processor. On many
processors today they are exactly the same.
That is not true. C++ defines a byte in terms of the size of a character
which must be at least 8 bits.
The meaning of "4 bytes" thus is 4 times the size of char which doesn't
have to equal 32 bits in size.
Jan 16 '07 #4
Philipp Reh write:
If they are both 4 bytes then they can both hold 2^32. The difference
between int and long depends on the type of processor. On many
processors today they are exactly the same.

That is not true. C++ defines a byte in terms of the size of a character
which must be at least 8 bits.
The meaning of "4 bytes" thus is 4 times the size of char which doesn't
have to equal 32 bits in size.
It seems to me, that there is no keyword "byte" in C++ programs.
Outside of C++, most people treat bit as bit and byte as 8 bit - fixed
size of "byte" is more useful, because a man can understand concrete
size of memory in bits. I think "byte" is language independent system
independent memory size, similar to "bit".

But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,
a) char by char describe continuous memory ( memory without holes and
lost bits )
b) char is suitable for all other types
1) as align bound of the type
2) number of chars per the type is integral number (not float)
c) sizeof() of char is always "1"
d) each type has MAX_SIZE according only to memory size - if "char" is
16 bit, char can hold values 0x0000 - 0xffff, not 0x00 - 0xff.
e) 8bit<=char<=short<=int<=long

On x86 "char" number of "bits" is equal to "byte" number of "bits".

The reason to declare type with sizeof==2, aligned in memory to
sizeof==4 boundary is allow packed storage in external memory or packed
structures, aligned to sizeof==2.

Jan 17 '07 #5
* Grizlyk:
Philipp Reh write:
>>If they are both 4 bytes then they can both hold 2^32. The difference
between int and long depends on the type of processor. On many
processors today they are exactly the same.
That is not true. C++ defines a byte in terms of the size of a character
which must be at least 8 bits.
The meaning of "4 bytes" thus is 4 times the size of char which doesn't
have to equal 32 bits in size.

It seems to me, that there is no keyword "byte" in C++ programs.
That's right, but in the C++ standard and C++ context (or C, for that
matter) 'byte' means 'char' and vice versa.

Outside of C++, most people treat bit as bit and byte as 8 bit - fixed
size of "byte" is more useful, because a man can understand concrete
size of memory in bits.
Yep, that's the old "eat dung" argument, that twenty zillion flies can't
be wrong. Yet, while they're undoubtedly doing what's right for them,
and so aren't wrong about eating that dung, the inference that the same
behavior is appropriate for, say, humans, is incorrect. There's more
than one meaning of 'byte' -- after all, language evolves -- and in a
technical context where the number of bits matters the 8 bit entity is
more properly and precisely called an 'octet' (mostly this is used by
electrical engineers and the telecommunications/network industry).

I think "byte" is language independent system
independent memory size, similar to "bit".
Not in the context of C++. Look it up at NIST, IEC or really anywhere.
Outside the context of C++, it is a /system dependent/ size. Which is
why 'octet' is used when it's necessary to be precise.

But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,
Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
char is 1).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 17 '07 #6

"Grizlyk" <gr******@yandex.ruwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
Philipp Reh write:
If they are both 4 bytes then they can both hold 2^32. The difference
between int and long depends on the type of processor. On many
processors today they are exactly the same.

That is not true. C++ defines a byte in terms of the size of a character
which must be at least 8 bits.
The meaning of "4 bytes" thus is 4 times the size of char which doesn't
have to equal 32 bits in size.

It seems to me, that there is no keyword "byte" in C++ programs.
No, the keyword is 'char'. The standard (IEC 14882) defines 'char'
as being a byte.
Outside of C++, most people treat bit as bit
A bit is a bit.
>and byte as 8 bit - fixed
That's not always true. The proper term for an eight-bit object
is 'octet'.
size of "byte" is more useful, because a man can understand concrete
size of memory in bits.
Except that each platform defines what 'concrete' is. :-)
I think "byte" is language independent system
It's platform dependent. Some use different size bytes than others.
that's why we have the 'CHAR_BIT' macro (and
std::numeric_limits<char>::digits)
independent memory size, similar to "bit".

But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,
Both C and C++ define 'char' as meaning *exactly* 'byte', whatever
the size of a byte on the host platform happens to be. However, both
languages require that the size of a char (i.e. byte) be at least eight
bits.

So on any system, regardless of how many bits in a byte, sizeof(char) == 1.
Always.

-Mike
Jan 17 '07 #7

Alf P. Steinbach wrote:
Yep, that's the old "eat dung" argument, that twenty zillion flies can't
No, it is not "eat dung". Ii is standard international (SI) measure of
information or storage capacity, used by most people in practical
cases, similarly to meter or second.
But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,

Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
char is 1).
You are right formally, but fortunatelly, the part of standard can be
silently ignored at practical cases, as well as we can silently ignore
existence of keyword "goto". I am shure, if you will describe storage
capacity elsewere out of C++ context, much better for all (and for you
too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
"13-bit C++ bytes", if you want, that your opponent can understand what
did you say.

And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
- C++ specific memory size as base for all types.

Jan 17 '07 #8
* Grizlyk:
Alf P. Steinbach wrote:
>Yep, that's the old "eat dung" argument, that twenty zillion flies can't

No, it is not "eat dung". Ii is standard international (SI) measure of
information or storage capacity, used by most people in practical
cases, similarly to meter or second.
>>But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,
Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
char is 1).

You are right formally, but fortunatelly, the part of standard can be
silently ignored at practical cases, as well as we can silently ignore
existence of keyword "goto". I am shure, if you will describe storage
capacity elsewere out of C++ context, much better for all (and for you
too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
"13-bit C++ bytes", if you want, that your opponent can understand what
did you say.

And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
- C++ specific memory size as base for all types.
Manual Plonk.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 17 '07 #10
Grizlyk wrote:
>
Alf P. Steinbach wrote:
>Yep, that's the old "eat dung" argument, that twenty zillion flies can't

No, it is not "eat dung". Ii is standard international (SI) measure of
information or storage capacity, used by most people in practical
cases, similarly to meter or second.
But C++ "char" is not a "byte". C++ "char" is minimal system depended
granularity (part) of memory,

Sorry, that's incorrect. See §1.7/1 (byte defined as fundamental
storage unit), §3.9/2 (effective equivalence of bytes and chars wrt.
copying PODs) and §5.3.3/1 (sizeof yields the number of bytes, sizeof
char is 1).

You are right formally, but fortunatelly, the part of standard can be
silently ignored at practical cases, as well as we can silently ignore
existence of keyword "goto".
Actually, we cannot ignore either of those in this group: neither for
practical purposes nor (and more importantly) for discussing C++.
I am shure, if you will describe storage
capacity elsewere out of C++ context, much better for all (and for you
too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
"13-bit C++ bytes", if you want, that your opponent can understand what
did you say.
Sure. It is somewhat unfortunate that the C++ standard is intruding quite a
bit into the ordinary English language by specializing the meanings of
common terms such
as "exception", "object", "undefined" / "unspecified" / "implementation
defined" (behavior) or, in this case, "byte". However, communication in
this group is greatly simplified if everybody just sticks to two simple
conventions:

a) If the standard defines the meaning of a term, do not use that term with
a different meaning without explicitly indicating so.

b) If the standard provides a term for a concept and you mean that concept,
use the terminology used in the standard.

As these two rules of thumb have proven quite useful, regulars tend to be
quite opinionate about breaking those rules willfully.

Note that I am not arguing that you should not use "byte" in its ordinary
meaning outside this group. But within this group, it has proven useful to
avoid confusion arising from competing meanings. The resolution of such
competition is: the meaning used in the standard wins.

And fortunatelly, "byte" is not C++ keyword, so we can easy use "char"
- C++ specific memory size as base for all types.
That "byte" is not a keyword does not make it any less C++ specific
terminology as, say, "object", which is also not a keyword.
Best

Kai-Uwe Bux
Jan 17 '07 #11

Kai-Uwe Bux wrote:
Grizlyk wrote:
I am shure, if you will describe storage
capacity elsewere out of C++ context, much better for all (and for you
too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
"13-bit C++ bytes", if you want, that your opponent can understand what
did you say.

Sure. It is somewhat unfortunate that the C++ standard is intruding quite a
bit into the ordinary English language by specializing the meanings of
common terms such
as "exception", "object", "undefined" / "unspecified" / "implementation
defined" (behavior) or, in this case, "byte". However, communication in
this group is greatly simplified if everybody just sticks to two simple
conventions:
Are you really do not see defferences between SI definitions and other
"words" in the world?

Well, maybe "byte" is not defined in SI, but the fixed size of byte (8
bit) is conventional for most peolpe in the world. And no one "cometee"
of any language can not force people to have more than one "byte" with
different meaning, as no one "cometee" can not introduce two types of
"dollar": "ordinary dollar"- 100-cents, and "C++ dollar" - with
variable number of cents, for example 315 cents.

Especially, when C++ has own replacement for "C++ specific byte" - it
is "char". There is no sence to have in C++ "byte" definition (which
constradiscts to ordinary "byte" definition), because "char" exist.

To discuss "to define 'C++ specific byte' or do not" is time wasted.

Jan 19 '07 #12
Grizlyk wrote:
>
e) 8bit<=char<=short<=int<=long
Accordance to "The design and evolution of C++"
e) 8bit<=char<=short<=16bit<=int<=long

Jan 19 '07 #13
Grizlyk wrote:
>
Kai-Uwe Bux wrote:
>Grizlyk wrote:
I am shure, if you will describe storage
capacity elsewere out of C++ context, much better for all (and for you
too) to use constant "byte, Kbyte, Mbyte and so on" size instead of
"13-bit C++ bytes", if you want, that your opponent can understand what
did you say.

Sure. It is somewhat unfortunate that the C++ standard is intruding quite
a bit into the ordinary English language by specializing the meanings of
common terms such
as "exception", "object", "undefined" / "unspecified" / "implementation
defined" (behavior) or, in this case, "byte". However, communication in
this group is greatly simplified if everybody just sticks to two simple
conventions:

Are you really do not see defferences between SI definitions and other
"words" in the world?

Well, maybe "byte" is not defined in SI, but the fixed size of byte (8
bit) is conventional for most peolpe in the world.
True and already conceded.
And no one "cometee"
of any language can not force people to have more than one "byte" with
different meaning, as no one "cometee" can not introduce two types of
"dollar": "ordinary dollar"- 100-cents, and "C++ dollar" - with
variable number of cents, for example 315 cents.
Actually, it can. In the very same way that mathematicians have overloaded
the terms "group", "ring", "field", "loop", etc. Once introduced by the
standard, the specialized meanings are default meanings In This News Group.
Especially, when C++ has own replacement for "C++ specific byte" - it
is "char". There is no sence to have in C++ "byte" definition (which
constradiscts to ordinary "byte" definition), because "char" exist.

To discuss "to define 'C++ specific byte' or do not" is time wasted.
Whatever.
Best

Kai-Uwe Bux
Jan 19 '07 #14
weaknessforcats
9,208 Expert Mod 8TB
Lotta discussion here. No one has mentioned <limits.h> for C or <limits> for C++.

For both C and C++ the sizeof the basic types is implementation dependent. That is, it is up to the compiler writer.

Usually (but not required) is 8 bits for char and 16 bits for short. Then int is >= short and long >= int.

All of these integer ypes were created in the days of computers with small memories so every byte was important. Today you use int.

C++ code should not use char or char* as there a library objects you use instead.
Dec 7 '10 #15

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

Similar topics

17
by: Nathan Given | last post by:
Hello All, I am trying to debug a broken query. The query uses Left$(,4) instead of Left(,4). What is the difference between the Left() and Left$() functions in Microsoft Access? Thanks!...
45
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
4
by: mosimu | last post by:
I have never fully understood what the difference is between these two forms of casting. Can anyone please clarify? long is a primitive data type so no, it's not calling a class constructor. ...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
2
by: John Smith | last post by:
What is the difference? Is LONG VARCHAR only 28 bytes longer then VARCHAR? Is this the only difference?
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
73
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may...
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.