1

I initialized q.size with 7 within main function, Then first it print q.size value as 7, but after the scanf() statement the value of q.size become 0. Why this is happening and how to solve it???

#include <stdio.h>
#include <stdlib.h>
struct sample
{
    int size;
    int rear;
    int front;
};

int main()
{
     struct sample q;
     q.size=7;
     char Ans;

     printf("%d\n",q.size);

     printf("Enter character : (y/n)");
     scanf("%s",&Ans);

    printf("%d",q.size);
    return 0;
}

Expected result from q.size after scanf() as 7 Actual result is 0

1
  • Which language? Commented Jul 16, 2019 at 10:50

1 Answer 1

7

You're reading a string %s into a char variable, this causes memory corruption. You have to put %c in scanf instead of %s in order to read a character.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.