Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why am i getting garbage values in output

+6 votes
asked Nov 27, 2021 by vaibhav (310 points)
#include <stdio.h>

int main ()

{

  int a[5], i = 0, front =0, rear = 0,j;

  char b;

  printf ("Enter Y if you wish to add elements to the queue\n");

//   scanf ("%c", &b);

//   if (b == "Y")

    

    //   rear = rear + 1;

    //   front = front + 1;

      while (rear < 5)

{

  scanf ("%d",&j);

  a[rear]=j;

  rear=rear+1;

}

    

  printf ("The data you entered is\n");

  while (i < 5)

    {

    

     printf ("%d\n",&a[i]);

     i++;

    }

}

1 Answer

+1 vote
answered Nov 28, 2021 by Peter Minarik (84,720 points)
edited Nov 29, 2021 by Peter Minarik

Print the value, not the address

printf ("%d\n",&a[i]);

The above code prints the memory address of the ith element in the a array. To print the value of a[i], you must not include the "memory of" (&) operator:

printf ("%d\n", a[i]);

Also, you should have a return statement before the end of the main() function to set the return value.

return 0;
commented Sep 24, 2022 by vaibhav (310 points)
Thanks for the solution, the 'memory of (&)' operator slipped my mind.
commented Sep 24, 2022 by Peter Minarik (84,720 points)
I'm happy to help. :)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...