Error: uninitialized local variable 'x' used

3

Good afternoon or everyone's day, I want to ask you for help with this little error that I have, I ask for help because I have already tried several things and I still get this error, I have almost no experience using functions in c, but here I leave my code:

#include <stdio.h>
#include <string.h>
int isdigit()
{
    int x, y;
    printf("inserte dos numeros\n");
    scanf_s("%d""%d",&x,&y);
    return x;
    return y;
}
int main()
{
    int x,y,z;
    isdigit();
    printf("%d\n", x);
    z = x + y;
    printf("%d\n",z);
}

the error appears to me as much to the variable x as to and, try placing int x = 0, y = 0, z; but it did not work, the program ran but at the time of printing x and z they give me 0, regardless of the values that I inserted with scanf_s.

    
asked by esencia 02.08.2017 в 03:28
source

2 answers

4

You have several concept errors. I'm going to put the code you should use and then I'll explain it step by step:

#include <stdio.h>
#include <string.h>

void foo(int *x, int *y)
{
    printf("inserte dos numeros\n");
    scanf("%d""%d",x,y);
}
int main()
{
    int x,y,z;
    foo(&x, &y);
    z = x + y;
    printf("%d\n",z);
    return 0;
}

First of all you should not use the name isdigit for your function since it is a reserved function of the library ctype . I will change the name to foo but give it the name that suits you.

Second, the functions of C can only have a return value, you are returning 2, which is incorrect. Anyway you also have a concept error, even if you had returned only one value, you never assigned the function in your main program, for example x = foo() .

To return more than one value in a function you have to make passages by reference (by default in C the parameters are passed by value). To do this you have to use pointers, that is, pass the memory address of the data you want to modify. Anyway, we would go into a very extensive explanation about pointers.

If you do not want to get pointers, you can do the sum in the function and return it to the main program:

#include <stdio.h>
#include <string.h>

int foo()
{
    int x, y;
    printf("inserte dos numeros\n");
    scanf("%d""%d", &x, &y);
    return (x + y);

}
int main()
{
    int z;
    z = foo();
    printf("%d\n",z);
    return 0;
}
    
answered by 02.08.2017 в 03:48
0

If we analyze this function:

int isdigit()
{
    int x, y;
    printf("inserte dos numeros\n");
    scanf_s("%d""%d",&x,&y);
    return x;
    return y;
}

We see that you have the following errors:

  • Incorrect name. The function does not check that the user input is numbers ... in fact if instead of numbers you enter letters the program will not show any error message nor will it act accordingly. That the name overlaps with that of the ctype.h library could only be problematic if you load this library in your application ... although it would not hurt to prevent and avoid overlaps ... just in case
  • Two return . The sad reality is that the program will abandon the function in the first return , so the second will never be executed. In addition, the function itself indicates that it returns a single integer ... it does not say anything about returning two values. To return a composite value you would have to do it with a single return ... and that is not the case.
  • Wrong the scope of the variables. You are assuming that the variables x e y of this function will modify the value of the own variables of main ... and it is not like that. These variables are local and your life will end when the program leaves the function. If you want the value of these variables to last via return you will have to capture the value:

        int x = isdigit();
    

Possible solutions:

Simple reading

You have a function that returns an integer. To read two values you will have to call the function twice:

int readInt()
{
  int x;
  while( !scanf("%d",&x) )
  {
    while ((c = getchar()) != '\n' && c != EOF); // limpieza del buffer de entrada
    printf("ERROR: No era un numero. Intentalo de nuevo: ");
  }

  return x;
}

int main()
{
  puts("Introduzca dos números: ");
  int x = readInt();
  int y = readInt();

  int z = x + y;
  printf("%d\n",z);
}

Example in ideone

Double reading with pointers

In this case the function reads the two numbers and returns them. To return the values use pointers received as arguments

void readInts(int* a, int* b)
{
  int x;
  while( scanf("%d %d",a,b) != 2 )
  {
    char c;
    while ((c = getchar()) != '\n' && c != EOF); // limpieza del buffer de entrada
    printf("ERROR: Uno de los valores no era un número. Intentalo de nuevo: ");
  }

  return x;
}

int main()
{
  puts("Introduzca dos números: ");
  int x, y;
  readInts(&x,&y);

  int z = x + y;
  printf("Resultado: %d\n",z);
}

Example in ideone

Composite reading with return

As I mentioned, in C the returned values have to be packed in a single return . This does not mean that only an integer can be returned ... structures can also be returned with all the information we need:

typedef struct
{
  int x;
  int y;
} TwoInts;

TwoInts readInts()
{
  TwoInts toReturn;
  while( scanf("%d %d",&toReturn.x,&toReturn.y) != 2 )
  {
    char c;
    while ((c = getchar()) != '\n' && c != EOF); // limpieza del buffer de entrada
    printf("ERROR: Uno de los valores no era un número. Intentalo de nuevo: ");
  }

  return toReturn;;
}

int main()
{
  puts("Introduzca dos números: ");
  TwoInts twoInts = readInts();

  int z = twoInts.x + twoInts.y;
  printf("Resultado: %d\n",z);
}

Example in ideone

    
answered by 02.08.2017 в 17:05