0

The following is my problem. When I try to use the following code to output, why does the output result, the value of the variable age becomes 0. (Note: I am using the GCC compiler to try in VScode, and I will not have this problem when I try it in vstudio2019)

#include <stdio.h>
int main(){
    int age;
    char x;
    printf("Please enter age:");
    scanf("%d", &age);

    printf("Please enter gender: ");
    scanf("%s", &x);
    printf("%d,%s",age,x);
    
    return 0;
}

working process: enter image description here

Thanks for the answer, I understand that %s needs to be intercepted to a null character, but I made the following changes, just let it output the value of age, and still output the result as 0, I don't quite understand, these two variables Will they affect each other?

#include <stdio.h>
int main(){
    int age;
    char x;
    printf("Please enter age:");
    scanf("%d", &age);

    printf("Please enter gender: ");
    scanf("%s", &x);
    printf("%d",age);
    
    return 0;
}

enter image description here

Seraphine
  • 29
  • 2
  • 4
    `%s` requires a char array large enough to store the input string - including the terminating NUL character. A single `char` cannot store anything except the empty string. Attempting to storing anything more results in a buffer overflow and Undefined Behaviour. – kaylum Oct 20 '22 at 03:09
  • You declare `x` as a single `char`. That is sufficient only for an empty string, because you need one char for a string terminator. `scanf()` is trying to read a non-empty string into that space. The result is a buffer overrun, with undefined behavior. – John Bollinger Oct 20 '22 at 03:10
  • The char v. string issue applies to the printf statement as well... `x` is not a string. – Fe2O3 Oct 20 '22 at 03:14
  • 1
    "*just let it output the value of age, and still output the result as 0*". That's because the error is still there. `scanf("%s", &x);` is overflowing the buffer and invokes Undefined Behaviour. UB means it can scribble over any of the other variables or can crash or any other unpredictable behaviour. Just declare `x` as an array big enough to hold the maximum string input. – kaylum Oct 20 '22 at 03:18
  • As has been commented %s requires use of a char array, which you don't currently have in your code, but could modify it to use. If your true intent in for a single char only, then the format specifier is %c. – Avi Berger Oct 20 '22 at 03:21
  • Thanks, I understand the problem, thank you very much – Seraphine Oct 20 '22 at 03:26

1 Answers1

-1

So on your updated code there is a memory leak and you also didn't print out your other variable.

#include <stdio.h>
int main(){
    int age;
    char x[6];
    printf("Please enter age:");
    scanf("%d", &age);

    printf("Please enter gender: ");
    scanf("%s", x);
    printf("%d %s", age, x);

    return 0;
}

Your code should look something like this. People before me have already mentioned that this is a buffer overflow so I won't go into detail on that. And when you're asking if the two data types will conflict with each other, I'm not exactly sure what you are asking? As your code stands, the two data entries are completely separated and do not affect each other whatsoever.

  • 4
    There is no memory leak. A memory leak is caused by unfreed memory. This code doesn't even use `malloc` etc. The problem is memory corruption, caused by writing beyond the bounds of a variable. – Tom Karzes Oct 20 '22 at 03:52
  • 2
    You really want to do `scanf("%5s", x);` here. You want to specify the size always, but especially when the string fits at most 5 characters, which is so very easily exceeded even by accident. – hyde Oct 20 '22 at 05:59