UPI Study › Courses › Programming in C › Free lessons › Lesson 1.7 Free with a free account
Why ANSI Compliance Exists
Programming in C · Chapter 1: Programming Fundamentals, Pseudocode, and Program DevelopmentSection 1.7 · lesson 7 of 789 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: From Bell Labs to Modern Compilers: The History of C →← The Program Development WorkflowNext lesson: From Bell Labs to Modern Compilers: The History of C →
In this lesson
How the ANSI and ISO C standards keep code portable.
- Explain portability and why competing compilers made a standard necessary
- Trace the C standards from C89 and C90 to C23
- Use -std=c17 and -pedantic to catch compiler extensions
- Read the __STDC_VERSION__ value to identify a standard
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,216 words). Read along, or search it.
Opening Analogy
Different Families, Different Rules
Have you ever played a board game at a friend's house, and found out their family plays by different rules? Everyone thinks they know the game, and then the arguments start. Early C had exactly that problem, with computers instead of families. The fix was an official rule book. Learning to follow it is how your C code can work on almost any computer in the world.
The Problem
Compilers That Disagreed
In the nineteen seventies and early eighties, as C spread, different companies wrote their own C compilers. Each followed the language a little differently, and many added extra features of their own. So one file could build with compiler A, fail with compiler B, and build with compiler C, but behave differently. Code is portable when it can be moved to a different computer, compiled there, and still work the same way. That matters. The same C code might need to run on a laptop, a phone, a game console, and a tiny chip inside an arcade machine.
Definition
An Official Rule Book
The fix is a standard: an official document that says exactly what something must be, so that things made by different people fit together. Standards are why any USB charger fits any USB port, and why a light bulb from one store fits a lamp from another. A language standard does the same for code. It lists every rule of the language, and compiler makers promise to follow it.
Standards Timeline
From C89 to C23
In nineteen seventy-eight came the famous K and R book, but no standard yet. The first standard was written by a committee for ANSI, the American National Standards Institute. It was approved in nineteen eighty-nine, which is why people say ANSI C, or C eighty-nine. In nineteen ninety, ISO, the International Organization for Standardization, adopted the same language as C ninety. Since then, each new edition is named after its year. C ninety-nine added double-slash comments, a bool type, and long long numbers. C eleven added threads, for doing jobs at the same time. C seventeen was fixes only, with no new features. And C twenty-three added binary numbers written with zero b, and true and false as keywords. These lessons follow C seventeen.
Naming Quirks
C17 Is Also Called C18
Two naming quirks. C seventeen was finished in twenty seventeen, but published in twenty eighteen, so you'll also see it called C eighteen. Same standard. Why keep the old rules in every edition? Millions of lines of C already exist. Break them, and nobody could upgrade. So most well-written C eighty-nine code still compiles as C seventeen.
Compiler Options
Choosing a Standard With Flags
Here's a surprise: a compiler doesn't follow a standard strictly unless you ask. Without instructions, it uses its own default, often with extras. Those extras are called extensions. GCC fifteen, for example, defaults to its own flavor of C twenty-three. Extensions can be handy, but code that uses them isn't portable. Two command-line options fix this. An option, or flag, is an extra word that starts with a dash and changes what the compiler does. GCC and Clang take the same ones. Dash s t d equals c seventeen picks the standard. Dash pedantic asks for a warning about every non-standard extra the standard says must be reported.
Version Check
Reading the Standard's Code Number
You can even ask from inside a program which rules the compiler is following. It fills in a code number called s t d c version. The code is a year and a month. With dash s t d equals c seventeen, it prints two zero one seven one zero. C ninety-nine is one nine nine nine zero one, C eleven is two zero one one one two, and C twenty-three is two zero two three one one.
Extension Caught
A Binary Literal Under Pedantic
Now an extension, caught. Priya wants to set which lamps on a game machine are on, so she writes the pattern in binary, with zero b one zero one one. Even with dash s t d equals c seventeen, Clang builds it without a word. Add dash pedantic, and here's the real message: line five, column seventeen, a warning. Binary integer literals are a C twenty-three extension. The program still builds, and prints eleven. But binary literals aren't part of C seventeen. A compiler that follows C seventeen exactly may reject this line. The tag at the end names the warning group. With dash s t d equals c twenty-three, the warning disappears, because C twenty-three made binary official.
Portable Fix
Writing Eleven Instead of Binary
The portable fix is the same value, in a form every C seventeen compiler accepts. Binary one zero one one is eleven. Starlight Arcade's build rule uses dash pedantic errors, which turns those warnings into errors. When Sam built Priya's first version that way, it stopped, and made no program at all. Priya grumbled, switched to eleven, and the build passed. Now it will build on the new machines too, said Sam.
Quick Check
Warning or Error for Binary 1100
Quick check. A friend's program sets a pattern with zero b one one zero zero. They build it with dash s t d equals c seventeen, and dash pedantic. What happens, and what's the fix that stays in C seventeen? A warning, then use twelve. An error, then use eleven hundred. Or no message at all? Pause, and decide. A warning, and the fix is twelve. Dash pedantic warns; only dash pedantic errors would stop the build. Binary one one zero zero is eight plus four, which is twelve. Eleven hundred would change the value completely.
Two-Sided Promise
Compiler Makers and Programmers
A standard works like a deal with two sides. Compiler makers promise: code that follows the rules will compile, and behave as the rules say. You promise: my code follows the rules. When both sides keep their promise, your code doesn't care whose compiler it meets, or what computer it runs on. The standard does leave a few details to each system, such as exactly how many bits an int uses. It sets a minimum, and careful programmers don't rely on one machine's choice.
Daily Habit
Strict Flags on Every Build
So beware of: it works on my computer, so it's correct. Your compiler may be quietly accepting extensions, and your machine may make choices another won't. Make dash s t d equals c seventeen, dash pedantic, dash W all, dash W extra your everyday command, and treat every warning as a to-do item.
Quick Check
Builds Here, Fails There
Quick check. Your program builds on your laptop with plain clang, no options. A teammate builds it with strict C seventeen settings, and it fails. What's the most likely cause? You used an extension. Their compiler is broken. Or C seventeen dropped some old rules? Pause, and decide. An extension. Plain clang quietly accepts extras; strict C seventeen settings report them, like the binary literal you just saw. A broken compiler is rarely the answer. And C seventeen dropped nothing that working code relied on: it was fixes only.
Key Takeaways
Portability and the Standards
To sum up. Portable code moves to another computer, compiles, and works the same. The C standards are the shared rule book: C eighty-nine and ninety, then ninety-nine, eleven, seventeen, and twenty-three. Dash s t d picks the standard, and dash pedantic reports extensions. And standard code is a two-sided promise: you follow the rules, and compilers obey them.
Think About It
Two Computers, Two Numbers
Something to think about. A program prints its standard code, and your laptop says two zero one seven one zero. A classmate's computer, with no options, prints a different number. Is either program wrong? And how would you make both print the same?
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 Why ANSI Compliance Exists
What is ANSI C?
What does portable code mean in C?
What do the -std=c17 and -pedantic compiler options do?
How can a C program tell which standard it was compiled with?
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 →