• C

float and long data type

Dear Experts,

Seed = 0.000000
Press any key to continue . . .

The above are the results I receive after compiling the program below.  Can someone explain why I am getting the number 0.000000?  This is for a school assignment.  The objective here is to use Random function that was given to us, and send in the value "12345L".  Then output the result!  

The person with the most understandable explaination for a beginning programmer will be the one awarded with points.

Thanks for the response!

/*Below program is compiled and executed in Visual C++.NET*/
#include <stdio.h>
#include <stdlib.h>

float Random(long *seed);
/* Defining function Random*/
float Random(long *seed)
      float temp=0 ; //initialized to 0

int main()
      float rn;
      long seed = 12345L;
      long *seedptr;
      seedptr = &seed;
      printf("Seed = %f \n", &seed);
      //printf("Seed = %f \n", *seedptr);
      //rn = Random(seedptr);

      //printf("The random number generated is: %f \n", rn);
Who is Participating?
Sjef BosmanConnect With a Mentor Groupware ConsultantCommented:
Printf cannot check whether the parameters passed are correct. The %f tells printf to get a float from its the stack, but you pushed an address of a long on it. A correct use of printf would be
    printf("Seed = %ld\n", seed)
    printf("Seed = %ld\n", *seedptr)
Just a comment Charley

Be aware that you're just printing the address of a local variable.
You're not calling the Random function.

(Note that the lines are commented)

Hope this helps

Charley420Author Commented:
I had failed to mention in the original post, I commented out all of the other lines because I wanted to make sure I was passing in the correct value into the Random function.  Thanks for your response!

I was searching on websites and non of them mentioned "%ld".   I understand that "%d" stands for signed integer written in decimal and "%l" specifies the variable is a long int*, but no one ever mentioned you concatenate the variables l and d together.  Thanks for your response!

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

Sjef BosmanGroupware ConsultantCommented:
Just some hints/links:
- search Google on "manual printf c", the second link is
- http://www.gnu.org/software/libc/manual/html_node/ and search for printf
- http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html#Formatted%20Output and click on Table of Output Conversions or Table of Integer Conversions
- the latter sends you to http://www.gnu.org/software/libc/manual/html_node/Integer-Conversions.html#Integer%20Conversions
- look on the second screen, where you will find anything on "type modifier"

And why a B?
Charley420Author Commented:
I fat fingered :o/....on the grading
I appreciated the links!

Sjef BosmanGroupware ConsultantCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.