Home > Enterprise >  Wrong output with scanf function
Wrong output with scanf function

Time:01-26

so this is supposedly not a difficult question, but I've been getting this problem a few times when running my code in VS code. I am trying to separate the alphabets and numbers from the string, and I have used the method as follows (in my code) according to what is taught in the book. However, despite having the program running, the output is wrong.

Here is my code:

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

int main(){
    int weight = 0;
    int height = 0;
    char wunit[] = "";
    char hunit[] = "";
    printf("Enter the body weight: ");
    scanf("%d%s",&weight,wunit);
    printf("Enter the height: ");
    scanf("%d%s",&height,hunit);
    printf("%d,%s,%d,%s", weight, wunit, height, hunit);
    return 0;
}

The thing is,if I type in 20lb for weight, and 30mt for height, what happens is that it gives the output: 20,t,30,mt; which generates this weird ‘t’ instead of lb, and I have no idea why this is the case.

Similarly, when I type 30kg for weight, and 20cm for height. It generates this weird output:30,m,0, cm. The kg becomes a 'm' and the 20 is now a '0'!? Why is that the case? The expected output would be 30,kg,20,cm

I tried simply replacing the strings, but that doesn't solve the problem fundamentally. For instance, (considering when my user puts logical inputs like lb or kg for weight), I tried this substitution and it appears to work, but doesn't fix the issue of making 20 -> 0

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

int main(){
    int weight = 0;
    int height= 0;
    char wunit[] = "";
    char hunit[] = "";
    char wunit2[] = "lb";
    char dummy[] = "t";
    printf("Enter the body weight: ");
    scanf("%d%s",&weight,wunit);
    printf("Enter the height: ");
    scanf("%d%s",&height,hunit);
    if (strcmp(wunit,dummy)==0){
        printf("%d,%s,%d,%s\n", weight, wunit2, height, hunit);
    }
    //printf("%d,%s,%d,%s", weight, wunit, height, hunit);
    return 0;
}

I've also tried running it in codecollab, and it shows this error of "stack smashing detected" after I run it a few times, which got me more confused, what has it to do with this?

Thanks in advance.

CodePudding user response:

wunit is an array of size 1 (it is initialized to "", which in chars looks like {'\0'}). What happens when you try to put lots of characters (say, "lb", which is {'l', 'b', '\0'}) into a memory location that is smaller than it should be?

scanf happily writes as many bytes as needed, smashing anything in its way ("stack-smashing", because wunit and all those local variables are stored on the stack). Try to give scanf more space, say using

char wunit[10] = "";

And never ever use "%s" directly. Limit the maximum of characters that you will allow scanf to place, for example using "%9s" to ensure that at most 9 characters terminator (10 total) will be read.

This works for me:

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

int main(){
    int weight = 0;
    int height = 0;
    char wunit[10] = "";
    char hunit[10] = "";
    printf("Enter the body weight: ");
    scanf("%d%9s",&weight,wunit);
    printf("Enter the height: ");
    scanf("%d%9s",&height,hunit);
    printf("%d,%s,%d,%s", weight, wunit, height, hunit);
    return 0;
}

Note: scanf with %s is rightfully considered very dangerous. See https://stackoverflow.com/a/2430310/15472

  • Related