Programing in C : DIGO

Data In, Garbage Out

So here I was having all sort of difficulty with a specific C program I had to write. Every printed number was a zero. So I tried a few different things to fix the problem, to no avail. Then I wrote a little test program:

#include <stdio.h>
void main
float p;
printf ("p=%f", &p);

And guess what prints: “p=0.000000”
If I do the same thing with integers, I get “p=3905890”

Anybody know what’s up with this?

If it matters, the compiler’s called Dev C++. It’s never done this before.

Thanks for your help.

Try p=1;

Also, are you sure &p is the proper way to print a floater? Try %p.

printf doesn’t take pointers. The following works correctly on my machine:

#include <stdio.h>

int main () {
   float p;
   p = 1.0;
   printf("p=%f", p);

(And the same is true for using ints and %d in your printf, pass the variable, not a pointer.)

Wolfstu - this compiled ?! :confused:

Firstly - you need at least some parens for main -

void main()

The prototype for main is usually

int main(int argc, char* argv)

but I believe there’s some leniency there. As SenorBeef pointed out you also need a semicolon after each statement, viz.

p = 1;

Also, your printf is very questionable:

&p is the address of the variable p, i.e. (on Windows) a 32-bit pointer value. To print one of those (if that’s what you really want) you need a “%p” format specifier:

printf (“p=%p”, &p);

To print out a float, it’s %f as the format specifier, then just specify the variable, not its address:

printf (“p=%f”, p);

When I do this under Microsoft Visual C++ 6.0 I get


which is what you wanted (I hope).

Simulpost :wink:

Friedo - printf does handle pointers OK, the format specifier is “%p” as I mentioned above.

Yep - I shoulda been more precise, “printf doesn’t take pointers for %f.”

Ahhh I see what you meant friedo.

wolfstu - please at least satisfy my curiosity as to how your original program compiled - is Dev C++ a VERY lenient compiler, or did you retype the program from memory and make a couple of typos?

Keep it up and you might be ready to enter this

Juuuust kidding :smiley:

Well, I’m pretty sure the professor always insisted on those ampersands:

printf ("p=%f", **&**p)

But I tried it out without them, and got the same answer: 0.000000

And you’re right. I did type that from memory, sometime after midnight. Here’s what actually went through the compiler:

#include <stdio.h>
void main()
float p;
printf ("p=%f", p);

And I’m still getting “p=0.000000”. It doesn’t make any sense to me.

[aside]You wouldn’t believe how long I spent trying to debug the program I’m actually trying to write before I discovered this little quirk[/aside]

try p=1.0;

wolfstu - there’s definitely a place for ampersands, but that isn’t one of them. Not to be condescending, but could you or your professor be getting confused with scanf(), which does usually use ampersands?

I cut-pasted your program into Visual C++, and got p=1.000000 as expected. Either

(1) Visual C++ is doing something non-ANSI (any standards experts out there?)

(2) There’s something you’re not telling us, or

(3) Dev C++ is not behaving correctly.

wolfstu, when working with floats, some compilers will require you to use 1.0 instead of 1. I believe the ANSI standard requires this also. Gcc is compiling it correctly without the decimal point, so I’m not sure if that’s your problem, and that is the only compiler which I have available to me right now.

Ampersands are for making pointers to variables, & means “address of” when it comes right before a variable name. For %f and %d, and so on, printf does not want a pointer, it wants the variable itself.

You may be confusing printf with scanf, as DarrenS suggested, which requires a pointer so it can write data to the variable.

Another thing to remember is that, in spite of the letter choice, the “%f” format takes a double argument.

You might try:

printf(“p=%f”,(double) p);

On some machines, this might not make a difference, on some it might. It depends on the sizes and formats of the float and double types.

Right on.

Foolish. I wasted a lot of energy on that. The Universe will never repay me.

printf(“Thanks, %f”, wolfstu);

For what’s it’s worth, without looking over the ANSI standard itself, gcc with the -ansi and -pedantic flags on will compile “float p = 1;” without complaint 1.0 is to be preferred for matters of clarity, 1.0f if you are really intent on being clear.