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

unwanted implicit casts

Hello,

Is there anyway to detect when scanf implicitly casts a value? If I
have a scanf call which expects a float and is passed a double I
believe that testing the scanf call with if(scanf("%f",&val) == 1)
would be true, but the double would have been truncated to a float. Is
there anyway to flag this cast? Thanks
Nov 13 '05 #1
5 2776
Hi,
"Andrew" <an***********@ca.com> wrote in message
news:65**************************@posting.google.c om...
Is there anyway to detect when scanf implicitly casts a value? If I
have a scanf call which expects a float and is passed a double I
believe that testing the scanf call with if(scanf("%f",&val) == 1)
would be true, but the double would have been truncated to a float. Is
there anyway to flag this cast? Thanks


No. Calls to printf/scanf, unfortunately, are not type-safe.
However, some compilers can help, and will verify the matching
of a format string with the type of the actual parameters.

So one has to be extra careful with these functions.
( C++ provides increased safety here, with its streaming
operations: cin >> val; will always behave correctly,
regardless of the type of val... )

Regards,
--
http://www.post1.com/~ivec <> Ivan Vecerina
Nov 13 '05 #2
Andrew wrote:
Hello,

Is there anyway to detect when scanf implicitly casts a value? If I
have a scanf call which expects a float and is passed a double I
believe that testing the scanf call with if(scanf("%f",&val) == 1)
would be true, but the double would have been truncated to a float.


Firstly, scanf never expects a float. It can certainly expect a pointer to a
float. To give it a pointer to double in that circumstance is an error.

Secondly, there is no such thing as an implicit cast. Casts are, by
definition, explicit conversions.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #3
Richard Heathfield <do******@address.co.uk.invalid> wrote in message news:<bj**********@titan.btinternet.com>...
Andrew wrote:
Hello,

Is there anyway to detect when scanf implicitly casts a value? If I
have a scanf call which expects a float and is passed a double I
believe that testing the scanf call with if(scanf("%f",&val) == 1)
would be true, but the double would have been truncated to a float.


Firstly, scanf never expects a float. It can certainly expect a pointer to a
float. To give it a pointer to double in that circumstance is an error.

Secondly, there is no such thing as an implicit cast. Casts are, by
definition, explicit conversions.


Thanks. I understand that passing a pointer to a double to scanf when
it expects a pointer to a float would be an error, but even if in the
example above val is declared type float won't the call to scanf
truncate any value entered to a float? For instance if I have the
following code snippet

unsigned int num;

printf("\nEnter number: ");
scanf("%i",&num);

printf("\nThe answer is: %i\n", num);

If on a 32 bit machine the user enters 4294967300 The answer printed
would be 4? Will errno be set to ERANGE? I can not see that it is. Or
is there some other way to test that this overflow condition occured?
Thanks
Nov 13 '05 #4
>Richard Heathfield <do******@address.co.uk.invalid> wrote in message news:<bj**********@titan.btinternet.com>...
In article <65*************************@posting.google.com>
Andrew <an***********@ca.com> writes:
... I understand that passing a pointer to a double to scanf when
it expects a pointer to a float would be an error, but even if in the
example above val is declared type float won't the call to scanf
truncate any value entered to a float? For instance if I have the
following code snippet

unsigned int num;

printf("\nEnter number: ");
scanf("%i",&num);

printf("\nThe answer is: %i\n", num);

If on a 32 bit machine the user enters 4294967300 The answer printed
would be 4? Will errno be set to ERANGE? I can not see that it is. Or
is there some other way to test that this overflow condition occured?


The scanf() function family should in general not be used on
"non-sanitized" input. (Certain formats can be used, especially
if you are Dan Pop :-) , on certain streams with reasonable
safety.)

In particular, scanning 4294967300 with %i format gives undefined
behavior. In *my* stdio, the %i conversion actually uses strtol(),
so that you will get 2147483647 on a 32-bit-int machine with errno
set to ERANGE. (Note that %i expects a pointer to a *signed* int,
not an unsigned one.) On other systems, however, you will indeed
get the result described above.

If you have the sequence "4294967300" (including terminating '\0'
character) in an array, however, you can write:

unsigned long result;

result = strtoul(that_array, NULL, 10);

to force a base-10 conversion of the numeric value, with clamping
at ULONG_MAX. (Set base to 0 for a %i-like prefix-determines-base
conversion.) Note that strtoul() allows leading "-" prefixes;
if you wish to prohibit those, you must check the input yourself.

Reading arbitrary user input is one of the most difficult things
to do in C, or indeed in many other languages. The problem is not
so much the language -- although C often makes it unusually easy
to get something wrong here -- but rather with the fact that humans
find such inventive ways to present your program with unexpected
input. For instance, the five-year-old might hold down the 'b'
key (or drop a sausage on it) until it has repeated several hundred
thousand times.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #5
On 11 Sep 2003 10:11:20 -0700, an***********@ca.com (Andrew) wrote in
comp.lang.c:
Richard Heathfield <do******@address.co.uk.invalid> wrote in message news:<bj**********@titan.btinternet.com>...
Andrew wrote:
Hello,

Is there anyway to detect when scanf implicitly casts a value? If I
have a scanf call which expects a float and is passed a double I
believe that testing the scanf call with if(scanf("%f",&val) == 1)
would be true, but the double would have been truncated to a float.


Firstly, scanf never expects a float. It can certainly expect a pointer to a
float. To give it a pointer to double in that circumstance is an error.

Secondly, there is no such thing as an implicit cast. Casts are, by
definition, explicit conversions.


Thanks. I understand that passing a pointer to a double to scanf when
it expects a pointer to a float would be an error, but even if in the
example above val is declared type float won't the call to scanf
truncate any value entered to a float? For instance if I have the
following code snippet

unsigned int num;

printf("\nEnter number: ");
scanf("%i",&num);

printf("\nThe answer is: %i\n", num);

If on a 32 bit machine the user enters 4294967300 The answer printed
would be 4? Will errno be set to ERANGE? I can not see that it is. Or
is there some other way to test that this overflow condition occured?
Thanks


On what 32 bit machine? Did you think there was only one?

As far as this group is concerned, the result doesn't matter since the
behavior is undefined.

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #6

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

Similar topics

4
by: Simon Ford | last post by:
Hi All, I'm having trouble understanding exactly how I can do some specific implicit casting. There are two problems here; does anyone know what I should be doing? //---------- // (1)...
7
by: pnsteiner | last post by:
i want to use the << operator defined for ostream with an object that itself knows nothing of the operator, but is castable to ostream&. it seems that C++ doesn't do implicit casts before invoking...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
6
by: Gecko | last post by:
I would like to know if there is a way to stop the runtime from implicitly casting values. For exampel, I would like the following code to crash: byte someByte = 5; int someInt = someByte; I...
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
3
by: Ray Gardener | last post by:
I searched around but couldn't find any previous discussion on this, so... I have a macro that implements a common memory disposal function, e.g.: #define safe_free(void* pv) if(pv) {...
1
by: not_a_commie | last post by:
I have an Angle class that I store angles in. It's basically just a bunch of fancy functions for manipulating a double. It has implicit casts for converting to/from double. Due to the project...
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: 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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.