CS 70

Exploring C++'s Numeric Types

In this week's lessons you learned about how numbers are represented in C++.

In this part you'll be able to actually see how different types are represented in your development environment.

show-rep and show-types

In the Types-and-Representations directory, we've provided two C++ programs that provide some details about the inner workings of the C++ system you're using. The source for these programs, show-rep.cpp and show-types.cpp, isn't something you need to go spelunking into—providing under-the-hood information necessarily involves some rather atypical C++ code and you won't gain much from peering at it. But you can take a peek if you like—the main function, at least, is mostly comprehensible. By the end of the semester, you'll understand a lot more and you'll be in a good position to figure out or look up the rest!

For now, just compile and run these programs. We've simplified the compilation process this time, so all you have to type is

make

and both programs will be compiled. (You'll learn about make later this semester!)

To run them, you can type

./show-types | more

and

./show-rep | more

The | more part stops the output from just zooming past on the screen. With more, you can page through the results by using the space bar to move forward one page at a time.

Explore the output to get a sense of how numbers are represented on your system!

Results May Vary

Remember that in the lesson we told you that many of the details of numeric representation are not standardized and depend on the system/compiler? We meant it!

True story: Our Docker image detects whether your processor has an ARM architecture (e.g., newer Macs) or an x86 architecture (older Macs and most other recent computers) and adjusts a few things so you will get correct behavior either way. If you run these programs on those different architectures, you will get different output! If you have access to computers with different architectures, maybe try it on both and see if you can spot the differences….

In any case, you should not take the output of these programs as a universal truth: They are giving you information just about your system.

Some Questions

Some of the written questions for this homework (in this Gradescope assignment) relate to this example.

Now might be a good time to answer them! (You can answer some questions, save your responses, and then go back and answer the rest later.)

To Complete This Part of the Assignment…

You'll know you're done with this part of the assignment when you've done all of the following:

(When logged in, completion status appears here.)