Solved

For Jhandby

Posted on 1998-10-08
1
195 Views
Last Modified: 2010-04-15
Your point for the comment in "simple printf question".
0
Comment
Question by:anik
1 Comment
 

Accepted Solution

by:
JHandby earned 10 total points
ID: 1253428
Here's that comment/answer again. Thanks for the points.


------------------8<---------------


Anik,


The reason that the compiler can't convert the float to an int automatically is to do with the fact that printf is declared to take a variable number of arguments. If you look in stdio.h, you'll probably see printf declared something like this:

int printf(const char *, ...);

The first argument, of type const char *, is the printf format string. This means that the compiler will be able to check the type of the first argument at compile time. The "..." indicates to the compiler that there will be zero or more arguments supplied, but that the compiler cannot make any assumptions about how many there will be, or what type they will be. This makes sense, if you think about how printf gets used:

printf("Just a string");
printf("%d green bottles", num_bottles);
printf("%d / %d = %lf", num, denom, (double)num / denom);

These are all valid printf calls (at least I hope they are :-) ), with different numbers and types of arguments. There's no way that could be type-checked or automatically converted at compile-time.

Because the compiler doesn't know how many arguments there'll be, the printf function itself is responsible for getting hold of the arguments passed to it at runtime (which is basically done internally by getting a pointer to the first argument, and moving it through memory to point to each extra argument in turn). The only information available to printf to tell it what type the arguments are and how many there should be is what you supply in your format string. So, if you tell printf to expect an int (by using "%d" in your format string), then it will try to get hold of the next argument and pull it out as an int. If you've actually passed in a float, there'll be trouble...

It's possible to create all sorts of fantastic bugs by using printf wrong :-)

For more information on variable-argument functions, look at  stdarg.h or varargs.h, depending on what flavour of compiler you're using.



Hope this helps



Jason


0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 145
Microsoft vcsetup.exe and vcssetup.exe 8 422
Which checksum is this? 7 142
rhel6 C system() call to zip to archive has problems 25 241
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question