Hello sir, I just have started to learn C programming from a few days and I am a total newbie. I created a simple program of printing the average of three integers (input by user) to one decimal place. My correct program is:
Code:
//Write a Program to request three integers...
//...and print their average to one decimal place.
#include <stdio.h>
main()
{
int a,b,c;
printf("\n Enter the values of a,b,c = ");
scanf("%d %d %d",&a,&b,&c);
double d=a+b+c;
printf("\n The Average of a,b,c to one decimal place is = %1.1f \n", d/3);
}
Now this program runs fine without any problem giving correct results. But when I write the statement 'double d=a+b+c' before first printf function (like in below program) instead of the place as in above, then I get a logic error (example image at the bottom) giving absurd value result on input values.
Code:
#include <stdio.h>
main()
{
int a,b,c;
double d=a+b+c;
printf("\n Enter the values of a,b,c = ");
scanf("%d %d %d",&a,&b,&c);
printf("\n The Average of a,b,c to one decimal place is = %1.1f \n", d/3);
}
Please anybody help me understanding the problem in the second program as what is wrong in it.