Numeric Data Representation in C, Explained with Code Examples

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

← C Program Formatting StandardsNext lesson: Computing with Operators: Precedence and Associativity →

Earn 3 college credits for Programming in CRecommended for credit by NCCRS · Computer Science 240 · self-paced · one payment, lifetime access

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.

Compare: C vs C++

Why take it here

Why students take Programming in C with UPI Study

How programs work close to the machineProgramming in C teaches how a program uses memory: variables and data types, control flow, functions, arrays and strings, pointers, files, structures and dynamic memory. C is the base for operating systems, embedded devices and many computer-science courses.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

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.

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?
On most current systems, an int is 32 bits and ranges from -2,147,483,648 to 2,147,483,647. The C standard guarantees only a minimum size, so the exact range can differ, and the limits.h header names the ends as INT_MIN and INT_MAX. About half of the roughly 4.3 billion bit patterns represent negative numbers. For values that can pass about two billion, use long long.
What happens when an unsigned integer overflows in C?
An unsigned integer wraps around when it goes past its limit, like an odometer rolling back to zero, and C guarantees this. Storing 255 + 1 back into an unsigned char gives 0, and subtracting one from an unsigned int holding 0 gives its maximum, about 4.3 billion with a 32-bit int. The wrap is well defined, unlike signed overflow, but it can still cause surprising bugs.
Is signed integer overflow undefined behavior in C?
Yes, signed integer overflow is undefined behavior in C, meaning the standard makes no promise about the result. Adding one to INT_MAX might print a strange number, change with optimization settings, or seem to work and break later. Compilers are allowed to assume it never happens. The safe approach is to choose a type large enough for the largest expected value and check before going past the limit.
Why does 0.1 + 0.2 not equal 0.3 in C?
The sum differs because doubles store binary approximations, and 0.1 has no exact binary form, just as one third has no exact decimal form. Printed to 17 significant digits, 0.1 + 0.2 is 0.30000000000000004, so testing it with == against 0.3 gives 0, false. Compare decimals by checking whether they are close enough, and store exact amounts such as money as whole cents.

About the Programming in C course

How many college credits is Programming in C?
Programming in C (Computer Science 240) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Programming in C cost and what is included?
$250, paid once, with lifetime access: all 78 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Programming in C take?
It is self-paced with no deadlines. The 78 lessons total about 9 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

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.

Programming in C
3 credits · NCCRS
Enroll →