Trying to make a program to take and print the natural log of a number input

AI Thread Summary
The discussion revolves around a user's issue with a C program designed to calculate the natural logarithm of a user-input number. The user reports that the program returns incorrect values, specifically a large number when inputting 1, which should yield 0 since ln(1) = 0. The source code provided contains a misunderstanding regarding the use of the ampersand (&) symbol in C. It is clarified that the ampersand is necessary for the `scanf` function to reference the variable's memory address, allowing it to store the input value. However, for the `printf` function, the ampersand should be omitted, as it requires the actual value rather than the address. After removing the ampersands from the `printf` statements, the user finds that the program works correctly. This highlights the importance of understanding variable references in C programming, particularly when using input and output functions.
DomBrown2406
Messages
3
Reaction score
0
I'm relatively new to C programming and I am trying to make a program to take and print the natural log of a number input by the user but for some reason my program always returns incorrect answers. For example I entered 1 as my number, and the answer came back as some ridiculously big value which is clearly wrong as I know that ln 1 = 0.
My source code is given below, any help would be greatly appreciated :)

Code:
#include <stdio.h>
#include <math.h>

int main(void)
{
    float in;
    float out;    
    do {
    
    printf("Enter the number to take ln of...\n");
    scanf("%f", &in);
    out = log(in);
    printf("Ln of %f is %f\n", &in, &out);
    printf("If you want to do this again press 1, if not press any other number\n");
    getchar();}
    while(1==1);
}
 
Last edited:
Technology news on Phys.org


DomBrown2406 said:
printf("Ln of %f is %f\n", &in, &out);

What & means?

Use [noparse]
Code:
[/noparse] tags to format your source code:

Code:
#include <stdio.h>
#include <math.h>

int main(void)
{
   float in;
   float out; 
   do {
      printf("Enter the number to take ln of...\n");
      scanf("%f", &in);
      out = log(in);
      printf("Ln of %f is %f\n", &in, &out);
      printf("If you want to do this again press 1, if not press any other number\n");
      getchar();
   } while(1==1);
}
 
The & sign is how my compiler knows what variable i want to use for %f etc.
 
Not exactly. & doesn't mean what variable, & means where the variable is. It is necessary when you use scanf, as scanf needs a reference to be able to put the value in the variable, but printf needs just a value.
 
Ok, like I said I am relatively new, but I removed those & signs from printf and it works fine now, thanks :)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top