Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func main() {
var b uint8 = 255
b++
fmt.Println(b)
}0
- Expecting
256: auint8holds only 0 to 255 andb++keeps the type, so 255 + 1 wraps around to0. - Expecting a compile error: the compiler rejects only a constant that overflows;
b++changes a variable at run time, where the value wraps silently.
- What does
var c int8 = 127; c++leave inc? - Why does
var b uint8 = 256fail to compile?