Home
Locate
CS 101
CS 496
Login
Tutors
Marks
About
Send
Close
Add comments:
(status displays here)
Got it!
This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.
nbsp; Note: This appears on each machine/browser from which this site is accessed.
C: Type casts
by RS
admin@ycp.powersoftwo.org
c
go
java
js
lua
py
vbs
rkt
scm
pro
1. C: Type casts
A type cast is an explicit conversion of a value of one type into a value of another type. Here is the C code.
#include <stdio.h> int main() { double x1; int i1; x1 = 3.9; i1 = x1; printf("1. x1=%lf i1=[%d] (i1 is truncated)\n",x1,i1); x1 = 3 / 4; printf("2. x1=%lf\n",x1); x1 = 3.0 / 4; printf("3. x1=%lf\n",x1); x1 = 3 / 4.0; printf("4. x1=%lf\n",x1); x1 = 3.0 / 4.0; printf("5. x1=%lf\n",x1); x1 = (double) 3 / 4; printf("6. x1=%lf\n",x1); x1 = 3 / (double) 4; printf("7. x1=%lf\n",x1); x1 = (double) 3 / (double) 4; printf("8. x1=%lf\n",x1); return 0; }
Here is the output of the C code.
1. x1=3.900000 i1=[3] (i1 is truncated) 2. x1=0.000000 3. x1=0.750000 4. x1=0.750000 5. x1=0.750000 6. x1=0.750000 7. x1=0.750000 8. x1=0.750000
Note: Only when both operands are integer is the result of a division (always) an integer.
2. End of page
3. Multiple choice questions for this page
1
questions omitted (login required)
by RS
admin@ycp.powersoftwo.org