# Programing in C : DIGO

**URL:** <https://boards.straightdope.com/t/programing-in-c-digo/90242>\
**Category:** Factual Questions\
**Created:** [October 29, 2001, 5:48am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242 "2001-10-29T05:48:40Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![wolfstu](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@wolfstu](https://boards.straightdope.com/u/wolfstu)\
**Post date:** [October 29, 2001, 5:48am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/1 "2001-10-29T05:48:40Z")

</div>

**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:

```auto

#include <stdio.h>
void main
{
float p;
p=1
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.

---

<div class="post-metadata">

**Author:** ![SenorBeef](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senorbeef/32/2220_2.png) [@SenorBeef](https://boards.straightdope.com/u/SenorBeef)\
**Post date:** [October 29, 2001, 6:03am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/2 "2001-10-29T06:03:55Z")

</div>

Try p=1;

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

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 29, 2001, 6:11am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/3 "2001-10-29T06:11:38Z")

</div>

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

```auto

#include <stdio.h>

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

```

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 29, 2001, 6:12am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/4 "2001-10-29T06:12:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DarrenS](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@DarrenS](https://boards.straightdope.com/u/DarrenS)\
**Post date:** [October 29, 2001, 6:13am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/5 "2001-10-29T06:13:27Z")

</div>

Wolfstu - this _compiled_ ?! 😕

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

p=1.000000

which is what you wanted (I hope).

---

<div class="post-metadata">

**Author:** ![DarrenS](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@DarrenS](https://boards.straightdope.com/u/DarrenS)\
**Post date:** [October 29, 2001, 6:15am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/6 "2001-10-29T06:15:29Z")

</div>

Simulpost 😉

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

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 29, 2001, 6:16am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/7 "2001-10-29T06:16:50Z")

</div>

> [@](#):
>
> \*Originally posted by DarrenS \*  
> \*\*Simulpost 😉
> 
> 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_.”

---

<div class="post-metadata">

**Author:** ![DarrenS](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@DarrenS](https://boards.straightdope.com/u/DarrenS)\
**Post date:** [October 29, 2001, 6:29am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/8 "2001-10-29T06:29:56Z")

</div>

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](http://www.ioccc.org/)

Juuuust kidding 😃

---

<div class="post-metadata">

**Author:** ![wolfstu](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@wolfstu](https://boards.straightdope.com/u/wolfstu)\
**Post date:** [October 29, 2001, 7:09am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/9 "2001-10-29T07:09:34Z")

</div>

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

```auto

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:

```auto

#include <stdio.h>
void main()
{
float p;
p=1;
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]

---

<div class="post-metadata">

**Author:** ![Bill\_H](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@Bill\_H](https://boards.straightdope.com/u/Bill_H)\
**Post date:** [October 29, 2001, 7:18am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/10 "2001-10-29T07:18:51Z")

</div>

try p=1.0;

---

<div class="post-metadata">

**Author:** ![DarrenS](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@DarrenS](https://boards.straightdope.com/u/DarrenS)\
**Post date:** [October 29, 2001, 7:26am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/11 "2001-10-29T07:26:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 29, 2001, 4:08pm UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/12 "2001-10-29T16:08:56Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![yabob](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/yabob/32/2821_2.png) [@yabob](https://boards.straightdope.com/u/yabob)\
**Post date:** [October 29, 2001, 4:38pm UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/13 "2001-10-29T16:38:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wolfstu](https://avatars.discourse-cdn.com/v4/letter/w/d6d6ee/32.png) [@wolfstu](https://boards.straightdope.com/u/wolfstu)\
**Post date:** [October 30, 2001, 12:31am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/14 "2001-10-30T00:31:34Z")

</div>

> [@](#):
>
> You may be confusing printf with scanf, as DarrenS suggested, which requires a pointer so it can write data to the variable.

Right on.

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

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

---

<div class="post-metadata">

**Author:** ![SDP](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@SDP](https://boards.straightdope.com/u/SDP)\
**Post date:** [October 30, 2001, 2:24am UTC](https://boards.straightdope.com/t/programing-in-c-digo/90242/15 "2001-10-30T02:24:47Z")

</div>

> [@](#):
>
> \*Originally posted by friedo \*  
> \*\ ***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.  
> \*\*

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.
