Why ANSI Compliance Exists | Programming in C

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

← The Program Development WorkflowNext lesson: From Bell Labs to Modern Compilers: The History of 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

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.

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 Why ANSI Compliance Exists

What is ANSI C?
ANSI C is the first official standard for the C language, approved by the American National Standards Institute in 1989, which is why it is also called C89. In 1990, the International Organization for Standardization adopted the same language as C90. Before the standard, compilers from different companies followed C a little differently, so code that built on one compiler could fail or behave differently on another.
What does portable code mean in C?
Portable code can be moved to a different computer, compiled there, and still work the same way. Following the C standard is the main way to achieve it, because compiler makers promise to obey the standard's rules. Portable code also avoids relying on one machine's choices, such as the exact number of bits in an int, since the standard sets only a minimum size.
What do the -std=c17 and -pedantic compiler options do?
The -std=c17 option tells GCC or Clang to compile using the C17 standard, and -pedantic asks for warnings about non-standard extensions. Without such options, a compiler uses its own default, often with extras. For example, a binary literal such as 0b1011 builds silently but triggers a warning under -std=c17 -pedantic. The -pedantic-errors option turns those warnings into errors that stop the build.
How can a C program tell which standard it was compiled with?
A C program can print the predefined macro __STDC_VERSION__, a number made from a year and a month. Compiled with -std=c17, it gives 201710. C99 gives 199901, C11 gives 201112, and C23 gives 202311. Without a -std option, the value reflects the compiler's default, which can differ between computers and can change when the compiler is updated.

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 →