0 votes 0 votes Consider a C Program situation like this, main() { float a=2.23564; printf("%d",a); } Then what will be the output?? please help me.. Programming in C programming-in-c + – Shijith M asked Jul 9, 2018 edited Jul 9, 2018 by srestha Shijith M 781 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes As here a is declared as float and a=2.23564 %d specifier is used to print some integer value but here a is declared as float so some garbage value will be printed as output. Anil Ji answered Jul 9, 2018 Anil Ji comment Share Follow See all 10 Comments See all 10 10 Comments reply srestha commented Jul 9, 2018 reply Follow Share why garbage?why not 2?? 0 votes 0 votes Anil Ji commented Jul 9, 2018 reply Follow Share because %d can only print integer value but a is float and %d will expect some integer value to print but here no such value so garbage value will be printed. 0 votes 0 votes srestha commented Jul 9, 2018 reply Follow Share but here 2 is integer 0 votes 0 votes Anil Ji commented Jul 9, 2018 reply Follow Share you can't assume it as integer by removing floating number because it does not work according to us ,it works according to compiler.As here no such integer value so garbage value will be printed.You can try the code in compiler also. 1 votes 1 votes srestha commented Jul 9, 2018 reply Follow Share why giving garbage value ? why not giving some type of error? 1 votes 1 votes Arjun commented Jul 9, 2018 reply Follow Share Follow the rules - if format specifier is wrong, output is undefined in C. Why should anyone expect it to be something he/she wishes? Only defined stuffs are in GATE syllabus by the way. So, you can avoid time wasting. 3 votes 3 votes Anil Ji commented Jul 9, 2018 reply Follow Share sir i tried the code and its not giving any type of error but printing some garbage value 0 votes 0 votes Anil Ji commented Jul 9, 2018 reply Follow Share https://ideone.com/3R6t5V sir this is the output 0 votes 0 votes Arjun commented Jul 9, 2018 reply Follow Share Undefined means compiler is free to output anything. Simply speaking, most compilers are just outputting whatever is in the Integer register when they see "%d" but the float value is passed in FLOAT registers. But a new compiler or new system may do differently - that is why this is undefined behaviour. Instead of wasting time on such undefined stuffs, focus on questions which are relevant for GATE or real programming. 2 votes 2 votes Anil Ji commented Jul 9, 2018 reply Follow Share thank you sir 0 votes 0 votes Please log in or register to add a comment.