473,385 Members | 1,673 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,385 software developers and data experts.

Type of Constant

I've read in a couple of textbooks that a integer constant is of type
int if an int can hold the number, otherwise it is of type long, or
unsigned long if necessary.

Does this take into account the extra negative number on a 2-s
complement machine? For example, if int-s are 16 bits, is -32768 an int?

Thank you.

Girish Sharma
(remove remv_ from address)

Nov 14 '05 #1
5 1356
On Wed, 30 Jun 2004 04:51:49 GMT, Girish Sharma
<re****************@earthlink.net> wrote in comp.lang.c:
I've read in a couple of textbooks that a integer constant is of type
int if an int can hold the number, otherwise it is of type long, or
unsigned long if necessary.

Does this take into account the extra negative number on a 2-s
complement machine? For example, if int-s are 16 bits, is -32768 an int?
No, that expression can't be an int. The constant "-32768" actually
consists of two parts. One is the literal constant "32768". The
other is the unary minus operator.

The numeric literal 32768 has type signed long on implementations
where INT_MAX is 32767. The unary minus operator is then applied to
the signed long value 32767 resulting in the signed long value -32768.

This expression can be used to initialize or assign to an int value,
if INT_MIN is defined as -32768, and the result is well defined
because even though the expression has the type signed long, its value
is within the range of values for an int.
Thank you.

Girish Sharma
(remove remv_ from address)


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
In article <qi********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
On Wed, 30 Jun 2004 04:51:49 GMT, Girish Sharma
<re****************@earthlink.net> wrote in comp.lang.c:
I've read in a couple of textbooks that a integer constant is of type
int if an int can hold the number, otherwise it is of type long, or
unsigned long if necessary.

Does this take into account the extra negative number on a 2-s
complement machine? For example, if int-s are 16 bits, is -32768 an int?


No, that expression can't be an int. The constant "-32768" actually
consists of two parts. One is the literal constant "32768". The
other is the unary minus operator.

The numeric literal 32768 has type signed long on implementations
where INT_MAX is 32767. The unary minus operator is then applied to
the signed long value 32767 resulting in the signed long value -32768.

This expression can be used to initialize or assign to an int value,
if INT_MIN is defined as -32768, and the result is well defined
because even though the expression has the type signed long, its value
is within the range of values for an int.


On a 2's complement machine, I would expect that

sizeof (INT_MIN) == sizeof (int)
sizeof (-32768) == sizeof (long)
sizeof (INT_MIN) != sizeof (-32768)

so you will usually find

#define INT_MIN (-32767 - 1)
Nov 14 '05 #3
"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
On a 2's complement machine, I would expect that

sizeof (INT_MIN) == sizeof (int)


Does C90 (or C95) not require INT_MIN to be of type int, as C99 does? [The requirement
seems absent from the C89 draft.]

--
Peter
Nov 14 '05 #4
Girish Sharma <re****************@earthlink.net> writes:
I've read in a couple of textbooks that a integer constant is of type
int if an int can hold the number, otherwise it is of type long, or
unsigned long if necessary.

Does this take into account the extra negative number on a 2-s
complement machine? For example, if int-s are 16 bits, is -32768 an int?


-32768 is not an integer constant; it's an expression consisting of an
integer constant 32768 with a unary "-" operator applied to it.

--
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
On Wed, 30 Jun 2004 08:13:45 +0100, Christian Bau
<ch***********@cbau.freeserve.co.uk> wrote in comp.lang.c:
In article <qi********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
On Wed, 30 Jun 2004 04:51:49 GMT, Girish Sharma
<re****************@earthlink.net> wrote in comp.lang.c:
I've read in a couple of textbooks that a integer constant is of type
int if an int can hold the number, otherwise it is of type long, or
unsigned long if necessary.

Does this take into account the extra negative number on a 2-s
complement machine? For example, if int-s are 16 bits, is -32768 an int?


No, that expression can't be an int. The constant "-32768" actually
consists of two parts. One is the literal constant "32768". The
other is the unary minus operator.

The numeric literal 32768 has type signed long on implementations
where INT_MAX is 32767. The unary minus operator is then applied to
the signed long value 32767 resulting in the signed long value -32768.

This expression can be used to initialize or assign to an int value,
if INT_MIN is defined as -32768, and the result is well defined
because even though the expression has the type signed long, its value
is within the range of values for an int.


On a 2's complement machine, I would expect that

sizeof (INT_MIN) == sizeof (int)
sizeof (-32768) == sizeof (long)
sizeof (INT_MIN) != sizeof (-32768)

so you will usually find

#define INT_MIN (-32767 - 1)


I'm not exactly sure from what you posted whether you are agreeing
with me and providing supplemental material, or arguing with me.

I think you're agreeing with me...

Another way to define INT_MIN, and also handy if you are writing your
own <stdint.h> header for an older compiler that lacks one, is:

#define INT_MIN (-INT_MAX - 1)

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6

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

Similar topics

21
by: Nitin Bhardwaj | last post by:
Hi all, It is said that C++ is a strongly typed language and thus a type-safe language (unlike C). So how does one explain the following behaviour : int main(void) { char *p = NULL; p = "A...
2
by: Sam Sungshik Kong | last post by:
Hello! I studied C# a little bit and am trying to compare it with VB.Net. There's 'Option Strict' in VB.Net. I thought that if I turn it on, it is as strict as C# when checking types. See...
31
by: CeZaR | last post by:
Hi, How can i specify the return type of a function returning a managed array of chars. If i try to write: "char __gc func()" i get an error! How can i do that? Thanks!
1
by: Angel Tsankov | last post by:
Does the C++ standard define what should happen if the size of int in the following code cannot accommodate the bit field? struct bit_fields { unsigned int digit:17; // int is 16 bits };
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
9
by: Hamish M | last post by:
Hi I am interested in opinions on this topic. I have heard that a suffix is not a good solution and type casts are much better for example. ...
3
by: mario | last post by:
Hi! First of all, sorry for my English, it's not my native tongue. Anyway, here we go: suppose I create a class that handles my own custom text strings. No, suppose I create TWO such classes,...
14
by: Martin Wells | last post by:
I come from C++, and there's a type called "bool" in C++. It works exactly like any other integer type except that when promoted, it either becomes a one or a zero, so you can you bitwise operators...
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.