Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I teach C89, C18 or something else? [closed]

Tags:

c

I am TA in a course where I have had to give a crash course in C and one of my students asked, why we still use -ansi as part of our compile commands as it is pretty old.

Now, when I was taught C, 10+ years ago, I was told that it was because it is the most widely adopted and used standard of C and that developers really haven't moved on from ANSI/C89. I looked at it before the lecture and all mentions in terms of usage that I could find was 2007 or before. But I also could not find any recommendations to update. I looked through some 50+ opensource C projects on GitHub and all of them also used ANSI in their Make/CMAKE/WAF configuration, so I thought it was still common practices. But is it?

I can see that ANSI (The organisation) official is at C18. So would it be more appropriate to teach the students this or should we stick with C89? I have not been able to find any documentation or description of what is common practice today and I have mostly worked in "legacy" systems that uses 89 or 90.

like image 608
Lars Nielsen Avatar asked Feb 11 '20 07:02

Lars Nielsen


People also ask

What is the difference between C89 and C99?

In C89, the results of / and % operators for a negative operand can be rounded either up or down. The sign of i % j for negative i or j depends on the implementation. In C99, the result is always truncated toward zero and the sign of i % j is the sign of i. In C89, declarations must precede statements within a block.

Is GCC ANSI C compliant?

Support from major compilersANSI C is now supported by almost all the widely used compilers. GCC and Clang are two major C compilers popular today, both are based on the C11 with updates including changes from later specifications such as C17 and C18.

Is C99 same as C?

C99 is another name of ISO/IEC 9899:1999 standards specification for C that was adopted in 1999.

What is the difference between C99 and C11?

C11 is the latest ANSI C specification, ISO/IEC 9899:2011. C11 looked to address the issues of C99 and to more closely match the C++ standard, C++11. It changes some C99 features required to optional. Some of the features include variable length arrays and complex numbers.


1 Answers

So would it be more appropriate to teach the students this or should we stick with C89?

Unfortunately, this question cannot be answered in an objective way, so it will be a bit opinionated.

If I was a teacher, I would use modern C, that is C11 or newer. But I would also frequently show examples of how it was done in the past. Many of the changes are aimed towards better programming habits.

However, one thing in newer C (which was made non-mandatory in C11) that I would advice against as a teacher is VLA:s. You can read my answer here to understand why: Why VLA:s are bad Note that this is a thing that people disagree on. I'm not claiming I'm correct. VLA:s have their pros, but definitely also their cons. If you want to teach VLA:s, it's important to also teach when and how NOT to use them.

If you want to use an old version, then at least stick to C99. The improvements from C89 to C99 are really major. In most cases, the improvement mainly is about readability convenience, but that should not be underestimated.

Example1, loop variables: (Imho, VERY important for good code structure)

/* C89 */
int i;
for(i=0; i<10; i++) {


// C99
for(int i=0; i<10; i++) {

Not only is this more convenient and readable. It also makes the loop variable disappear when the end of the loop is reached, keeping it local.

Example2, comments:

/* C89 comment */
// C99 comment, and is not valid in C89

Example3, initializers:

struct myStruct {
    uint8_t a;
    char b;
    char *s;
};

struct myStruct createStructC89(uint8_t a, char b, char *s)
{
    struct myStruct x;

    x.a = a;
    x.b = b;
    x.s = s;

    return x;
}

struct myStruct createStructC99(uint8_t a, char b, char *s)
{
    struct myStruct x = {a, b, s};
    return x;
}

The decision might also depend on the goal of the C course.

If it aims towards becoming a C programmer, then I would say that it is important to point out the differences, because most professional C programmers will have to deal with legacy.

On the other hand, consider the cases where C is just chosen as a language for to teach programming for beginners, or if the course aims to come close to the hardware, like courses in computer architecture. Then I would definitely go for C11 or C18. I would consider requiring all handins to compile with gcc -Wall -Wextra -Werror -Werror=vla -pedantic -std=c11

  • -Wall -Wextra Enable extra warnings
  • -Werror Treat warnings as errors. Typical school tasks should have no warnings.
  • -Werror=vla Don't allow vla
  • pedantic Disable many extensions and force the program to (mostly) conform to the standard specified
  • -std=c11 Use C11. Change to C99 or C18 if desired.
like image 95
klutt Avatar answered Oct 25 '22 08:10

klutt