003_assignment.zig 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. // const 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. // const foo: u8 = 20;
  24. // const bar: u16 = 2000;
  25. //
  26. // You can do just about any combination of these that you can think of:
  27. //
  28. // u32 can hold 0 to 4,294,967,295
  29. // i64 can hold −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  30. //
  31. // Please fix this program so that the types can hold the desired values
  32. // and the errors go away!
  33. //
  34. const std = @import("std");
  35. pub fn main() void {
  36. const n: u8 = 50;
  37. n = n + 5;
  38. const pi: u8 = 314159;
  39. const negative_eleven: u8 = -11;
  40. // There are no errors in the next line, just explanation:
  41. // Perhaps you noticed before that the print function takes two
  42. // parameters. Now it will make more sense: the first parameter
  43. // is a string. The string may contain placeholders '{}', and the
  44. // second parameter is an "anonymous list literal" (don't worry
  45. // about this for now!) with the values to be printed.
  46. std.debug.print("{} {} {}\n", .{ n, pi, negative_eleven });
  47. }