-2

It's an extremely simple code, when I run it, it lets me enter the int, but when I enter it, the program crashes. I tried putting a printf right after the scanf to check if the program reads the int correctly, it seems it doesn't. I can't output the integer that I input.

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

void coolfunction(int x, int *y)
{
    *y = exp(x);
}

int main()
{
    int n;
    double result = 0;

    printf("Enter an integer: ");
    scanf("%d", n);

    coolfunction(n, &result);

    printf("\n e^%d = %lf \n", n, result);
    return 0;
}
cognophile
  • 802
  • 11
  • 25

1 Answers1

1

Here's a version using a double parameter:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void coolfunction(int x, double *y)
{
    *y = exp(x);
}

int main()
{
    int n = 0, scanned = 0;
    double result = 0;

    printf("Enter an integer: ");
    scanned = scanf("%d", &n);
    if (scanned < 1) {
        fprintf(stderr, "value entered not a valid integer\n");
        exit(EXIT_FAILURE);
    }
    coolfunction(n, &result);
    printf("\n e^%d = %g \n", n, result);
    return 0;
}

You also missed the & in your scanf call: the function needs to know the address of your variable, not its value.

Patrick Bucher
  • 1,302
  • 2
  • 14
  • 36