03_assignment.zig 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // It seems we got a little carried away making everything "const u8"!
  3. //
  4. // "const" values cannot change.
  5. // "u" types are "unsigned" and cannot store negative values.
  6. // "8" means the type is 8 bits in size.
  7. //
  8. // Example: foo cannot change (it is CONSTant)
  9. // bar can change (it is VARiable):
  10. //
  11. // const foo: u8 = 20;
  12. // var bar: u8 = 20;
  13. //
  14. // Example: foo cannot be negative and can hold 0 to 255
  15. // bar CAN be negative and can hold −128 to 127
  16. //
  17. // const foo: u8 = 20;
  18. // var bar: i8 = -20;
  19. //
  20. // Example: foo can hold 8 bits (0 to 255)
  21. // bar can hold 16 bits (0 to 65,535)
  22. //
  23. // You can do just about any combination of these that you can think of:
  24. //
  25. // u32 can hold 0 to 4,294,967,295
  26. // i64 can hold −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  27. //
  28. // Please fix this program so that the types can hold the desired values
  29. // and the errors go away!
  30. //
  31. const std = @import("std");
  32. pub fn main() void {
  33. const n: u8 = 50;
  34. n = n + 5;
  35. const pi: u8 = 314159;
  36. const negative_eleven: u8 = -11;
  37. // There are no errors in the next line, just explanation:
  38. // Perhaps you noticed before that the print function takes two
  39. // parameters. Now it will make more sense: the first parameter
  40. // is a string. The string may contain placeholders '{}', and the
  41. // second parameter is an "anonymous list literal" (don't worry
  42. // about this for now!) with the values to be printed.
  43. std.debug.print("{} {} {}\n", .{n, pi, negative_eleven});
  44. }