Hi all,
Im a beginner with no previous programming experience. Anyway, Im taking an online course in ANSI C programming, and need some help.
This is a test question, and Im not asking for the answer. Just some help in a way for me to find/understand the answer.
the question is:
Given the following declarations and initialization:
int i = 1;
int x = 2.0;
What is the value and type of the expression i/x ?
My book explains usual arithmatic conversions, and it says:
"if either operand is of type double, the other operand is converted to double."
I wrote a sample program to see how the computer evaluates this.
Code:
#include <stdio.h>
int i = 1;
int x = 2.0;
int y;
int main(void)
{
y=i/x;
printf("y = %d as decimal", y);
return 0;
}
When I run this I get 0 as result, however when I run:
Code:
#include <stdio.h>
int i = 1;
int x = 2.0;
int y;
int main(void)
{
y=i/x;
printf("y = %f as float", y);
return 0;
}
I get a runtime error.
Any suggestions that I could try to help me understand this better?
Thanks,
Darren