0
#include <stdio.h>
int Second_minimum;
int GetSecondMinimum(int,int,int);
int main(){ 
  int a = scanf("%d",&a);
  int b = scanf("%d",&b);
  int c = scanf("%d",&c);
  int Secondminimum=GetSecondMinimum(a,b,c);
  printf("%d",Secondminimum);
}
int GetSecondMinimum(int x,int y,int z){
  if(x>y && x<z){
    Second_minimum=x;}
  else if(x>z && x<y){
    Second_minimum=x;}
  else if(y>x && y<z){
    Second_minimum=y;}
  else if(y<x && y>z){
    Second_minimum=y;}
  else if(z>x && z<y){
    Second_minimum=z;}
  else if(z<x && z>y){
    Second_minimum=z;}
  return Second_minimum;
}

The above code is not printing on the basis of user-input.

For example, if I am passing a direct value to the function, it is showing the proper answer.

It is printing 20 if I am passing GetSecondMinimum(20,10,30) but it is not printing if I am passing parameters this way:

a=20;b=10;c=30;
GetSecondMinimum(a,b,c)
Capt_Steel
  • 25
  • 7
Kobra
  • 19
  • 3
  • 3
    `int a = scanf("%d",&a);` you are assign return value of `scanf`. – kiran Biradar Sep 17 '20 at 10:01
  • 1
    Reading the man page of scanf: On success, these functions return the number of input items successfully matched and assigned; this can be fewer than provided for, or even zero, in the event of an early matching failure. – kungjohan Sep 17 '20 at 10:04
  • How to pass the user-input through the function and print the result? – Kobra Sep 17 '20 at 10:04
  • it is printing 0 – Kobra Sep 17 '20 at 10:07
  • 2
    Please read the [manpage for scanf](https://www.manpagez.com/man/3/scanf/). The return value is not the same as one of the parsed values. (If you scan 5 integers, which one would you expect as return value anyway?). You should also check that return value if you were able to read any value at all. You probably only got 1 value because there is a `\n` left in the input buffer. – Gerhardh Sep 17 '20 at 10:14
  • 1
    Basically: `int FOO = scanf("%d",&FOO)` -> `scanf("%d",&FOO)`. BTW your question title _"function is not printing right value"_ is misleading, it should be _"scanf is not working as I expect"_ – Jabberwocky Sep 17 '20 at 11:53

1 Answers1

-1
#include <stdio.h>
int Second_minimum;
int GetSecondMinimum(int,int,int);
int main()
{ 
  int a,b,c;
  scanf("%d%d%d",&a,&b,&c);
  int Secondminimum=GetSecondMinimum(a,b,c);
  printf("%d",Secondminimum);
}
int GetSecondMinimum(int x,int y,int z)
{
  if(x>y && x<z)
  {
    Second_minimum=x;
  }
  else if(x>z && x<y)
  {
    Second_minimum=x;
  }
  else if(y>x && y<z)
  {
    Second_minimum=y;
  }
  else if(y<x && y>z)
  {
    Second_minimum=y;
  }
  else if(z>x && z<y)
  {
    Second_minimum=z;
  }
  else if(z<x && z>y)
  {
    Second_minimum=z;
  }
  return Second_minimum;
}

Output :

20 10 30
20

Your Doubt :

#include <stdio.h>
int main ()
{
  int a;
  int b;
  a = scanf("%d", &b);  //lets input b=23
  printf("b = %d",b);
  printf("\na = %d", a);
  return 0;
}

Output :

b = 23
a = 1

int a = scanf("%d",&a); is returning the number of successfully scanned items(in this case 'b') thus if you do this and input the value of b=23, then it will return 1 which is the number of scanned items. Hence, a=1; Here a will contain the return value of scanf function.

scanf returns the number of successfully scanned items.

Ref: What does the scanf function return?