Home > Software engineering >  Function output is only showing as a value of 16
Function output is only showing as a value of 16

Time:09-03

When I run this and input 2 values, it always only ever outputs the value of 16. I never receive anything else. Please can someone show me where I've gone wrong with this.

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

#define result

int higher(int num1 , int num2);

int main (int argc, char *argv[]) {
    int val1;
    int val2;
    int num;

    num = higher(val1, val2);

    printf("Enter two numbers: ");
    scanf("%d %d", &val1, &val2);
    printf("The higher number is: %d\n", num);
    return EXIT_SUCCESS;
}

int higher(int num1 , int num2) {
    // Based on what values are called from the function
    // num1 or num2 should reflect as the highest.
    int result;

    if (num1 <= num2)
        result = num2;
    else
        result = num1;

    return result;
}

CodePudding user response:

You are calling the function before getting the values in scanf it should be like this.

printf("Enter two numbers: ");
scanf("%d %d", &val1, &val2);
num = higher(val1, val2);
printf("The higher number is: %d\n", num);
  •  Tags:  
  • c
  • Related