Unexpected closure before requesting data

0

Find the division of two numbers recursively the executable only asks for the first digit and then closes without asking for the second digit.

#include<stdio.h> 



int division (int a, int b);

int main (){

int   a, b;
printf("Digite el primer numero");
scanf("%d", a);
printf("Digite el segundo numero");
scanf("%d", b);

printf("la division es %d ",division(a, b));

system("PAUSE");
return 0;
}


 int division (int a, int b) {
 if(b > a) {
 return 0;
 }
 else {
 return division(a-b, b) + 1;
 }
 }
    
asked by Laura 03.09.2018 в 16:35
source

1 answer

1

The error is here:

printf("Digite el primer numero");
scanf("%d", a);                      // <<---
printf("Digite el segundo numero");
scanf("%d", b);                      // <<---

And the reason is that scanf need you to pass a pointer as it is not impossible for you to modify your variables.

Since C has a rather weak typing, the compiler will show you, at best, a warning ... but the resulting code is not so benevolent and modern operating systems do not tolerate intromissions in regions of memory that do not belong to you.

Solution: Pass pointers to scanf :

printf("Digite el primer numero");
scanf("%d", &a);
printf("Digite el segundo numero");
scanf("%d", &b); 
    
answered by 03.09.2018 в 16:39