Jump to content

Variable size/Set: Difference between revisions

Go solution
(Go solution)
Line 107:
iprec3 3 8 9
iprec4 4 16 18</pre>
=={{header|Go}}==
{{trans|Ada}}
For task interpretation this follows the spirit of the Ada example included by the task author. In it, an enumeration type is defined from enumeration values, then a storage size--smaller than the default--is specified for the type. A similar situation exists within Go. Defining types from values is called duck-typing, and the situation where a type smaller than the default can be specified exists when a variable is duck-typed from a numeric literal.
<lang go>package main
 
import (
"fmt"
"unsafe"
)
 
func main() {
i := 5 // default type is int
r := '5' // default type is rune (which is int32)
f := 5. // default type is float64
c := 5i // default type is complex128
fmt.Println("i:", unsafe.Sizeof(i), "bytes")
fmt.Println("r:", unsafe.Sizeof(r), "bytes")
fmt.Println("f:", unsafe.Sizeof(f), "bytes")
fmt.Println("c:", unsafe.Sizeof(c), "bytes")
iMin := int8(5)
rMin := byte('5')
fMin := float32(5.)
cMin := complex64(5i)
fmt.Println("iMin:", unsafe.Sizeof(iMin), "bytes")
fmt.Println("rMin:", unsafe.Sizeof(rMin), "bytes")
fmt.Println("fMin:", unsafe.Sizeof(fMin), "bytes")
fmt.Println("cMin:", unsafe.Sizeof(cMin), "bytes")
}</lang>
Output:
<pre>
i: 4 bytes
r: 4 bytes
f: 8 bytes
c: 16 bytes
iMin: 1 bytes
rMin: 1 bytes
fMin: 4 bytes
cMin: 8 bytes
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
1,707

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.