What’s the mistake?
Has a ;
in the #define
, and should not have, this is wrong syntax. If the class taught like this, run away from it. In any compiler this does not do what you want.
Why different compilers display different errors?
Because they are different. They can handle the code as long as they obey the specification.
The language is not standardized?
Yes, and as long as the compiler respects what has been specified it can function as it wishes. If the specification told every detail to be followed it would not need to have more than one compiler. Each one does as he thinks best and each programmer chooses the compiler that best suits him.
The compiler can even add new things.
There are compilers that don’t even meet the specification. They say they compile C, but they don’t say they meet the standard. No one is required to meet the standard, just can’t say that answers without answering. Even so do not know if something would happen if you lie.
Or I’m not understanding the mistakes?
That only you can answer.
The mistake
Behold What is undefined behavior, unspecified and defined by the implementation?.
But that’s not even the case. The compiler can handle the errors however you want, you can check in the order you want, you can stop anytime you want.
As we do not have the errors in the two compilers we cannot compare the behaviors.
It is a fact that an error can trigger others and solving this, solves all, so much so that there is only one and showed 4, possibly show others if the code was larger or if the compiler did not stop when it already screwed up a lot.
And the error in one location generated error in another. First understand like the #define
works and the alternatives to it, what I would have used in this case. I consider it almost obsolete.
So when the code is compiled, there is a processing of the text exchanging all the texts MAX
for 50;
which is the text that is written in your code. Let’s see the first occurrence of it:
REGISTRO A[MAX];
Once processed it would be
REGISTRO A[50;];
That one ;
within the declaration of array is prohibited in any compiler.
There are compilers who "understand" the type of error and can give a better message, this is his prerogative to do, the language does not say how the error should be treated, what the message should be, none of that. Then some compiler may give a message indicating the error at the right place, that is on line 1, since the compiler may understand that this is a common error. But most will prefer to give the error where it actually occurred.
Smarter compilers are harder to develop, and often take longer to compile to find the real mistake. In general only compilers who have a lot of effort is smart, so I only recommend using "exotic" compilers if it’s really necessary.
And then one error triggers the others since the compiler understands that after 50 is closing the line. So the syntax on that line is wrong because it wasn’t closed, and the next line of code is wrong because no new line starts with a ]
, and he gets lost thinking that would come the closure of the structure and still end up leaving a ;
loose that is enclosing nothing, since there was an earlier closure where it should not.
That is why I say and I repeat: the person does not know how to program until he understands all the characters that are in his code, until the blank space. Everything has an influence on code, be it pro compiler or readability.
This code compiles:
#define MAX 50
typedef int TIPOCHAVE;
typedef struct {
TIPOCHAVE chave;
} REGISTRO;
typedef struct {
REGISTRO A[MAX]; //Linha 10
int nroElem;
} LISTA;
int main(void) {
// your code goes here
return 0;
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
It’s not that it’s "forbidden" to put the
;
at the end of#define
; is that, if the;
is in the definition, that;
will be replaced in the occurrences of token defined. This is rarely what one wants; but it may be that the programmer has taken this into account. In any case, the Preprocessor does not prohibit the use of;
at the end of#define
, and obediently replaces the token with the;
.– Wtrmute
@Wtrmute this, I was not happy in the words, I will edit
– Maniero
stdio include that. h was unnecessary? since we don’t have a print?
– Dorathoto
@Dorathoto actually stayed in the ideone feedback, I didn’t even need it. I improved the answer.
– Maniero