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.

garbage value being printed

–1 vote
asked Apr 1, 2021 by Akshansh Sharma (140 points)

#include <stdio.h>

int main()
    int a;
    int c=4;
    float b;

    if ((a=b=c)){
    printf("%d\t%d\t%d\n",a,b,c);/* here because %d is used for b which is float it is returning garbage value and that's okay,
    but why the value of c also garbage when %d is used for c which is int.*/
    printf("%d",c);// here correct value of c is  printed.

3 Answers

+1 vote
answered Apr 9, 2021 by Peter Minarik (55,660 points)

If you replace your printf formatting with the correct format, then the right value is printed.

printf("%d\t%f\t%d\n", a, b, c);

We saw that the value of c is fine, so I'd speculate that it's just printf that doesn't handle correctly when you try to print a floating point number as an integral number. The following data to be printed suffers the mistakes made before.

+1 vote
answered Apr 10, 2021 by Ashvin Birla (160 points)
because you use %d at place of %f ...and float consider the %f that concept to return the garbage value
commented Apr 12, 2021 by Peter Minarik (55,660 points)
Not exactly, as %f was the 2nd format specifier and the 3rd output was 0, not the 2nd (with the wrong formatting). There must be a bit more to this.
0 votes
answered Apr 22, 2021 by Alex Rujoiu (160 points)
float variables are made in 2 parts , so that is part of the problem , you need to convert it explicit

->ex              ( int) f
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.