UPI Study › Courses › Programming in C › Free lessons › Lesson 2.4 Free with a free account
Numeric Data Representation in C
Programming in C · Chapter 2: C Language Setup, Data Types, Variables, and Console I/OSection 2.4 · lesson 11 of 788 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Programming in C.
Lesson complete
Earn the 3 college credits for Programming in C
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Computing with Operators: Precedence and Associativity →← C Program Formatting StandardsNext lesson: Computing with Operators: Precedence and Associativity →
In this lesson
How C stores numbers and where each type runs out.
- Explain how bits and bytes determine how many values a type can hold
- Use sizeof, limits.h and float.h to find type sizes and ranges
- Contrast unsigned wraparound with undefined signed overflow
- Explain why floating-point values are approximate and how to compare them safely
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (1,088 words). Read along, or search it.
Opening Question
How Big Can a Number Get
How big a number can a computer hold? Not as big as you might guess, and it depends on the box you keep it in. Choose too small a box, and a score of five billion quietly turns into something else. Choose a decimal, and 0.1 plus 0.2 won't give exactly 0.3. Let's see the boxes C offers, and where each one runs out.
Bits and Bytes
Each Extra Bit Doubles the Values
Inside a computer, everything is stored with tiny switches that are off or on. One switch is a bit, short for binary digit. It holds a zero or a one. Eight bits make a byte. A byte is the smallest piece C can name, and every type is measured in bytes. The number of bits sets how many different values you can write. One bit gives two choices, and each extra bit doubles them. So eight bits give 256 patterns, sixteen bits give 65,536, and thirty-two bits give about 4.3 billion.
Integer Types
Measuring Types With sizeof
C's whole-number types differ only in how many bytes they use. From small to big: char, short, int, long, and long long. Char is mainly for letters, but it's a whole number too. The language promises only minimums. A char is one byte by definition, and each type is at least as big as the one before. The sizeof operator asks the compiler how many bytes a type uses, so you never have to guess. Here's a real run on this Mac. An int is four bytes and a long is eight. On 64-bit Windows, a long is usually only four. And a long double is eight bytes here, the same as a double. On some Linux machines it's sixteen. These are typical values, not laws.
Ranges
INT_MAX, INT_MIN and limits.h
How big can a value get? Two headers hold the answers. Limits dot h names the smallest and biggest whole numbers. Float dot h describes the decimal types. The names are easy to read. INT underscore MAX is the biggest int, and INT underscore MIN the smallest. Run it. A char has eight bits. And an int runs from minus 2,147,483,648 up to 2,147,483,647. About half of the 4.3 billion patterns are used for negative numbers, and half for zero and the positive ones. When in doubt, use int. It's the natural size for the computer. Reach for long long when a value can pass two billion.
Unsigned Types
Wrapping Around Like an Odometer
Put the word unsigned in front of a whole-number type, and you tell C: this value is never negative. The sign half of the patterns is used for bigger positive numbers instead. An unsigned int goes from zero to about 4.3 billion. What happens at the top? For unsigned types, C makes a clear promise: the value wraps around, like a car's odometer rolling from 99999 to 00000. The biggest value plus one gives zero. And zero minus one wraps to the top: a player with no tokens who loses one suddenly owns four billion.
Signed Overflow
Undefined Behavior Past INT_MAX
Signed types, plain int and the rest, are different, and this is a big deal. Add one to INT MAX, and C does not promise a wrap. The standard calls this undefined behavior: it makes no promise at all about what happens. Compilers may assume it never occurs. A program might print something strange, change when you turn on optimization, or work on Tuesday and break on Friday. So we won't show a signed overflow as a result. Any number printed would be an accident, not an answer. The rule: choose a type big enough for your largest value, and check before you push past the limit.
Quick Check
An Unsigned Char at 255
Quick check. A game stores a player's level in an unsigned char: one byte, no sign. It starts at the biggest value an unsigned char can hold. The program prints that level, adds one, and prints it again. What are the two numbers? Pause and predict. 255, then 0. Eight bits give 256 patterns, and zero takes one of them, so the biggest is 255. Adding one goes past the top of an unsigned type, so it wraps around to zero. 256 never fits in eight bits.
Floating Point
Why 0.1 Plus 0.2 Misses 0.3
For numbers with a decimal point, C has float, double and long double. They're called floating-point types, because the point floats to wherever the number needs it. A float uses four bytes and a double eight, so a double reaches further and keeps more digits. That's about 6 trusted digits for a float, and 15 for a double. Use double by default. The catch: these types store approximations. Computers count in binary, and 0.1 is a repeating pattern in binary, the way one third is 0.3333 in decimal. Priya adds 0.1 and 0.2. To one digit, the sum looks like 0.3. To 17 digits, it's 0.30000000000000004. So asking whether it equals 0.3 gives 0, false. A float 0.1 shows the same thing at nine digits. Nothing is broken; it's how approximations work.
Safe Comparisons
Close Enough and Whole Cents
So never use a double equals test to check whether two decimal results are exactly equal. You'll meet that test in lesson 4.2. Check whether they're close enough instead. And for money, which must be exact, many programs count cents as whole numbers: 1999 cents, instead of 19.99 dollars.
Quick Check
Seconds in One Day
Quick check. Sam wants to count the seconds in one day: 86,400. Which is the smallest of these types that holds that value on this Mac? Pause and predict. Int. A short tops out at 32,767, so 86,400 doesn't fit. An int reaches about 2.1 billion, plenty. A long long would also work, but it isn't the smallest.
Too-Small Type
A Century of Seconds in an int
A hundred years is a bigger count: 3,155,760,000 seconds, held in a long long. Watch what happens if Sam picks int anyway. Clang warns that the value changes, to a negative number. It adds a second warning: the printf call still expects a long long. Both say the same thing. The box is too small.
Story Ending
A High Score Too Big for short
Priya once stored a player's high score in a short. It worked in every test, until Sam typed 40,000, and the result was nonsense. A signed short tops out at 32,767. Sam's bug note said: not a typo, the box was too small. Priya switched to int, then to long long for the lifetime total, and the number was never lost again.
Key Takeaways
Sizes, Ranges and Approximations
To recap. Each extra bit doubles the values a type can hold, and sizeof tells you a type's real size. Limits dot h gives the ranges. Unsigned values wrap around, by rule. Signed overflow is undefined behavior: prevent it. And decimals are approximate. Never test them with double equals, and keep money in whole cents.
About Programming in C at UPI Study
Programming in C (Computer Science 240) is a self-paced online course of 78 video lessons in 11 chapters, from pseudocode and your first program to data types, decisions, loops, functions, arrays, strings, pointers, files, structures, dynamic memory and linked lists. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: C vs C++
Why take it here
Why students take Programming in C with UPI Study
Where the credit goes
Universities that work with NCCRS
Programming in C carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Numeric Data Representation in C
What is the range of an int in C?
What happens when an unsigned integer overflows in C?
Is signed integer overflow undefined behavior in C?
Why does 0.1 + 0.2 not equal 0.3 in C?
About the Programming in C course
How many college credits is Programming in C?
How much does Programming in C cost and what is included?
Which universities accept UPI Study credits?
How long does Programming in C take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Programming in C.
78 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →