0

I'm trying to create a function that takes 4 integers and returns the greatest int of them. However when I excute the code it takes "5" integers and returns the largest of the first 4. Idk why there is a fifth input, also there is a warning " Unintialized local vriable "d"

`

#include <stdio.h>
int max(int ,int , int , int );

int main() {
    
   int ans,a,b,c,f;
   ans=max(a,b,c,f);
   printf("%d",ans);
    
    return 0;
}
int max(int x,int y, int z, int k){
    
    int greatest=0;

   
        
        scanf("%d\n",&x);
        scanf("%d\n",&y);
        scanf("%d\n",&z);
        scanf("%d\n",&k);
        
        if(greatest<x){
            greatest=x;
        }
         if(greatest<y){
            greatest=y;
        } if(greatest<z){
            greatest=z;
        }
         if(greatest<k){
            greatest=k;
        }
        
    
    return greatest;
    
}

`

Thank you in advance.

  • Why do you pass any parameters to your function? You pass uninitialized values and do not use them within that function. Instead better define the 4 variables locally within your function. – Gerhardh Oct 29 '22 at 09:03
  • Your approach to find maximum of 4 values is broken. If you enter only negative values, you will stick with your start value of `0`. Instead of thinking about some useful start value, you better just take the first real value to start with. Then compare with all the others. – Gerhardh Oct 29 '22 at 09:05
  • @Gerhardh I'm sorry I'm new to coding and its terminology. Could you explain what you mean? – MadGaber Oct 29 '22 at 09:05
  • I mean, you should make it `int max(void){ int x,y,z,k;` and update the function call in `main` accordingly – Gerhardh Oct 29 '22 at 09:06
  • You should also always check the return value of `scanf`. – Gerhardh Oct 29 '22 at 09:07
  • 1
    "Unintialized local vriable "d"" It is not possible that this code produces such a warning as there is no variable `d` in your code. Please provide code and messages that actually match. In reality the warnings should be about `a,b,c,f` in `main`. – Gerhardh Oct 29 '22 at 09:09
  • Please correct me if I'm wrong. What you're trying to say is. It's better just to void the input parameters and do what i got to within the function itself? – MadGaber Oct 29 '22 at 09:11
  • @Gerhardh I meant to type f in my question not "d" sorry.. the code worked right when i removed the "\n". but still gives me a warning that there is a uninitialized variable "f" – MadGaber Oct 29 '22 at 09:12
  • Because you do not assign any value to your integers and then pass them to a function. – Gerhardh Oct 29 '22 at 09:14
  • The `max()` is already defined in `stdlib.h`. You can rename your own function and do `return max(max(x, y), max(z, k));`. – Weather Vane Oct 29 '22 at 09:26
  • @Gerhardh thank you for your help. I don't really understand what has to be done. The code works. Maybe I'll understand what you mean in further coding experience. I wish not to ask too much, saving your energy for other people's inquiries. Appreciate the assistance. – MadGaber Oct 29 '22 at 09:29
  • *"I don't know why there is a fifth input"*. It is because of the newlines in the `scanf` format string. Remove them, see [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) Change `scanf("%d\n",&x);` to `scanf("%d",&x);` and the other inputs too. – Weather Vane Oct 29 '22 at 09:29

0 Answers0