473,396 Members | 2,013 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,396 software developers and data experts.

implicit long int -> int ?

Is there an expression where an int may be used while a long int may
not?
The standard ('99) says:
"The following may be used in an expression wherever an int or
unsigned int may be used:
- An object or expression with an integer type whose integer
conversion rank is less than the rank of int and unsigned int.
- A bit-field of type _Bool, int, signed int, or unsigned int."
Does there exist an example of the sort I described or other rules
apply which imply non-existence of this example? Thanks!
Nov 14 '05 #1
1 1570
Alexander Korovyev wrote:
Is there an expression where an int may be used while a long int may
not?


Yes. It's not valid to pass a long int as an argument to an
unprototyped or variadic function expecting an int:

#include <stdio.h>
printf("%d", 1); /* valid */
printf("%d", 1L); /* invalid */

int abs();
abs(1); /* valid */
abs(1L); /* invalid */

Also, the usual arithemtic conversions may lead to an overflow in
certain circumstances if a long int is substituted for an int.
Consider a machine where int and long are the same width, and INT_MAX
is equal to UINT_MAX. Then in the expression

UINT_MAX + 1

the second operand is converted to unsigned int, which is also the
type of the whole expression; the value is 0. If the second operand
is a long, however:

UINT_MAX + 1L

the first operand is converted to long, and the expression results in
an overflow, which is undefined behaviour.

There are also examples involving sizeof:

int x[sizeof(int)] = {0};
x[(sizeof 1) - 1];
x[(sizeof 1L) - 1];

The last statement has undefined behaviour on machines where long is
larger than int.

Jeremy.
Nov 14 '05 #2

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

Similar topics

3
by: Reneé | last post by:
I wanted to know the order of implicit conversions and which sort of values allow them. From searching around in books and the archive of this mailing list, it seems to be that only numbers are...
15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
2
by: Jon Shemitz | last post by:
How come I can write code like "if (L)" but NOT code like "if (L == true)" when L is a class with an implicit operator bool? /////////// List L = new List(); public class List { private...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
8
by: Cory Nelson | last post by:
In the class here: http://dev.int64.org/snips/bigint.hpp I want to do uint512 = 0; large_integer has a constuctor taking the lo-word (a uint256 in this case), so this currently goes from long...
22
by: Nathan Laff | last post by:
so, myself and the other developers i work with were looking at the v3.0 specifications. extensions look great, buy what is the point of implicit variables with the 'var' type? before you bash...
18
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
2
by: kalki70 | last post by:
Hello, If I have a function like : long long getNumber(); I can use implicit casting, so I can use this function with any compatible value, for instance: int a = getNumber();
82
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure...
12
by: elliot.li.tech | last post by:
Hello everyone, I'm migrating a C++ program from 32-bit systems to 64-bit systems. The old programmers used some annoying assignments like assigning "long" to "int", which may lead to data loss....
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:
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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.