Casting literals causes trouble

Posted By: Uhrwerk

Casting literals causes trouble - 02/05/13 21:33

This one really knocked my socks off. shocked
Code:
#include <acknex.h>

int x = (int)1;

void main()
{
	printf("%d",x);
}

I still have a feeling I overlooked something...
Posted By: MasterQ32

Re: Casting literals causes trouble - 02/05/13 23:16

well, that's indeed strange
Posted By: jcl

Re: Casting literals causes trouble - 02/06/13 13:00

It admittedly looks unsuspicious, but a look in the manual reveals that it is in fact wrong syntax, at least in lite-C. This is also a bug, as the compiler should give a syntax error here.

Opportunity for a user quiz: Who can put Uhrwerk back in his socks, show where his code won't work, and fix it with a very small modification? Solution tomorrow.

Posted By: Uhrwerk

Re: Casting literals causes trouble - 02/06/13 13:35

Fixing the code isn't a problem as the cast is pointless and simply can be omitted. It also works when defining x as a local variable. But that does not help me get my socks back on... tongue

I'm curious where the syntax error hides...
Posted By: jcl

Re: Casting literals causes trouble - 02/06/13 13:40

The solution is not omitting the cast. Typecasting a literal is an allowed operation in C.
Posted By: Uhrwerk

Re: Casting literals causes trouble - 02/06/13 13:47

I'm glad the language supports this exotic feature! grin
Posted By: sivan

Re: Casting literals causes trouble - 02/06/13 14:08

-
Posted By: jcl

Re: Casting literals causes trouble - 02/07/13 09:31

Ok, here's the solution:

In a programming language, all expressions must be inside functions. Outside a function you can initialize a variable with a constant, but not with an expression.

The reason is that the result of an expression is normally not known at compile time, but only at runtime. In lite-C an expression is any value that is not a constant.

int x = 1; // ok
int x = y; // not ok
int x = sqrt(1); // not ok
int x = (int)1; // not ok

I hope you see now why your code can not work. Here's the fixed version:

Code:
#include <acknex.h>

int x;

void main()
{
	x = (int)1;
	printf("%d",x);
}


Posted By: Rackscha

Re: Casting literals causes trouble - 02/07/13 10:33

@JCL: Constant expressions can be evaluated in programminglanguages tongue (and thats what (int)1 is)
And maybe its possible to add an error message if a dynamic expression is used for initialization? It does not work, so the compiler should abort(or am i wrong?)
Posted By: Uhrwerk

Re: Casting literals causes trouble - 02/07/13 18:33

Originally Posted By: jcl
In a programming language, all expressions must be inside functions.
That's not true. Neither for programming languages in general, nor for Lite-C. The c standard includes constant expressions for very good reason. And you obviously followed the standard at very least partially:
Code:
#include <acknex.h>

int my_flag = PASSABLE;

void main()
{
	printf("%d",my_flag);
}


You can also replace "PASSABLE" by "(1 << 9)", hence the expression evaluation cannot be done by the precompiler. It already has to be implemented in the compiler. You can even do more complex expressions, like "2 + 2 * 2 % 2". I did some quick tests and the results suggest that you already implemented correct operator precedence for static expressions. I'd love to see this functionality expanded to fully meet the c standard.

@Rakscha
Originally Posted By: jcl
This is also a bug, as the compiler should give a syntax error here.
© 2024 lite-C Forums