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

Question about mixed scalar type operation

Hi,
I have this little test program:

#include <stdio.h>

int main(){
float f1 = 2.2;
double d1 = 3.3;
int j = f1 * d1;
int i = (int) f1 * d1;
int i2 = (int) (f1 * d1);
printf("%d, i=%d, j=%d, i2=%d\n", f1*d1, i, j, i2);
}

I use gcc on Linux to compile it, there is no warning and the result
is like this:

May 14 '07 #1
3 1499
li*****@hotmail.com wrote:
Hi,
I have this little test program:

#include <stdio.h>

int main(){
float f1 = 2.2;
double d1 = 3.3;
int j = f1 * d1;
int i = (int) f1 * d1;
int i2 = (int) (f1 * d1);
printf("%d, i=%d, j=%d, i2=%d\n", f1*d1, i, j, i2);
}

I use gcc on Linux to compile it, there is no warning and the result
is like this:
Well, if you got /no/ output, that's surprising but allowed, since
the expression `f1*d1` is of type double and you're trying to print
it through `%d`, which requires an `int`; undefined behaviour, anything
can happen in the next half-hour.

Compile with warnings turned up: gcc spots (some, compile-time-visible)
format mismatches.

(fx:muse) Shouldn't it be possible for an implementation to arrange to
pass to the printf-family a description of the actual types of the arguments,
so that the called function could check the requested conversions against
the type list? Since it would be implementation magic, it wouldn't need
to be expressible in Standard C. You'd only need to use the full power
of the machinery when the format and arguments weren't both visible at
compile-time. And since it's IO conversion, the cost of the check should
be small compared to the cost of the call.

--
There' no hortage of vowel on Uenet.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 15 '07 #2
li*****@hotmail.com wrote:
>
.... snip ...
>
#include <stdio.h>

int main(){
float f1 = 2.2;
double d1 = 3.3;
int j = f1 * d1;
int i = (int) f1 * d1;
int i2 = (int) (f1 * d1);
printf("%d, i=%d, j=%d, i2=%d\n", f1*d1, i, j, i2);
}

I use gcc on Linux to compile it, there is no warning and the
result is like this:
[2] c:\c\junk>gcc -W -Wall -ansi -pedantic junk.c
junk.c: In function `main':
junk.c:9: warning: int format, double arg (arg 2)
junk.c:10: warning: control reaches end of non-void function

The above are the minimum arguments needed to compile std C. The
casts are unnecessary. However casts are missing in the arguments
to printf.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

May 16 '07 #3
On Tue, 15 May 2007 09:34:37 +0100, Chris Dollin <ch**********@hp.com>
wrote:
<snip: YA printf-mismatch problem>
(fx:muse) Shouldn't it be possible for an implementation to arrange to
pass to the printf-family a description of the actual types of the arguments,
so that the called function could check the requested conversions against
the type list? Since it would be implementation magic, it wouldn't need
to be expressible in Standard C. You'd only need to use the full power
of the machinery when the format and arguments weren't both visible at
compile-time. And since it's IO conversion, the cost of the check should
be small compared to the cost of the call.
You would have to choose whether to use it for all vararg calls, or
only *printf/scanf. The former might be bad but might be good. The
latter would have less impact, but is one more thing to implement and
support, or really one and a half: some magic, pehaps an attribute, to
distinguish the special targets, plus the special mechanism.

As an alternative to actually passing to *printf/scanf, you could have
a preliminary format-validate function that is auto-called for
variable formats, perhaps only once per value (e.g. hoisted above a
loop), equivalent to the check now done at compile time by gcc.

The complete type includes the length of strings (char arrays); this
information won't be easily available at the call site in many cases.
If you leave that part out, and only check %d-to-int etc., it's easy
enough, but not quite complete.

And of course *printf/scanf is only a small part of the problem; there
are still zillions of other places in C you can easily cause UB, most
of which can't be (nearly) fixed so easily. This seems kind of like
putting a blade guard on one-eighth of the blade; it will help in some
cases, but can give a false sense or at least suggestion of safety
that may hurt in more cases than it helps. If you want a safe HLL,
even a safe efficient HLL, C is not a very good place to start.

And since languages that are safe are easily enough available and in
most cases easily enough interoperable with C, why waste the effort?

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 1 '07 #4

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

Similar topics

7
by: Steve Jorgensen | last post by:
Hi all, I've been using scalar functions as a way to perform some complex data transformation operations, and I've noticed that scalar functions reaaaaalllllyyyy sloooowwwwww thiiiiiings...
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
4
by: cjl | last post by:
As a learning exercise, I am trying to write a web-based version of 'drawbot' in PHP. See: http://just.letterror.com/ltrwiki/DrawBot I am interested in hearing ideas about how to approach...
25
by: Nicholas Parsons | last post by:
Howdy Folks, I was just playing around in IDLE at the interactive prompt and typed in dir({}) for the fun of it. I was quite surprised to see a pop method defined there. I mean is that a...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
4
by: Stephan Rose | last post by:
Hi everyone, I have a little problem I'm not sure what to do about or if anything can even be done about it. Situation is as follows: I had previous implemented a class called Scalar that...
1
by: pitjpz | last post by:
We have moved our Database to another server. The server it was on used SQL 4 and the new one its on now uses SQL5 the only problem we can find is that when you attempt to delete a record from...
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: 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
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.