Complete Data Type Reference in C | Programming in C

UPI Study › Courses › Programming in C › Free lessons › Lesson 3.1 Free with a free account

Complete Data Type Reference in C

Programming in C · Chapter 3: Data Types, Variables, Math Library, and Console I/OSection 3.1 · lesson 13 of 787 min · free with a free account

← Computing with Operators: Precedence and AssociativityNext lesson: Variable Declaration in C →

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

In this lesson

C's basic data types, character codes and automatic conversions.

  • Describe the five basic types char, int, float, double and void
  • Use character codes to do arithmetic with char values
  • Explain implicit conversion and integer promotion in mixed expressions
  • Identify where truncation and precision loss occur in conversions

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 (934 words). Read along, or search it.

Opening Analogy

The Right Container for Each Job

A kitchen has a container for each job: a spoon for sugar, a jug for milk, a big pot for soup. Pour milk into a spoon, and most of it hits the floor. C works the same way. Every piece of data needs the right container, called a type. And when you mix containers in one line, C quietly pours data from one into another. Sometimes that's harmless. Sometimes the answer changes.

Basic Types

Five Basic Types in C

A data type tells the computer two things about a value: how much memory it needs, and how to read the bits stored there. The same thirty-two bits can mean the whole number 1,065,353,216, or the decimal 1.0. The type decides which. C starts you with five basic types. Char holds one character, or a tiny whole number. Int holds whole numbers. Float and double hold decimals, and a double has twice the room, so it keeps more digits. And void means nothing at all. In int main, void says main takes nothing in. You can't make a void variable; functions bring it back in lesson 6.1.

Type Sizes

Bytes Per Type on One Mac

How big is each one? Ask sizeof, as in lesson 2.4. Percent z u is the printf code for a sizeof result. On this Mac: a char is one byte, an int four, a float four, and a double eight. Typical sizes; the rule book only promises minimums.

Characters

Letters Stored as Number Codes

Computers only store numbers. So how does a char hold the letter A? Every character has a number, its character code. Capital A is 65, B is 66, and lowercase a is 97. When you write A in single quotes, C uses 65. Percent c shows that number as a letter. Percent d shows the plain number. Because a char is a number, you can do arithmetic on it. Letter plus one gives the next letter. Run it. A has code 65, and the next letter is B. Subtracting the character zero turns the digit character seven into the number 7. And the gap between lowercase and capitals is 32.

Quick Check

Adding Two to Capital D

Quick check. A char holds capital D. The program prints c plus two, once with percent c and once with percent d. What does it print? Pause and predict. It prints F 70. D is code 68, so plus two is 70, and code 70 shown as a letter is F. 68 is D's own code, before adding. And E would be plus one.

Implicit Conversion

Smaller Types Move Up

What does three times 2.5 mean, when three is an int and 2.5 is a double? The processor can only multiply two values of the same type. So C converts one of them first, without being asked. That's an implicit conversion. Implicit means unspoken. The rule: the smaller type moves up to the bigger one, so no detail is lost on the way up. Char, then int, then float, then double. The risky trip is the other way: storing a big type into a small one.

Mixed Arithmetic

One Operation at a Time

Build it first. Clang prints one warning, about the last line. We'll come back to it. Now run it. The first line mixes an int and a double: three becomes 3.0, and 3.0 times 2.5 gives 7.50. Now the trap. Conversion happens one operation at a time. When seven is divided by two, both sides are still ints, so C does whole-number division and gets 3. Only afterwards is that 3 turned into 3.0, to be stored. A double on the left doesn't rescue the division on the right. Make one side a double before dividing, as in seven divided by 2.0, and the answer is 3.5. And the last line stores 9.99 in an int. It prints 9: C cuts the decimals off; it doesn't round. Here's the warning from the build again. Clang saw that the value changes from 9.99 to 9. It builds, but treat it as a bug.

Integer Promotion

Chars Are Added as Ints

Even when you mix nothing, C does a conversion you can't see. Arithmetic is done in int at the smallest. So any char in a calculation is first widened to an int. That step is called integer promotion. Two chars of 100 are added as two ints, so the sum has room to be 200. And sizeof shows it: a alone is one byte, but a plus b measures four.

Precision Loss

When a Float Drops Digits

One last surprise: moving up isn't always safe. A float has the same four bytes as an int, but spends some of them on the decimal point. So it keeps fewer digits of a big whole number. The int 16,777,217 becomes the float 16,777,216. And 0.1 can't be stored exactly in binary. To ten places, the float shows the error. The double hides it much further out. Sam's test once found Priya's float prize total off by a tiny amount. She switched to double, and it matched.

Quick Check

Five Divided by Two Plus 1.5

Quick check. Five tokens, divided by two, plus 1.5, stored in a double. Work it one operation at a time. What value ends up printed? Pause and predict. It prints 3.5. First, tokens divided by two: both ints, so the answer is two, not 2.5. Then two plus 1.5. Now a double is involved, so two becomes 2.0, and the sum is 3.5. Four would need the division done with decimals.

Key Takeaways

Types, Codes and Conversions

To recap. The five basic types are char, int, float, double and void, and a char is a number in disguise. When types meet, the smaller one moves up, but one operation at a time, so int division can happen first. Small types are promoted to int in arithmetic. Storing into a smaller type cuts data off, and a float keeps fewer digits than you might expect.

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 Complete Data Type Reference in C

How does C store a char like 'A'?
C stores a char as a small whole number, its character code. In ASCII, which most systems use, 'A' is 65, 'B' is 66 and 'a' is 97, so a lowercase letter and its capital differ by 32. Printing with %c shows the letter, while %d shows the number. Because a char is a number, 'A' + 1 gives 'B', and '7' - '0' gives the number 7.
What is implicit type conversion in C?
Implicit type conversion is a change of type that C makes automatically when an operation mixes types. The smaller type moves up to the larger one, along the order char, int, float, double, so 3 * 2.5 converts 3 to 3.0 and gives 7.5. Conversion happens one operation at a time, so in 7 / 2 both operands are ints and the result is 3, even if it is stored in a double.
What is integer promotion in C?
Integer promotion is C's automatic widening of small integer types, such as char, to int before arithmetic. Arithmetic is done in int at the smallest, so two char variables holding 100 are added as ints, and their sum of 200 is computed without overflow. The sizeof operator shows it: sizeof(a) is 1 for a char, but sizeof(a + b) is the size of an int, 4 on typical systems.
Can converting an int to a float lose information?
Yes, a large int can lose precision when converted to float. On typical systems both use four bytes, but a float spends some bits on the exponent, leaving 24 bits of precision. So the int 16,777,217 becomes the float 16,777,216. Storing a double such as 9.99 into an int also loses data, because the fraction is cut off to give 9 rather than rounded.

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 →