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
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: Variable Declaration in C →← Computing with Operators: Precedence and AssociativityNext lesson: Variable Declaration in C →
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.
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 Complete Data Type Reference in C
How does C store a char like 'A'?
What is implicit type conversion in C?
What is integer promotion in C?
Can converting an int to a float lose information?
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 →