Does C++ Contain the Entire C Language

C WikiBooks - How is C a small what you see is all you get language?

The assembly is the language for a single processor family, it is directly compiled to the machine code that the processor runs. If one programs in assembly, one needs to rewrite the entire code for the different processor family. Phones usually use ARM processors whereas the desktop computers have 32-bit or 64-bit x86-compatible processors. Each 3 of these potentially need a completely separately written program, and perhaps not even limited to that.

In contrast standard C is a portable language - if you write so-called strictly conforming programs. C11 4p5:

A strictly conforming program shall use only those features of the language and library specified in this International Standard. (3) It shall not produce output dependent on any unspecified, undefined, or implementation-defined behavior, and shall not exceed any minimum implementation limit.

With footnote 5 noting that:

Strictly conforming programs are intended to be maximally portable among conforming implementations. Conforming programs may depend upon nonportable features of a conforming implementation

Unlike the assembler whose specifics vary from processor to another, it is possible to write programs in C and then port them to various platforms without any changes into the source code, yet these programs will still be compiled into the assembly language, and performance could - and often will - surpass hand-written assembly when using a modern high-quality optimizing compiler.

Additionally the C standard library, which any conforming hosted implementation needs to provide, provides for a portable way to manage files, dynamic memory, input and output, all of which are not only processor but also operating-system specific when using assembler.

However, C is still quite close to the assembly, to the extent that it has been called a "high-level assembly language" by some.

Is C/C++ one language or two languages?

C++ diverged from C in 1982-1983, and that's a long time in computer years. But, there are many C libraries with C++ compatibility, including the C standard library itself, and a steady stream of programs are ported across from C to C++. Many C programmers only know or use the features that are compatible with C++.

They are defined by different ISO standards from separate committees. Even when they define compatible features, it is often defined in different terms.

Referring to C/C++ is about as valid as referring to Italian/Spanish. You should be careful to whom and when you use such a term. But it's true that there is diffusion of ideas in both directions, and the similarities are more than coincidence.

Where is C not a subset of C++?

If you compare C89 with C++ then here are a couple of things

No tentative definitions in C++

int n;
int n; // ill-formed: n already defined

int[] and int[N] not compatible (no compatible types in C++)

int a[1];
int (*ap)[] = &a; // ill-formed: a does not have type int[]

No K&R function definition style

int b(a) int a; { } // ill-formed: grammar error

Nested struct has class-scope in C++

struct A { struct B { int a; } b; int c; };
struct B b; // ill-formed: b has incomplete type (*not* A::B)

No default int

auto a; // ill-formed: type-specifier missing

C99 adds a whole lot of other cases

No special handling of declaration specifiers in array dimensions of parameters

// ill-formed: invalid syntax
void f(int p[static 100]) { }

No variable length arrays

// ill-formed: n is not a constant expression
int n = 1;
int an[n];

No flexible array member

// ill-formed: fam has incomplete type
struct A { int a; int fam[]; };

No restrict qualifier for helping aliasing analysis

// ill-formed: two names for one parameter?
void copy(int *restrict src, int *restrict dst);

Is it true that there is no need to learn C because C++ contains everything?

Overview:

It is almost true that C++ is a superset of C, and your professor is correct in that there is no need to learn C separately.

C++ adds the whole object oriented aspect, generic programming aspect, as well as having less strict rules (like variables needing to be declared at the top of each function). C++ does change the definition of some terms in C such as structs, although still in a superset way.

Examples of why it is not a strict superset:

This Wikipedia article has a couple good examples of such a differences:

One commonly encountered difference is
that C allows implicit conversion from
void* to other pointer types, but C++
does not. So, the following is valid C
code:

int *i = malloc(sizeof(int) * 5);  

... but to make it work in both C and
C++ one would need to use an explicit
cast:

int *i = (int *) malloc(sizeof(int) * 5)

Another common portability issue is
that C++ defines many new keywords,
such as new and class, that may be
used as identifiers (e.g. variable
names) in a C program.

This wikipedia article has further differences as well:

C++ compilers prohibit goto from crossing an initialization, as in the following C99 code:

 void fn(void)
{
goto flack;
int i = 1;
flack:
;
}

What should you learn first?

You should learn C++ first, not because learning C first will hurt you, not because you will have to unlearn anything (you won't), but because there is no benefit in learning C first. You will eventually learn just about everything about C anyway because it is more or less contained in C++.

why In C all executable code is contained within functions

Is this for optimization purposes ?

No.

If not why Dennis Ritchie designed the language this way:

Because this is how (most) mainstream high level programming languages designed before C were designed to worked. (The main exceptions to this were COBOL, BASIC and RPG.)

As all influentual programming language designers do, Dennis Ritchie adopted (and adapted) ideas that worked well in earlier languages, and discarded ideas that didn't. The "works well" is determined by the collective experience of the people who developed the language compilers, and the thousands or millions of programmers used the languages to write working programs.

Does C have 32 or 44 keywords?

  • The claims of there being "32" keywords in C refer to the original ANSI-specified version of C from 1989, aka C89.
    • Because this is the Internet, and because the real C specifications are behind ISO's ridiculous paywall most people so-inclined probably can't fact-check the claim.
    • And it's not a claim worth fact-checking: the number of keywords in a language is utter trivia of no consequence.
  • The ISO/IEC 9899 specification you linked to refers to C17 (the proposed updated C specification in 2017) which postdates C89 by 28 years.
  • It should come as no surprise that a future updated revision of a programming language introduces new keywords.

Historically, when C was introduced, people were impressed by its minimalist syntax and how the language's design effectively reified everything by implementing functionality as library features instead of language features which is what keeps C simple and helps mitigate every language's designer's fears about feature creep.

In comparison, C's early contemporaries like COBOL, opted to implement functionality into their own languages as first-class features, which is why COBOL has over 300 keywords; so I'll admit that using the stark difference in keyword-count does serve as a proxy for language-complexity and by extension: a way to almost quantify good design. But using it as the basis for comparing languages today in 2021 is of limited-utility as the most relevant programming languages today1 are already either inspired by C or derived from it somehow, and they all share C's decision to do things in the library instead of the language, so all those languages similarly have a low keyword count compared to COBOL, SQL, and others, and so that's why C's keyword count just isn't interesting anymore.


1: C-inspired or C-derived languages in use today: C++, Objective-C, Java, Groovy, Swift, C#, JavaScript, TypeScript, Go, PHP, Perl. Other popular languages that aren't modelled on C (like Haskell, OCaml, etc) do share C's library-first philosophy, but I can't say if C originated it or not - but I feel that languages opting for library-first designs is inevitable: the cost to implement language-features is easily ten-fold that of implementing library features.

Where can I find a C programming reference?

"The C Programming Language"



Related Topics



Leave a reply



Submit