Following program is erroneous as I am passing a float as int. The
output is
---
|
V
Int f is 0 float f is 0.000000
[return value 32 error number 0]
float f is 3.000000 int f is 0
[return value 33 error number 0]
---
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
The error should be contained only in first value as it will try to
read float as integer.
Is it totally unpredictable?
#include <stdio.h>
#include <errno.h>
extern int errno;
int main()
{
int n;
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
printf("[return value %d error number %d]\n",n,errno);
/* Print f as float and then int */
n = printf("\nfloat f is %f int f is %d \n",f,f);
printf("[return value %d error number %d]\n",n,errno);
return 0;
} 8 2671
quarkLore wrote:
Following program is erroneous as I am passing a float as int. The
output is
---
|
V
Int f is 0 float f is 0.000000
[return value 32 error number 0]
float f is 3.000000 int f is 0
[return value 33 error number 0]
---
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
The error should be contained only in first value as it will try to
read float as integer.
Once any aspect of the behaviour is undefined, so is the complete
behaviour. You're not even sure to the text that separates the
numbers.
Is it totally unpredictable?
For a specific implementation, you may be able to look at how printf
is written, or figure out how it might have been written. Consider how
the va_ macros in <stdarg.hmust work, and consider that printf
typically uses the same method to access its arguments.
But as far as standard C is concerned, yes, it is totally
unpredictable.
quarkLore said:
Following program is erroneous as I am passing a float as int. The
output is
---
|
V
Int f is 0 float f is 0.000000
[return value 32 error number 0]
float f is 3.000000 int f is 0
[return value 33 error number 0]
---
In the first print why is the second value not right?
Because your program is erroneous, and exhibits undefined behaviour as a
consequence. Once you break the rules, C no longers defines what your
program does.
I am using 'gcc
version 3.2.2' on Linux.
The error should be contained only in first value as it will try to
read float as integer.
C doesn't guarantee that.
>
Is it totally unpredictable?
From the point of view of the C Standard, yes. Whether the results are
predictable for a given implementation depends on the implementation.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
On 8 Feb 2007 21:46:51 -0800, "quarkLore" <ag************ *@gmail.com>
wrote:
>Following program is erroneous as I am passing a float as int. The output is
---
|
V Int f is 0 float f is 0.000000 [return value 32 error number 0]
float f is 3.000000 int f is 0 [return value 33 error number 0] ---
In the first print why is the second value not right? I am using 'gcc version 3.2.2' on Linux. The error should be contained only in first value as it will try to read float as integer.
Is it totally unpredictable?
As far as the C Standard is concerned, yes.
>
#include <stdio.h> #include <errno.h>
extern int errno;
int main() {
int n;
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
printf("[return value %d error number %d]\n",n,errno);
/* Print f as float and then int */
n = printf("\nfloat f is %f int f is %d \n",f,f);
printf("[return value %d error number %d]\n",n,errno);
return 0; }
You shouldn't need any help from tools to tell you that undefined
behavior of this sort is asking for trouble. Nevertheless, here's what
PC-lint says:
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006
--- Module: stdc.c (C)
_
extern int errno;
stdc.c(4) : Info 762: Redundantly declared symbol 'errno' previously
declared at line 68, file errno.h
_
n = printf("\nInt f is %d float f is %f\n",f,f);
stdc.c(11) : Warning 559: Size of argument no. 2 inconsistent with
format
_
n = printf("\nfloat f is %f int f is %d \n",f,f);
stdc.c(14) : Warning 559: Size of argument no. 3 inconsistent with
format
Best regards
--
jay
"quarkLore" <ag************ *@gmail.comwrot e:
[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
In addition to what the others wrote (it's UB; all bets are off), note
that there are very real reasons why printf() could get confused here.
Assume, for example, that ints are smaller than floats. In our
hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char *
is 4 bytes. What gets passed to printf() is:
[4 bytes char *] [4 bytes float] [4 bytes float]
What printf() could do is:
Read the first 4 bytes, and decode the string that resides at that
address.
See a %d specifier. Decide to read 2 bytes.
Read the next 2 bytes that were passed to it, and print them as if
they were an int.
See a %f specifier. Decide to read 4 bytes.
Read the next 4 bytes that were passed. If you've counted along,
you'll have noticed that this is not a float, but the second half
of the first float plus the first half of the second, glued together
the wrong way.
Print this chimaera-float as if it were a real one.
And that's just the innocuous version, where you're just getting
semi-random garbage printed. You _could_ get it to read a trap value and
crash. You could confuse printf()'s internal workings so badly that from
then on, even properly passed values get printed wrong. IOW, there are
quite real reasons why this has undefined behaviour.
Richard
On Feb 9, 4:28 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"quarkLore" <agarwal.prat.. .@gmail.comwrot e:
[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
In addition to what the others wrote (it's UB; all bets are off), note
that there are very real reasons why printf() could get confused here.
Assume, for example, that ints are smaller than floats. In our
hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char *
is 4 bytes. What gets passed to printf() is:
Thanks for the response I know all that.
@Richard Bos: I stated that I am using Linux. It uses 4 byte integer,
float and char * so the assumption and conclusions hold invalid in
this particular case. The plausible explanation looks right if
assumption is satisfied
I think Richard Heathfield is right. Hence I should post this to Linux
platform specific forum.
On Feb 9, 5:28 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"quarkLore" <agarwal.prat.. .@gmail.comwrot e:
[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
In addition to what the others wrote (it's UB; all bets are off), note
that there are very real reasons why printf() could get confused here.
Assume, for example, that ints are smaller than floats. In our
hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char *
is 4 bytes. What gets passed to printf() is:
[4 bytes char *] [4 bytes float] [4 bytes float]
Ummm, no. Parameters to variadic functions (like printf) always
undergo the usual argument promotions. You can't pass a float to
printf: It always gets converted to a double.
Regards,
-=Dave
"quarkLore" <ag************ *@gmail.comwrit es:
On Feb 9, 4:28 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>"quarkLore" <agarwal.prat.. .@gmail.comwrot e:
[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
In addition to what the others wrote (it's UB; all bets are off), note that there are very real reasons why printf() could get confused here.
Assume, for example, that ints are smaller than floats. In our hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char * is 4 bytes. What gets passed to printf() is:
Thanks for the response I know all that.
@Richard Bos: I stated that I am using Linux. It uses 4 byte integer,
float and char * so the assumption and conclusions hold invalid in
this particular case. The plausible explanation looks right if
assumption is satisfied
<OT>
Linux does not guarantee that char* is 4 bytes; I use Linux systems on
which all pointers are 8 bytes.
</OT>
--
Keith Thompson (The_Other_Keit h) 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.
"Dave Hansen" <id**@hotmail.c omwrote:
On Feb 9, 5:28 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"quarkLore" <agarwal.prat.. .@gmail.comwrot e:
[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
In addition to what the others wrote (it's UB; all bets are off), note
that there are very real reasons why printf() could get confused here.
Assume, for example, that ints are smaller than floats. In our
hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char *
is 4 bytes. What gets passed to printf() is:
[4 bytes char *] [4 bytes float] [4 bytes float]
Ummm, no. Parameters to variadic functions (like printf) always
undergo the usual argument promotions. You can't pass a float to
printf: It always gets converted to a double.
*Shrug* Assume a double is also 4 bytes, then. Or assume an int is 4
bytes and a double 6, or 8. The point remains the same.
Richard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Keith Doyle |
last post by:
I'm curious if setvbuf behavior is known to be undefined with regards
to effects it may have on the file pointer. I've found that the
following code works different on AIX 4.3.3 xlc and FreeBSD gcc 2.95.2
19990314 than it does on SCO 5.0.4 cc or Linux gcc egcs-1.1.2 19991024.
This is a bit curious as here's a gcc example that works one way and
one that works the other, and I would think the relevant code is in
libc/glibc.
This is what...
|
by: E. Robert Tisdale |
last post by:
In the context of the comp.lang.c newsgroup,
the term "undefined behavior" actually refers to
behavior not defined by the ANSI/ISO C 9 standard.
Specifically, it is *not* true that "anything can happen"
if your C code invokes "undefined behavior".
Behavior not defined by the ANSI/ISO C 9 standard
may be defined by some other standard (i.e. POSIX) or
it may be defined by your compiler,
your operating system or your machine architecture.
|
by: Mantorok Redgormor |
last post by:
#include <stdio.h>
struct foo { int example; struct bar *ptr; };
int main(void)
{
struct foo baz;
baz.ptr = NULL; /* Undefined behavior? */
return 0;
|
by: Nitin Bhardwaj |
last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a
student and have been doing & studying C programming for the last 4
years).....and also a regular reader of "comp.lang.c"
I don't have a copy of ANSI C89 standard,therefore i had to post this
question:
What is the difference between "unspecified" behaviour & "undefined"
behaviour of some C Code ??
|
by: jimjim |
last post by:
Hello,
#include <stdio.h>
int main(int argc, char *argv)
{
int x = 1;
printf("%d %d %d\n", ++x, x, x++);
return 0;
}
| |
by: deepak |
last post by:
Using 'char' as an array index is an undefined behavior?
|
by: bbaale |
last post by:
Why would this code cause undefined behavior?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
|
by: Chris Thomasson |
last post by:
I was wondering if the 'SLINK_*' and 'SLIST_*' macros, which
implement a simple singly-linked list, will produce _any_ possible
undefined behavior:
____________________________
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
|
by: Franz Hose |
last post by:
the following program, when compiled with gcc and '-std=c99', gcc says
test.c:6: error: jump into scope of identifier with
variably modified type
that is, it does not even compile.
lcc-win32, on the other hand, reports
Warning test.c: 7 unreachable code
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |