Home > Backend Development > C++ > How Does C 's Stricter Rules Differ from C's More Lenient Approach?

How Does C 's Stricter Rules Differ from C's More Lenient Approach?

Barbara Streisand
Release: 2024-12-15 05:06:30
Original
157 people have browsed it

How Does C  's Stricter Rules Differ from C's More Lenient Approach?

C 's Incompleteness: Where C Diverges from Its Subset

Despite frequent claims that C is wholly contained within C , subtle distinctions render this assertion incomplete. This article highlights instances where code acceptable in C becomes incompatible in C , shedding light on the differences that set them apart.

Intricate Definiteness

C enforces stricter rules for variable definitions, prohibiting tentativeness. Declaring the same variable repeatedly, as in int n; int n;, is impermissible in C , unlike in C.

Array Compatibility Disparity

C introduces type compatibility constraints that don't exist in C. In C, an array int a[1] can be assigned to a pointer int (*ap)[] = &a, despite being of different types (int[] vs. int[1]). This assignment would fail in C .

Obsolete Function Definitions

C abandons the K&R style of function definitions, rendering constructions like int b(a) int a; { } syntactically incorrect.

Nested Struct Scope Difference

Nested structs in C have class scope, unlike in C. Consequently, declaring a nested struct outside of an enclosing struct definition, such as struct B b;, results in an incomplete type error in C .

Explicit Type Specifiers Required

C mandates explicit type specifiers for all declarations. Omitting type information, as in auto a;, leads to syntax errors.

C99 Contributes Further Discrepancies

C99 introduces additional incompatibilities:

  • Array Dimensions: Declaration specifiers in array dimensions of parameter lists must now align with C syntax (void f(int p[static 100]) { }).
  • Variable Length Arrays: C prohibits non-constant expressions for array dimension calculations (int n = 1; int an[n];).
  • Flexible Array Members: C disallows flexible array members (struct A { int a; int fam[]; }).
  • Restrict Qualifiers: C introduces restrict qualifiers for alias analysis, which are absent from C (void copy(int *restrict src, int *restrict dst);).

The above is the detailed content of How Does C 's Stricter Rules Differ from C's More Lenient Approach?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template