Hi,
Whenever we type in this code
int main()
{
printf("%f",10) ;
}
we get an error. We can remove that by using #pragma directive t
direct that to the 8087. Even after that the output is 0.00000 and no
10.0000. Can anybody tell me why it is like that and why typecasting i
not done in this case?
-
andynai
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------
Nov 14 '05
63 3422
jacob navia wrote: "Dan Pop" <Da*****@cern.c h> a écrit dans le message de news:cb******** **@sunnews.cern .ch...
OTOH, lcc-win32 silently accepts printf("%d\n", "foo"), which is very bad, considering the relative positions of the D and S keys on most keyboard layouts (i.e. it is a fairly frequent mistake to type d when you mean s).
I pondered a long time about that one, since it is perfectly legal to do:
char *p; ...
printf("The address is %d\n",p);
The specifier for a pointer is "%p" and it expects a (void *) argument.
This supposedly "legal" line should be
printf("The address is %p\n", (void *)p);
Please stop misleading people who might think you know what you're
talking about. specially in debugging code.
Granted, this is weird, but how to discriminate between legal and wrong usage?
You "legal" usage is _wrong_. There is no need to discriminate between
your error and that which is wrong.
The expression
printf("the address is: 0x%x\n",p);
where p is some pointer appears in several million lines in
existing code.
The warnings can become a nuisance and people would stop
using this feature. Personally I think warnings should be
kept to the essential ones, warnings that would uncover a
possible error.
Strictly speaking you should use %p, but I have almost
never seen it in debugging code, where this conversion is
used.
To the contrary of your expectations, I work to make a usable
compiler, not one that will please the purists around c.l.c
In article <cb**********@n ews-reader1.wanadoo .fr>, ja***@jacob.rem comp.fr
says... I pondered a long time about that one, since it is perfectly legal to do:
char *p; ...
printf("The address is %d\n",p);
specially in debugging code.
I just lost any faith I might have had in lcc-win32. Thanks for
the flashing warning label.
In <2k************ @uni-berlin.de> Martin Ambuhl <ma*****@earthl ink.net> writes: andynaik wrote:
Hi, Whenever we type in this code int main() { printf("%f",10) ; } we get an error. We can remove that by using #pragma directive to direct that to the 8087. Even after that the output is 0.00000 and not 10.0000. Can anybody tell me why it is like that and why typecasting is not done in this case?? Because you forgot #include <stdio.h> You also forgot to terminate the last line of output with an end-of-line character. ^^^^^^^^^^^
^^^^^^^^^
In C, it is called new-line character, even if no line follows. You must
be confusing with the end-of-line *indicator* (which needs not be a
character).
The first mistake, omission of the prototype for a variadic function, is always an error. The second is an error in code designed to be portable, and may result in behavior you were not expecting.
Neither of then having anything to do with the real cause of the OP's
problem: type mismatch in a printf call, which will continue to manifest
even after including <stdio.h> and adding the new-line character.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
On Tue, 29 Jun 2004, jacob navia wrote:
jn>The expression
jn>
jn>printf("the address is: 0x%x\n",p);
jn>
jn>where p is some pointer appears in several million lines in
jn>existing code.
jn>
jn>The warnings can become a nuisance and people would stop
jn>using this feature. Personally I think warnings should be
jn>kept to the essential ones, warnings that would uncover a
jn>possible error.
jn>
jn>Strictly speaking you should use %p, but I have almost
jn>never seen it in debugging code, where this conversion is
jn>used.
jn>
jn>To the contrary of your expectations, I work to make a usable
jn>compiler, not one that will please the purists around c.l.c
That has nothing to do with purism. Ever cared to work on a machine where
sizeof(void *) > sizeof(int)? The nearest to correct thing to do if you
happen to have a printf() without %p would be to use %lx and cast the
pointer to an unsigned long.
harti
In <2e************ *************** ***@news.thenew sgroups.com> andynaik <an************ *@mail.codecomm ents.com> writes: Whenever we type in this code int main() { printf("%f",10 ); } we get an error. We can remove that by using #pragma directive to direct that to the 8087. Even after that the output is 0.00000 and not 10.0000. Can anybody tell me why it is like that and why typecasting is not done in this case??
Since type casting is a programming construct, it is you who have to
explain us why you have omitted it from your program.
If your question is why 10 wasn't *automatically converted*, which is
something *completely* different from type casting, the answer is that
it is located in the variable argument part of the printf call and,
therefore, it is subject to the default argument promotions, *only*.
In other words, although the compiler is allowed to have special
knowledge about printf, it is not required to have such knowledge or to
use it for fixing any mismatch between the format string and the rest
of the arguments.
Furthermore, because printf is a variadic function, you *must* provide
a correct declaration for it, before calling it. So, including <stdio.h>
is NOT optional in programs using printf and/or scanf. And a new-line
character in the format string wouldn't hurt, either: the shell prompt
*may* overwrite the last line of output, otherwise. Or be appended to it,
which, even if less destructive, is still far from desirable.
As a last remark, NOW it is the right time to get used to properly
indenting your code.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <Xn************ *************** @212.27.42.65> Emmanuel Delahaye <em**********@n oos.fr> writes: In 'comp.lang.c', andynaik <an************ *@mail.codecomm ents.com> wrote:
Hi, Whenever we type in this code int main() { printf("%f",10) ; } we get an error. Compiling MAIN.C: Warning MAIN.C 4: Call to function 'printf' with no prototype Warning MAIN.C 5: Function should return a value Linking EXE\PROJ.EXE:
You're sorely mistaken if you believe that compiler diagnostics *by
themselves* prove anything at all.
This code invokes an undefined behaviour (UB).
Which means that no diagnostic is required. Furthermore, even if all the
diagnostics of your compiler are fixed, the program is still as broken
as it was in the first place.
It is mandatory to supply a prototype when using a variadic function. Add
#include <stdio.h>
That said, 10 is a int. "%f" is expecting a double. If you want to printf a double, use 10.0, or the (double) typecast.
Did you actually try to understand the OP's question? He *knows* that
"%f" is expecting a double and he is asking why the compiler doesn't
convert 10 to double. WHERE are you addressing this question in your
reply?
Finally, some old Borland C compilers anre buggy and forget to link the floating point library un such a case. This little hack can help:
Since when are some old Borland C compilers topical to this newsgroup?
Aren't they properly handled by the FAQ?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <cb**********@n ews-reader3.wanadoo .fr> "jacob navia" <ja***@jacob.re mcomp.fr> writes: "Alex Fraser" <me@privacy.net > a écrit dans le message de news:2k******* ****@uni-berlin.de... "andynaik" <an************ *@mail.codecomm ents.com> wrote in message news:2e******** *************** *******@news.th enewsgroups.com ... > Whenever we type in this code > int main() > { > printf("%f",10) ; > } > we get an error. We can remove that by using #pragma directive to > direct that to the 8087.
Means nothing to me (compiler specifics are off-topic here).
> Even after that the output is 0.00000 and not 10.0000. Can anybody tell > me why it is like that and why typecasting is not done in this case??
Read section 15 in the FAQ. Post back if you still have questions. http://www.eskimo.com/~scs/C-faq/s15.html The FAQ should mention that some compilers DO test the arguments for sprintf/printf/fprintf etc for validity.
But they *still* don't perform any automatic conversions on the arguments,
to make them match the format string (other than the default argument
promotions).
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
"Harti Brandt" <br****@dlr.d e> a écrit dans le message de
news:20******** ***********@bea gle.kn.op.dlr.d e... On Tue, 29 Jun 2004, jacob navia wrote: That has nothing to do with purism. Ever cared to work on a machine where sizeof(void *) > sizeof(int)?
In the next implementation of lcc-win32 that will be the case (64 bit
machine).
In THAT environment I will issue the warning for all pointer/int
conversions.
In an environment where sizeof(int)==si zeof(void*) many people
(including me) write printf("pointer value=%#x\n",p) ; to debug
a piece of code. The compiler spitting you hundreds of warnings
would only have the consequence of people IGNORING all warnings.
lcc-win32 has several levels of warnings. After this discussion I have added
a warning when the warning level is higher than normal for all
implicit pointer/int conversions in the printf formats.
It is a pity that people here like to have an atmosphere of
aggresivity that is very boring.
In any case this discussion was positive for me (and lcc-win32).
I have been able to improve lcc-win32 a bit.
Thanks for your time.
"Randy Howard" <ra*********@FO OverizonBAR.net > a écrit dans le message de
news:MP******** *************** *@news.verizon. net... In article <cb**********@n ews-reader1.wanadoo .fr>, ja***@jacob.rem comp.fr says...
I just lost any faith I might have had in lcc-win32. Thanks for the flashing warning label.
Mr Howard
It is a pity that people here like to have an atmosphere of
aggresivity that is so boring.
In any case this discussion was positive for me (and lcc-win32).
I have been able to improve lcc-win32 a bit.
Thanks for your time. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kapil Khosla |
last post by:
Hi,
I have been trying to understand this concept for quite sometime now
somehow I am missing some vital point. I am new to Object Oriented
Programming so maybe thats the reason.
I want to understand what is typecasting in C++. Say I have a Base
class and a Derived class, I have a pointer to an object to each.
Base *ba = new Base;
Derived *de = new Derived;
|
by: Nicolay Korslund |
last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody
help me with the rules for when the this operator is invoked?
In the class 'Test' in the code below, the typecast operator returns a
'mytype'. In main() I use the operator on 'Test'-class objects. If
'mytype' is a pointer the operator works fine. But if I instead try
to call an overloaded operator, the typecast operator is not invoked,
and I get a compiler error...
|
by: Arun Prasath |
last post by:
Hi all,
I have the following question regd pointer typecasting. Is the
following type of pointer typecasting valid?
#define ALLOC(type,num) ((type *)malloc(sizeof(type)*num))
/*begin code*/
|
by: Vinod Patel |
last post by:
I have a piece of code : -
void *data;
......
/* data initialized */
......
struct known_struct *var = (struct known_struct*) data; /*typecasting*/
How is this different from simple assignment.
int b = some_value;
|
by: jdm |
last post by:
In the sample code for the SortedList class, I see the use of a string
typecasting macro consisting of a single letter "S". i.e.:
Sortedlist->Add(S"Keyval one", S"Item one");
Now I have deduced that the "S" can be replaced with (String __gc *) and the
code will compile and run just fine. But what I can't find is what exactly
Microsoft calls these macros (I have also seen "L" used the same way) and
where they are all documented. I...
| |
by: Raghu |
last post by:
Hello All,
I need some help regarding overloading operation.
Is there any way to overload typecasting?
I mean if i have a piece of code as below.
int a = 2:
float b;
b = (float)a;
|
by: Abhishek |
last post by:
why do I see that in most C programs, pointers in functions are
accepted as:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing
to a void and why is it done?
Aso, what happens when we typecast a pointer to another type. say for
example int *i=(char *)p;
under different situations? I am kind of confused..can anybody clear
this confusion by clearly...
|
by: vivekian |
last post by:
Hi,
This is the part of the code am trying to compile to :
void Server::respondToClient ( std::string response )
{
....
....
if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
(struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
|
by: bwaichu |
last post by:
What is the best way to handle this warning:
warning: cast from pointer to integer of different size
I am casting in and out of a function that requires a pointer type. I
am casting an integer as a pointer, but the pointer is 8 bytes while
the integer is only 4 bytes.
Here's an example function:
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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: 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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |