0

hello I am a beginner in c programming , I was building a program that gives prints the maximum digit when entering a 2 digit number,if the user doesn't give a 2 digit number , my program asks him to enter again using functions. Also this is my 1st question , I hope some1 answers me

error :- stack smashing detected *** terminated

language :- c programming

source code

#include <stdio.h>
int aa(int x);
int tryg();

int main() {
    int h;
    printf("enter a 2 digit number\n");
    scanf("%d", &h);
    aa(h);

    return 0;
}
int tryg() {
    int d;
    printf("enter a 2 digit number\n");
    scanf("%d", &d);
    aa(d);
}

int aa(int x) {
    int count = 0, a[2], max;
    while (x > 0) {
        a[count] = x % 10;
        x = x / 10;
        count++;
    }
    if (count != 2) {
        printf(" error you've entered non 2 digit number try again \n");
        tryg();
    } else {
        if (a[0] > a[1])
            max = a[0];
        else
            max = a[1];
        printf("maximum digit is %d", max);
    }
}
paolo
  • 2,345
  • 1
  • 3
  • 17

1 Answers1

0

If you enter a number greater than 2 digits then you are trying to use more memory than what you have declared (here the size of the array is 2), So you get this error as it's a kind of a protection mechanism to detect buffer overflow error. Refer to this to know more about "stack smashing detected" : Stack smashing detected

Piyush Aneja
  • 23
  • 1
  • 6