Solved

For Jhandby

Posted on 1998-10-08
1
177 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now