0

Hey so I'm looking into C examples where the code will not compile due to its type error but not give any run time error when the user executes the program. I can't seem to find much about this topic. One example I found of this is: "if an expression always evaluates to true at run-time, a program containing the code if <complex test> then 42 else <type error> will be rejected as ill-typed, because a static analysis cannot determine that the else branch won't be taken 'ref = https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages'".

I'm looking into more examples of Static typing errors that aren't conditional

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
Gringo
  • 81
  • 5
  • 3
    If you get a compiler error, you don't have a program to run. Your question makes absolutely no sense whatsoever. It's like you're throwing together a bunch of unrelated concepts. – Mad Physicist Mar 22 '21 at 10:33
  • If your compiler refuses to compile your code, what program do you expect to cause a runtime error? – Gerhardh Mar 22 '21 at 10:39
  • Unless you mean different compilers or different compilation flags (looking at you, -Werror), but as a general rule, if a compiler throws you an error, there's no executable. – Adalcar Mar 22 '21 at 10:39
  • An example where part of the source code does not compile but the program can be executed is where file `a.c` contains `int b(void); int main(void) { return 1 ? 0 : b(); }` but the definition of `b` in `b.c` is `int b(void) {` / `#error` / `}`. The file `b.c` will not compile, but, depending on the compiler, `a.c` may be compiled with optimization, linked, and executed. – Eric Postpischil Mar 22 '21 at 11:55
  • You should specify more about what you mean by “type error.” The C standard specify syntax rules and constraints that compilers are required to diagnose violations of, but it does permit them to translate programs containing violations of those rules and constraints. If you want to ask about programs that **will** compile even though they have errors rather than programs that **will not** compile, that is a different question. – Eric Postpischil Mar 22 '21 at 11:57

1 Answers1

2

C is a statically typed language. So the example(s) you're seeking aren't available in C (if your program doesn't compile, there's no "runtime" - what do you execute?). However, you're misinterpreting (or misunderstanding) what that quote is trying to convey.

if an expression always evaluates to true at run-time, a program containing the code if then 42 else will be rejected as ill-typed. because a static analysis cannot determine that the else branch won't be taken.

says code like the example below won't compile in a statically typed language like C:

#include <stdio.h>

int main(void)
{
    int i = 0;
    int  x = 5;

    if (42)
        x = 0;
    else
        x = "str";

    printf("%d\n", x);
}

(Here the if clause is always true and thus the else clause with type mismatch should never get to execute).

And that's mostly true except for "static analysis cannot determine that the else branch won't be taken.". Even if static analysers can detect that the else branch won't be taken, type checking is generally done and diagnostics emitted. So that part of the quote isn't quite accurate.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Thanks. I think I had to reconsider my question more specific for examples of type-systems that rejects at compile time, but no run-time error, if you can say so – Gringo Mar 22 '21 at 10:58
  • The code shown in this answer as “the example below” that is said not to “compile in a statically typed language like C” does in fact compile and execute in Clang and GCC with default options. A required warning is issued, but that does not prevent translation or execution. – Eric Postpischil Mar 22 '21 at 11:59